I'm Having an Apex Class which I used to deserialize the JSON. I'm calling the function which passed the string and sObject name which is Contact.

This is my String which i passed in the function with object name

    "attributes": {
        "type": "Contact",
        "url": "/services/data/v44.0/sobjects/Contact/0030b000026mjUsAAI"
    "Name": "89178917",
    "AccountId": "0010b00002EeEE0AAN",
    "Title": "8917",
    "Phone": "91+123-456-7890/0",
    "Email": "aslam.bari@gmail.com",
    "OwnerId": "0050b0000032MekAAE",
    "Time__c": "00:45:00.000Z",
    "Id": "0030b000026mjUsAAI",
    "Account": {
        "attributes": {
            "type": "Account",
            "url": "/services/data/v44.0/sobjects/Account/0010b00002EeEE0AAN"
        "Name": "Pepsi",
        "Id": "0010b00002EeEE0AAN"
    "Owner": {
        "attributes": {
            "type": "User",
            "url": "/services/data/v44.0/sobjects/User/0050b0000032MekAAE"
        "Alias": "abrow",
        "Id": "0050b0000032MekAAE"

Here is my code:

public static sObject ParseSobject(String jsonString,type typeOf){
    Map<string,Object> newUpList = (Map<String, Object>) JSON.deserializeUntyped(jsonString);
    sObject record = (sObject)typeOf.newInstance();  
    list<sObject> newList = new list<sObject>();      
    Map<String,Schema.SObjectField> fields = record.getSObjectType().getDescribe().fields.getMap();
    for(String str : newUpList.keySet()){  
        Schema.SoapType fieldType = fields.get(str).getDescribe().getSOAPType();            
        Schema.DescribeFieldResult fldDescribe = fields.get(str).getDescribe();  
        switch on fldDescribe.getType() {
            when TIME {                   
                record.put(fldDescribe.getName(), ParseTime(newUpList.get(str)+''));
            } when BOOLEAN {
                record.put(fldDescribe.getName(), boolean.valueOf(newUpList.get(str)));
            } when DOUBLE {
                record.put(fldDescribe.getName(), double.valueOf(newUpList.get(str)));
            } when DATE {
                record.put(fldDescribe.getName(), Date.valueOf(newUpList.get(str)));
            } when DATETIME {
                record.put(fldDescribe.getName(), datetime.valueOf(newUpList.get(str)));
            } when INTEGER {
                record.put(fldDescribe.getName(), integer.valueOf(newUpList.get(str)));
            }when STRING {


     return record;
public static time ParseTime(String timeString){
    // 14:26:41.276Z or "14:26:41.276Z"
    List<String> values = timeString.replace('Z', '').replace('"', '').split(':'); 

    // (14, 26, 41.276)         
    Integer hours = Integer.valueOf(values[0]); 
    Integer minutes = Integer.valueOf(values[1]);

    // 41.276 -> (41, 276) 
    Integer seconds = Integer.valueOf(values[2].split('\\.')[0]);
    Integer milliseconds = Integer.valueOf(values[2].split('\\.')[1]);

    return Time.newInstance(hours, minutes, seconds, milliseconds);
  • 3
    Please take a moment to scroll through the tour and read How to Ask. This question would be greatly improved if you edit it to include some words to describe what this code is supposed to do, and also explain how you are calling it. – Adrian Larson Jan 29 '19 at 18:14
  • Have you tried simply using JSON.deserialize? – Adrian Larson Jan 29 '19 at 21:29

The Name field on Contact is a compound field, and is therefore always read-only. You cannot populate a value in the Name field.

Unfortunately, there is no easy way to determine if a field is compound or not (link is to my blog). While you can check permissions on the field using fldDescribe.isCreateable(), I suspect what you're trying to do here is ignore permissions and create any field that is sent to you. As far as I'm aware, there's no way to distinguish in the Describe results when a field is not createable by nature versus not createable by permission.

That doesn't leave you with a great many options. One would be to have the service sending you this JSON stop including fields you know you can't create. Another would be to hard-code an exclusion on the Contact.Name field.

It's a fact that the Salesforce data model contains a lot of unexpected edge cases, making it more difficult than you would expect to write fully generalized code like this piece. If your org uses or encounters Person Accounts, you'll find another compound Name field there. There may be yet others of which I'm unaware, and of course there are also compound Geolocation and Address fields throughout the standard data model, although it looks like your logic would omit those fields as it stands.

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.