REST API : Create New Address Links

Hello everyone ,

I’m creating new billing address via REST API.I can insert normal type of variable on my request , it works fine. But I want to link it that billing address to my customer. Anyone can explain how to add on request ?

here is my request example :

  public function new_address(Request $request){
$data = $request->all();
try {
$result = $this->insert(
“New Address”,
array(
“address_title” => $data[‘address_title’],
“address_type” => $data[‘address_type’],
“address_line1” => $data[‘address_line1’],
“address_line2” => $data[‘address_line2’],
“city” => $data[‘city’],
“county” => $data[‘county’],
“state” => $data[‘state’],
“city” => $data[‘city’],
“state” => $data[‘state’],
“pincode” => $data[‘pincode’],
“email_id” => $data[‘email_id’],
“phone” => $data[‘phone’],
“fax” => $data[‘fax’],
// linked???
)
);
return response()->json([‘status’ => Response::HTTP_OK]);
} catch (Exception $e) {
var_dump($e);
}
}

thanks for your help !

You can pass, “links”: [ {“link_doctype”: “Customer”, “link_name”: “Test customer”} ] in your JSON to link the address to customer.

1 Like

Hello @Pawan thanks your help. i added.

“links” => array(“link_doctype”=>“Customer”,“link_name”=>“Test Customer”),

in my request but i got this error :

Traceback (most recent call last):\n File “/home/frappe/frappe-bench/apps/frappe/frappe/app.py”, line 66, in application\n response = frappe.api.handle()\n File “/home/frappe/frappe-bench/apps/frappe/frappe/api.py”, line 122, in handle\n “data”: frappe.get_doc(data).insert().as_dict()\n File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 222, in insert\n self.run_before_save_methods()\n File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 869, in run_before_save_methods\n self.run_method(“validate”)\n File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 765, in run_method\n out = Document.hook(fn)(self, *args, **kwargs)\n File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 1041, in composer\n return composed(self, method, *args, **kwargs)\n File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 1024, in runner\n add_to_return_value(self, fn(self, *args, **kwargs))\n File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 759, in \n fn = lambda self, *args, **kwargs: getattr(self, method)(*args, **kwargs)\n File “/home/frappe/frappe-bench/apps/frappe/frappe/contacts/doctype/address/address.py”, line 42, in validate\n deduplicate_dynamic_links(self)\n File “/home/frappe/frappe-bench/apps/frappe/frappe/core/doctype/dynamic_link/dynamic_link.py”, line 18, in deduplicate_dynamic_links\n t = (l.link_doctype, l.link_name)\nAttributeError: ‘str’ object has no attribute ‘link_doctype’\n

sended json :

{
    "address_title": "test",
    "address_line1": "test",
    "address_line2": "test",
    "city": "test",
    "state": "test",
    "county": "test",
    "country": "Turkey",
    "pincode": "48200",
    "email_id": "test@barankaraboga.com",
    "links": {
        "link_doctype": "Customer",
        "link_name": "Baran Karaboga"
    }
}

Okey I fixed error. Thank you @Pawan . I saw my missing when i want to show you my json response :slight_smile:

postfield json should be:

{
    "address_title": "test",
    "address_line1": "test",
    "address_line2": "test",
    "city": "test",
    "state": "test",
    "county": "test",
    "country": "Turkey",
    "pincode": "48200",
    "email_id": "test@barankaraboga.com", 
    "links": [
        {
            "link_doctype": "Customer",
            "link_name": "Baran Karaboga"
        }
    ]
}

i hope it will help somebody :

array(
              "address_title" => $data['address_title'],
              "address_line1" => $data['address_line1'],
              "address_line2" => $data['address_line2'],
              "city" => $data['city'],
              "state" => $data['state'],
              "county" => $data['county'],
              "country"=>$data['country'],
               "pincode" => $data['pincode'],
               "email_id" => $data['email_id'],
               "phone" => $data['phone'],
               "fax" => $data['fax'],
               "links" => array(array("link_doctype"=>"Customer","link_name"=>$data['name']))
               )
            );
1 Like