Adding a new mobilenumber to a person using REST API?

Hi,

I'm trying to use the new REST API to make a couple of changes to a person. There are two tasks I want to achieve; add/update e-mailaddress, and add/update mobile phone.

I need to make examples of those two scenarios to help the inhouse developers at a customersite, and use curl to make the examples as simple as possible.

For the first task, to add/update e-mail address I've found a neat metod on the PersonAgent called AddEmailAddressToPerson, and call it like this.

curl -s --user webapiuser:secret --request POST https://crm.example.com/SuperOffice/api/v1/Agents/Person/AddEmailAddressToPerson -H "Content-Type: application/json" --data '{"PersonId": 766, "EmailAddress": "the.new@email.no", "SetAsPrimaryEmail": true}' -v

That works fine. Calling it will add e-mail address to the person.

However the next task is to add/update mobile number for a person. That seems a bit trickier.

I'm able to update the mobilenumber, but only if the person allready has at least one mobilenumber. For that I do this:

curl --silent --user webapi:secret --request PATCH https://crm.example.com/SuperOffice/api/v1/Person/766 --data '[{"op":"replace","path":"/MobilePhones/0/Value", "value":"99887766"}]'

If the person doesn't have any mobilenumbers from before this will crash with an error saying

Failed to set value at path \"/MobilePhones/0/Value\" while performing \"replace\" operation: Failed to get value from path \"/MobilePhones/0\": Cannot access index 0 from collection at path \"/MobilePhones\".

I tried using the "add" operator, but it seems to require a more complex JSON object to be passed in as the Value, and I couldn't figure out the correct syntax.

I guess one alternative could be to call the PersonAgent.GetPersonEntity method, change the personentity, and send the entire structure back, but I was hoping there was a slimmer solution than that. The PersonAgent.AddEmailAddressToPerson method worked exactly like I wanted, so perhaps there is a simple way to handle this for mobilephones that I'm just not seeing?

 

RE: Adding a new mobilenumber to a person using REST API?

Hi Frode,

You just have to remember the type you are trying to set. MobilePhones is an array of EntityElement ( EntityElement[ ] ), so you need to pass an array to the value.

PATCH /api/v1/Person/9
Authorization: ...
Accept: application/json; charset=utf-8
Content-Type: application/json; charset=utf-8

[ 
  { 
    'op' : 'replace',
    'path': 'MobilePhones', 
    'value' : 
    [
      {
        'Description':'Personal cell phone',
        'Value':'99887766'
      }
    ]
   }
]



Hope this helps!

By: Tony Yates 7 Apr 2021

RE: Adding a new mobilenumber to a person using REST API?

Perfect Tony, thanks! That seems to work like I want it to.

By: Frode Lillerud 7 Apr 2021