We’ve developed some resources to help you work effectively from home during COVID-19 Click to learn more

Example-code for using REST to update an udeffield

Hello, 

I'm having some trouble figuring out how to update an udeffield through REST in Online. It is a simple update of a short text-field and I want to do it through the standard API instead of creating a custom REST-endpoint that sets the field through CRMScript. 

I have figured out i need to use the CustomFields property, and i guess PATCH is the way to go here: 
https://community.superoffice.com/documentation/sdk/SO.NetServer.Web.Services/html/v1DocumentEntity_PatchDocumentEntity.htm

Working from the example in the SDK i figured something like this would do the trick:

[
{
"op": "replace",
"path": "/CustomFields",
"value": {
"value1": {
"SuperOffice:1": "what"
}
}
}
]

Does anyone have an example i can look at? I'm either setting path wrong, and/or im not creating the correct object that the value expects.

Any input is appreciated :) 

//Eivind

RE: Example-code for using REST to update an udeffield

I thought UserdefinedFields and ExtraFields were deprecated, but if i do run a GET /api/v1/Document/{id} the returned object looks like this: 

"UserDefinedFields": {
    "SuperOffice:1": "value"
  },
"ExtraFields": {},
"CustomFields": {},

I looked at this some more and I havent found a way to update the udeffield throught the CustomFields property. 
In other words this works: 

[
  {
    "op": "replace",
    "path": "UserDefinedFields",
    "value": {
      "SuperOffice:1": "temp"
    }
  }
]


This does not: 

[
  {
    "op": "replace",
    "path": "CustomFields",
    "value": {
      "SuperOffice:1": "temp"
    }
  }
]

I assumed this would work in a smiliar way as Frode explained in this thread: 
https://community.superoffice.com/en/developer/forum/rooms/topic/netserver-api-group/core-components/how-to-use-contactentityextrafields/ 

#setLanguageLevel 3;

NSContactAgent contactAgent;
NSContactEntity contact = contactAgent.GetContactEntity(2737);
Map customFields;
customFields.insert("x_number", "1234");         //Extrafield, number
customFields.insert("x_text_short", "Hello");    //Extrafield, text
customFields.insert("SuperOffice:10", "World!"); //Userdefined, text
customFields.insert("SuperOffice:12", "2");      //Userdefined, list
contact.SetCustomFields(customFields);

contact = contactAgent.SaveContactEntity(contact);

Looks like you can use the 'old fieldnames' from both Service and Sales to set values on both 'types of udeffields' in CRMscript. This does not look to be the case when updating through REST? 

The documentation doesnt really give me what i need to figure out what it 'expect' to be able to update the correct field: 

 "UserDefinedFields": {
    "SuperOffice:1": "True",
    "SuperOffice:2": "1011391663"
  },
  "ExtraFields": {
    "ExtraFields1": "cum",
    "ExtraFields2": "fugiat"
  },
  "CustomFields": {
    "CustomFields1": "totam",
    "CustomFields2": "dolores"
  }

Btw this is a new udef-field created on version 9 (SOD) of SuperOffice, if it makes a difference :) 

//Eivind

Von: Eivind Johan Fasting 10. Jun 2020

RE: Example-code for using REST to update an udeffield

HI Eivind,

I believe you need to express the value property as an array:

[
  {  
    "op": "replace",
    "path": "CustomFields",
    "value": [
      {     
        "SuperOffice:1": "temp"
      }
    ]
  }
]

I haven't tried it, but hope this helps!

Von: Tony Yates 11. Jun 2020

RE: Example-code for using REST to update an udeffield

Hi Tony, 

Tried it like this: 

[
  {
    "op": "replace",
    "path": "CustomFields",
    "value": [
      {
        "SuperOffice:1": "1ae3d407-b5d6-4d95-9f24-8de0d315ec76"
      }
    ]
  }
]

Unfortunately i still get "StatusCode: 400, ReasonPhrase: 'Bad Request' " :( 


//Eivind

Von: Eivind Johan Fasting 11. Jun 2020

RE: Example-code for using REST to update an udeffield

Hi Eivind, 

Just following the example from the REST API helped. This worked for me.

PATCH https://sod.superoffice.com/Cust12345/api/v1/contact/32 HTTP/1.1
Authorization: Bearer 8A:...
Content-Type: application/json
Accept: application/json

    { 
        "op": "replace", 
        "path": "CustomFields", 
        "value": {
            "SuperOffice:1": "Testing"
        }
    } 
]
Von: Tony Yates 15. Jun 2020