3

All,

I'm trying to create a Contact via the REST API - that works without issues if I don't include custom boolean fields in the request. However, my Contact object contains a pair of custom boolean values:

isSystemUser__c
MaintenanceEmailOptedOut__c

Whenever I try to set these via Java REST POST commands, I end up with an HTTP 400.

In the workbench I can create these without issues. I set the request to POST, with a URL of /services/data/v36.0/sobjects/Contact and data as follows:

{"accountId":"001g000000myI4m","email":"test@test.com","hasOptedOutOfEmail":true,"MaintenanceEmailOptedOut__c":true,"firstName":"firstname","lastName":"lastname","phone":"123-456-7890","isSystemUser__c":false,"Role__c":"role"}

Similarly, in my Java Code, the creation succeeds if I only pass in the following fields (note that role is a custom string field but works without issues):

String json = "{\"AccountId\":\"001g000000myI4m\",\"Email\":\"test@test.com\",\"FirstName\":\"firstname\",\"LastName\":\"lastname\",\"Phone\":\"123-456-7890\",\"HasOptedOutOfEmail\":\"true\",\"Role__c\":\"dummyrole\"}";
StringEntity customerEntity = new StringEntity(json);
httpPost.addHeader("Content-Type","application/json; charset=UTF-8");
httpPost.addHeader("Accept", "application/json");
httpPost.setEntity(customerEntity);
HttpResponse response = httpClient.execute(httpPost);

However, as soon as I add the pair of custom boolean values, I get that pesky 400. Is there some different way of handling custom boolean attributes? I've verified that the API name for these fields is correct, and the fact that it works within workbench leads me to believe that the JSON is fine. I've tried passing in different values ("1"/"0" instead of "true"/false" for example) with no joy.

Can anyone lend any expertise here?

Thanks in advance!

PS

When I was swapping "1"/"0" I also tested true and false (without the quotes) and still get a 400. I've re-tested that again and have received the same result. My confusion stems mainly from the fact that I can set the HasOptedOutOfEmail boolean property (a standard field) via the API, using a String value of "true" (with the quotes) but am unable to do the same for the custom field.

  • Can you confirm that your custom fields do not require a namespace? You might also try using JSON.serialize on a Contact in an execute anonymous window to see the format that the data is serialised into. – Keith C Mar 10 '16 at 18:50
  • 2
    It sounds like your user in the Java context doesn't have the correct access those two fields. Check the security (Profile or Permission Set) of the user that you are connected with from your Java app. Are you using the same User in the Java app that you used successfully in the Workbench? – Mark Pond Mar 10 '16 at 19:44
  • do you have namespace enabled ? – Himanshu Mar 10 '16 at 19:50
  • @KeithC: Thanks - I believe that we don't use a namespace. When I go to Packages->I don't see any defined (and it claims we have unmanaged only - we need to sign up for developer edition?) I tried playing with the execute anonymous window but was unable to figure out how to use it for this purpose (I have issued commands such as setting up a pushtopic, and have run queries in the dev console, but am otherwise new to SF!) Any other tips would be appreciated! – Salman Mar 10 '16 at 22:19
  • @MarkPond: That's a good point. The Java App user is based on an API Only profile I created; it has permissions to standard objects such as Contacts. When I login to Workbench it uses my own credentials and succeeds. However, I am able to create a more stripped-down contact from the Java app (after excluding the custom fields). Is there some fine grained permission that would prevent me from adjusting those fields from a certain profile? I have not defined any permission sets yet. – Salman Mar 10 '16 at 22:23
3

Check the security on your two custom fields to see if the API Only profile has access to those two fields.

Setup > Customize > Contacts > Fields > [your custom field] > Set Field-Level Security (button)

This will let you see the access each one of your Profiles has to this specific field.

  • 1
    Thanks so much! That did the trick. The profile, which I had added after the custom fields had been created, had "read only" access to those custom fields. After unchecking that read only field, I was able to use the Java app to do it. – Salman Mar 11 '16 at 0:05
0

JSON supports the tokens true and false (note no quotes and different from the strings "true" and "false") as values. Using those - removing the quotes - should fix this.

I recommend spending a few minutes reading this short Introducing JSON page to be clear about the structure of JSON - it is pretty simple.

PS

When I run:

System.debug(JSON.serializePretty([select Name, Flag__c from contact limit 1]));

I get this JSON i.e. the token false is output for the checkbox field (and in this case the field has the namespace cvx):

[  
  {  
    "attributes":{  
      "type":"Contact",
      "url":"..."
    },
    "Name":"Joe Random",
    "cvx__Flag__c":false,
    "Id":"003i000000bUJiiAAG"
  }
]
  • Keith, thank you for the suggestion. When I was swapping "1"/"0" I also tested true and false (without the quotes) and still get a 400. I've re-tested that again and have received the same result. My confusion stems mainly from the fact that I can set the HasOptedOutOfEmail boolean property (a standard field) via the API, using a String value of "true" (with the quotes) but am unable to do the same for the custom field. Is that clearer? – Salman Mar 10 '16 at 18:38
  • @Salman It is. I'll add your comment back into the question so it stands out. Sorry my suggestion was wrong. – Keith C Mar 10 '16 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.