how-to-edit-partner-addresses-via-api-in-odoo-17

How to Edit Partner Addresses via API in Odoo 17?

Managing partner addresses efficiently is crucial for any business using Odoo 17. If you’re new to Odoo or API integrations, don’t worry! This blog will walk you through implementing an API endpoint that allows users to edit the addresses of an existing parent partner. You can smoothly modify address details by utilizing Odoo’s controller mechanism and JSON-based API requests, ensuring data accuracy and reducing manual errors.

Why Edit Partner Addresses via API?

In large-scale business operations, updating customer and partner addresses details manually can be time-consuming and error-prone. An API-based approach ensures:

  • Automation

    Updates can be performed programmatically.

  • Accuracy

    Reduces manual data entry errors.

  • Efficiency

    Saves time by allowing bulk updates.

  • Security

    Controlled access with authentication ensures data integrity.

  • Scalability

    Works well for businesses handling large volumes of partner data.

Who Should Use This API?

This API is ideal for:

  • Business Owners

    Reorganize address updates and maintain accurate records.

  • Developers

    Integrate address updates into existing workflows.

  • E-commerce and Logistics Companies

    Ensure delivery accuracy and customer satisfaction.

  • Odoo Administrators

    Maintain a clean and updated partner database effortlessly.

Step 1: Overview of API Endpoint

Understanding the API Implementation

We have defined a custom API endpoint /api/v2/edit_partner using Odoo’s http.route. This endpoint allows authenticated users to update address details of an existing partner.

The API is structured as follows:

  • Route: /api/v2/edit_partner
  • Type: JSON
  • Authentication: User-based
  • Method: POST
  • CSRF Protection: Disabled

Step 2: Code Implementation

2.1 API Route Definition

The API route is defined using Odoo’s controller framework:

				
					@http.route('/api/v2/edit_partner', type='json', auth='user', methods=['POST'], csrf=False)
    def edit_partner(self, **kwargs):

				
			

This ensures that only authenticated users can make requests, enhancing security.

2.2 Extracting and Processing Input Data

When a request is received, we extract JSON data from the request object:

        Try:

				
					 data = request.jsonrequest

				
			

        Except AttributeError:

				
					data = request.httprequest.get_json()
        parent_partner_id = data.get('parent_partner_id', '')
        type = data.get('type', '')
        street = data.get('street','')
        street2 = data.get('street2','')
        city = data.get('city','')
        state_id = data.get('state_id','')
        country_id = data.get('country_id', 0)
        email = data.get('email','')
        phone = data.get('phone','')
        zip = data.get('zip','')
        is_default = data.get('is_default','')
        address_type = data.get('address_type','')

				
			

2.3 Updating the Partner Record in Odoo

The extracted data is then used to update the partner record:   

				
					 res = {}
        if street:
            res['street'] = street
        if street2:
            res['street2'] = street2
        if city:
            res['city'] = city
        if state_id:
            res['state_id'] = state_id
        if country_id:
            res['country_id'] = country_id
        if email:
            res['email'] = email
        if phone:
            res['phone'] = phone
        if zip:
            res['zip'] = zip
        if type:
            res['type'] = type
        if is_default:
            res['is_default'] = is_default
        if address_type:
            res['address_type'] = address_type
        if type == 'contact':
            rec = request.env['res.partner'].with_user(SUPERUSER_ID).browse(parent_partner_id)
            rec.write(res)
        return {'status': 'success', 'msg': 'Address updated successfully'}

				
			

This ensures that only valid fields are updated, preventing unnecessary data modifications.

Step 3: Testing the API

Testing via API Client

To test the API, send a JSON payload using a POST request to the following endpoint:

POST Request:
				
					POST {{localhost}}//api/v2/edit_partner

				
			
API Request Example
				
					{"parent_partner_id":16, "type": "contact", "street": "MIYAPUR", "street2": "454545", "city": "telangana", "state_id": 5, "country_id": 1, "email": "kali@example.com", "phone": "1111111111", "zip": "12345", "is_default": true, "address_type": "home"}

				
			
Expected Response
				
					{
   "jsonrpc": "2.0",
   "id": null,
   "result": {
       "status": "success",
       "msg": "address Updated successfully"
   }
}

				
			

Common Issues and Measures

  1. Invalid Parent Partner ID

    Ensure the parent_partner_id exists in the database.

  2. Missing Required Fields

    Validate inputs before processing.

  3. Authentication Issues

    Ensure that only authenticated users can access the endpoint.

  4. Data Integrity

    Prevent duplicate or incorrect data updates.

  5. CSRF Token Issues

    Since CSRF is disabled, ensure requests are from trusted sources.

Best Practices

  • Proper logging is used
    to track API usage and detect anomalies.
  • Implement role-based access control
    to enhance security.
  • Validate data
    before updating records to prevent inconsistencies.
  • Use exception handling
    to prevent crashes and improve error messages.

Partner Addresses Management with Odoo API

This API offers a well-structured and secure approach to updating partner addresses in Odoo 17. By utilizing JSON-based requests and Odoo’s controller framework, developers can ensure smooth address modifications while maintaining data consistency.

Implementing this API simplifies partner address management, improving efficiency and reducing manual intervention. Whether you’re an Odoo administrator, developer, or business owner, this solution helps keep your partner database accurate and current. Businesses can enhance their customer and partner data management processes by following best practices and handling potential errors effectively.

Would you like to see more API tutorials like this? Let us know in the comments!

Picture of Madhu Satya Deva Raju

Madhu Satya Deva Raju

I'm Madhu Satya Deva Raju P, a Junior Odoo Developer passionate about building solutions through Odoo development, customization, and integration. I share insights and experiences here to help others grow in the ERP ecosystem.

Get a free quote