function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
William Roach-BarretteWilliam Roach-Barrette 

Cannot update contact records: |EXCEPTION_THROWN|[56]|System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []

Afternoon, I am working on a program that will take in data our users give us regarding their contact prefrences, this data llives on a company server, and import that data into salesforce. I have created a program that pulls JSON from an endpoint and stores it in a wrapper. Then I attempt to convert the information im given into a list of contacts that I then use to update my contacts list of perrences "in this case thats just a few checkboxes" Im running into this error that might be due to the fact that the salesforceIDs of the contacts in my sandbox dont match the values in my production org. Still I thought I would post my code and the error to see what the group thinks:

ERROR:
15:44:26.17 (858546781)|EXCEPTION_THROWN|[56]|System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []
15:44:26.17 (858854119)|HEAP_ALLOCATE|[56]|Bytes:114
15:44:26.17 (858886596)|VARIABLE_SCOPE_BEGIN|[60]|e|System.DmlException|true|false
15:44:26.17 (858943076)|VARIABLE_ASSIGNMENT|[60]|e|"common.apex.runtime.impl.DmlExecutionException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []"|0x302efa02
15:44:26.17 (858951078)|STATEMENT_EXECUTE|[60]
15:44:26.17 (858953210)|STATEMENT_EXECUTE|[61]
15:44:26.17 (858957978)|HEAP_ALLOCATE|[61]|Bytes:33
15:44:26.17 (858999597)|HEAP_ALLOCATE|[61]|Bytes:110
15:44:26.17 (859015344)|HEAP_ALLOCATE|[61]|Bytes:143
15:44:26.17 (859034345)|USER_DEBUG|[61]|DEBUG|An unexpected error has occured: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []
15:44:26.17 (859050401)|METHOD_EXIT|[29]|01p1F000000y10l|JSONDeserialize.UpdateData(GDPRWrapper)
15:44:26.859 (859062803)|CUMULATIVE_LIMIT_USAGE
15:44:26.859 (859062803)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 200
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 282 out of 10000
  Maximum CPU time: 139 out of 60000
  Maximum heap size: 0 out of 12000000
  Number of callouts: 1 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 1
  Number of Mobile Apex push calls: 0 out of 10

15:44:26.859 (859062803)|CUMULATIVE_LIMIT_USAGE_END

15:44:26.17 (859096399)|CODE_UNIT_FINISHED|JSONDeserialize.deserialize
15:44:26.17 (860011897)|EXECUTION_FINISHED

CODE:
public class JSONDeserialize {

    public GDPRWrapper wrapper {get;set;}
   
    
    @Future(callout=true)
    public static void deserialize() {
        GDPRWrapper wrapper;
    
        
        Http h = new Http();
        HttpRequest request = new HttpRequest();
        
        request.setEndPoint('https://gdpr.rajant.com/pilot/api/gdpr/allOptInUsers');
        Blob headerValue = Blob.valueOf('d18849ea4155:d83ce6ef3dbe');
        String authorizationHeader = ('Basic ' + EncodingUtil.base64Encode(headerValue));
        
        
        request.setHeader('Authorization', authorizationHeader);
        request.setMethod('GET');
        
        HttpResponse response = h.send(request);
        
        
        List<GDPRWrapper.GDPRData> obj = GDPRWrapper.parse(response);
        wrapper = new GDPRWrapper(obj);
         
        System.assert(wrapper.GDPRList!=null);
        updateData(wrapper);
        

      }
      
      
      public static void UpdateData(GDPRWrapper wrapper){
                    List<Contact> contactPref = new List<Contact>();
          
        for(Integer i = 0; i < wrapper.GDPRList.size(); i ++){
            Contact toInsert = new Contact();
            toInsert.firstName = wrapper.GDPRList[i].firstName;
            toInsert.lastName = wrapper.GDPRList[i].lastName;
            toInsert.email = wrapper.GDPRList[i].email;
            
          //  toInsert.Id = wrapper.GDPRList[i].contactId;
            toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing;
            toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security;
            toInsert.Product_Information__c = wrapper.GDPRList[i].support;
            toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone;
            toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail;
            contactPref.add(toInsert);
            
          
        
        }
          try{
              update contactPref;
           
                        
            } 
            catch(DmlException e){
                    System.debug('An unexpected error has occured: ' + e.getMessage());
                }
      
      }
     }
 
public class GDPRWrapper{

    public GDPRWrapper(List<GDPRData> templst){
            GDPRList = templst;
        }
    public List<GDPRData> GDPRList {get; set;}

    public class GDPRData {

        public Integer gdprId {get; set;}  //26636
        public String firstName {get; set;}
        public String lastName {get; set;}
        public String email {get; set;}
        public String phone {get; set;}
        public String accountName {get; set;}
        public String contactId {get; set;}    //AA111222333AAAe
        public String emailHash {get; set;}    //78fcb5ad502033c46d35abcecb3615bd92757fb0451485a19b27b7515f6d82d0
        public String createDate {get; set;}   //2018-05-17T15:19:37.000+0000
        public String responseDate {get; set;} //2018-05-21T10:38:53.000+0000
        public String notifyDate {get; set;}
        public boolean marketing {get; set;}
        public boolean security {get; set;}
        public boolean support {get; set;}
        public boolean contactPhone {get; set;}
        public boolean contactEmail {get; set;}
        public boolean contactOther {get; set;}
        public boolean invalid {get; set;}
        
        
    }
   public static List<GDPRData> parse(httpResponse json){
        return (List<GDPRData>) System.JSON.deserialize(json.getBody(), List<GDPRData>.class);
    }
 
    static void testParse() {
        System.debug('Entered test Method');
        System.debug('Made it to start of deserialization');
        Http h = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndPoint('https://www.gdpr.rajant.com/pilot/api/gdpr/allOptInUsers');
        Blob headerValue = Blob.valueOf('d18849ea4155:d83ce6ef3dbe');
       String authorizationHeader = ('Basic ' + EncodingUtil.base64Encode(headerValue));
       System.debug('Authorization Header: ' + authorizationHeader);
        request.setHeader('Authorization', authorizationHeader);
        request.setMethod('GET');
        HttpResponse response = h.send(request);
        System.debug('Response: ' + response.getBody());
        List<GDPRData> obj = parse(response);
        System.assert(obj!=null);
        
        for(Integer i = 0; i < obj.size(); i ++){
            System.debug(obj[i]);
        
        }
        
        }
        
}

​TRIGGER:
trigger GDPR_Prefrences_Updater on Contact (after insert, after update) {
   if(System.IsBatch() == false && System.isFuture() == false){ 
    JSONDeserialize.deserialize();
} 

}
I am still extremely new to APEX so this might be a simple fix. I apoligize for the lack of expirence in advanced. And appreciate any and all feedback
 
Best Answer chosen by William Roach-Barrette
Raj VakatiRaj Vakati
You cannt able to set Id on the insert. Please comment this link 

//  toInsert.Id = wrapper.GDPRList[i].contactId;

All Answers

Raj VakatiRaj Vakati
public class JSONDeserialize {

    public GDPRWrapper wrapper {get;set;}
   
    
    @Future(callout=true)
    public static void deserialize() {
        GDPRWrapper wrapper;
    
        
        Http h = new Http();
        HttpRequest request = new HttpRequest();
        
        request.setEndPoint('https://gdpr.rajant.com/pilot/api/gdpr/allOptInUsers');
        Blob headerValue = Blob.valueOf('d18849ea4155:d83ce6ef3dbe');
        String authorizationHeader = ('Basic ' + EncodingUtil.base64Encode(headerValue));
        
        
        request.setHeader('Authorization', authorizationHeader);
        request.setMethod('GET');
        
        HttpResponse response = h.send(request);
        
        
        List<GDPRWrapper.GDPRData> obj = GDPRWrapper.parse(response);
        wrapper = new GDPRWrapper(obj);
         
        System.assert(wrapper.GDPRList!=null);
        updateData(wrapper);
        

      }
      
      
      public static void UpdateData(GDPRWrapper wrapper){
                    List<Contact> contactPref = new List<Contact>();
          
        for(Integer i = 0; i < wrapper.GDPRList.size(); i ++){
            Contact toInsert = new Contact();
            toInsert.firstName = wrapper.GDPRList[i].firstName;
            toInsert.lastName = wrapper.GDPRList[i].lastName;
            toInsert.email = wrapper.GDPRList[i].email;
            
          //  toInsert.Id = wrapper.GDPRList[i].contactId;
            toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing;
            toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security;
            toInsert.Product_Information__c = wrapper.GDPRList[i].support;
            toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone;
            toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail;
            contactPref.add(toInsert);
            
          
        
        }
          try{
              upsert contactPref;
           
                        
            } 
            catch(DmlException e){
                    System.debug('An unexpected error has occured: ' + e.getMessage());
                }
      
      }
     }

Use this code
Raj VakatiRaj Vakati
Or below code also works
public class JSONDeserialize {

    public GDPRWrapper wrapper {get;set;}
   
    
    @Future(callout=true)
    public static void deserialize() {
        GDPRWrapper wrapper;
    
        
        Http h = new Http();
        HttpRequest request = new HttpRequest();
        
        request.setEndPoint('https://gdpr.rajant.com/pilot/api/gdpr/allOptInUsers');
        Blob headerValue = Blob.valueOf('d18849ea4155:d83ce6ef3dbe');
        String authorizationHeader = ('Basic ' + EncodingUtil.base64Encode(headerValue));
        
        
        request.setHeader('Authorization', authorizationHeader);
        request.setMethod('GET');
        
        HttpResponse response = h.send(request);
        
        
        List<GDPRWrapper.GDPRData> obj = GDPRWrapper.parse(response);
        wrapper = new GDPRWrapper(obj);
         
        System.assert(wrapper.GDPRList!=null);
        updateData(wrapper);
        

      }
      
      
      public static void UpdateData(GDPRWrapper wrapper){
                    List<Contact> contactPref = new List<Contact>();
          
        for(Integer i = 0; i < wrapper.GDPRList.size(); i ++){
            Contact toInsert = new Contact();
            toInsert.firstName = wrapper.GDPRList[i].firstName;
            toInsert.lastName = wrapper.GDPRList[i].lastName;
            toInsert.email = wrapper.GDPRList[i].email;
            
          //  toInsert.Id = wrapper.GDPRList[i].contactId;
            toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing;
            toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security;
            toInsert.Product_Information__c = wrapper.GDPRList[i].support;
            toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone;
            toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail;
            contactPref.add(toInsert);
            
          
        
        }
          try{
              insert contactPref;
           
                        
            } 
            catch(DmlException e){
                    System.debug('An unexpected error has occured: ' + e.getMessage());
                }
      
      }
     }

 
William Roach-BarretteWilliam Roach-Barrette
Thanks for your responce, I was able to use the code in your above post and after seeing it run with no known erros I removed the comment from toInset.ID to see if I could update records based on the ID I have stored in JSON. Im running into this error now: 
Class.JSONDeserialize.UpdateData: line 44, column 1
Class.JSONDeserialize.deserialize: line 29, column 1
16:18:46.25 (603557624)|FATAL_ERROR|System.TypeException: Invalid id value for this SObject type: 00Q1300001N6BNPEA3

Class.JSONDeserialize.UpdateData: line 44, column 1
Class.JSONDeserialize.deserialize: line 29, column 1
16:18:46.603 (603563631)|CUMULATIVE_LIMIT_USAGE
16:18:46.603 (603563631)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 200
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 60000
  Maximum heap size: 0 out of 12000000
  Number of callouts: 1 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 1
  Number of Mobile Apex push calls: 0 out of 10

16:18:46.603 (603563631)|CUMULATIVE_LIMIT_USAGE_END

16:18:46.25 (603602284)|CODE_UNIT_FINISHED|JSONDeserialize.deserialize
16:18:46.25 (605269565)|EXECUTION_FINISHED
Here is the updated code for refrence:
 
public class JSONDeserialize {

    public GDPRWrapper wrapper {get;set;}
   
    
    @Future(callout=true)
    public static void deserialize() {
        GDPRWrapper wrapper;
    
        
        Http h = new Http();
        HttpRequest request = new HttpRequest();
        
        request.setEndPoint('https://gdpr.rajant.com/pilot/api/gdpr/allOptInUsers');
        Blob headerValue = Blob.valueOf('d18849ea4155:d83ce6ef3dbe');
        String authorizationHeader = ('Basic ' + EncodingUtil.base64Encode(headerValue));
        
        
        request.setHeader('Authorization', authorizationHeader);
        request.setMethod('GET');
        
        HttpResponse response = h.send(request);
        
        
        List<GDPRWrapper.GDPRData> obj = GDPRWrapper.parse(response);
        wrapper = new GDPRWrapper(obj);
         
        System.assert(wrapper.GDPRList!=null);
        updateData(wrapper);
        

      }
      
      
      public static void UpdateData(GDPRWrapper wrapper){
                    List<Contact> contactPref = new List<Contact>();
          
        for(Integer i = 0; i < wrapper.GDPRList.size(); i ++){
            Contact toInsert = new Contact();
            toInsert.firstName = wrapper.GDPRList[i].firstName;
            toInsert.lastName = wrapper.GDPRList[i].lastName;
            toInsert.email = wrapper.GDPRList[i].email;
            
            toInsert.Id = wrapper.GDPRList[i].contactId;
            toInsert.Sales_and_Marketing__c = wrapper.GDPRList[i].marketing;
            toInsert.Critical_Security_Notes__c = wrapper.GDPRList[i].security;
            toInsert.Product_Information__c = wrapper.GDPRList[i].support;
            toInsert.Contact_Via_Text__c = wrapper.GDPRList[i].contactPhone;
            toInsert.Contact_Via_Email__c = wrapper.GDPRList[i].contactEmail;
            contactPref.add(toInsert);
            
          
        
        }
          try{
              insert contactPref;
           
                        
            } 
            catch(DmlException e){
                    System.debug('An unexpected error has occured: ' + e.getMessage());
                }
      
      }
     }

 
Raj VakatiRaj Vakati
You cannt able to set Id on the insert. Please comment this link 

//  toInsert.Id = wrapper.GDPRList[i].contactId;
This was selected as the best answer
William Roach-BarretteWilliam Roach-Barrette
Ah that’s my problem, can I update using the ID as a method to search and match contact records?
Raj VakatiRaj Vakati
You can not use Insert that case use upsert ( Combination of insert and update ) operation .. 
William Roach-BarretteWilliam Roach-Barrette
Thanks, I appreciate you going this low level with me. I changed my code but am still getting this error:
10:24:20.33 (879116129)|FATAL_ERROR|System.TypeException: Invalid id value for this SObject type: 00Q1300001N6BNPEA3

Class.JSONDeserialize.UpdateData: line 44, column 1
Class.JSONDeserialize.deserialize: line 29, column 1
10:24:20.33 (879148426)|FATAL_ERROR|System.TypeException: Invalid id value for this SObject type: 00Q1300001N6BNPEA3

Class.JSONDeserialize.UpdateData: line 44, column 1
Class.JSONDeserialize.deserialize: line 29, column 1
10:24:20.879 (879161211)|CUMULATIVE_LIMIT_USAGE
10:24:20.879 (879161211)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 200
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 60000
  Maximum heap size: 0 out of 12000000
  Number of callouts: 1 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 1
  Number of Mobile Apex push calls: 0 out of 10

10:24:20.879 (879161211)|CUMULATIVE_LIMIT_USAGE_END

10:24:20.33 (879223376)|CODE_UNIT_FINISHED|JSONDeserialize.deserialize
10:24:20.33 (880929951)|EXECUTION_FINISHED

My guess is I have both contactId(s) and Lead Id(s) stored here and I need to ignore the lead ID(s) because they dont match the contact object type. Is there any way I can catch the error, ignore that value and keep going? 
William Roach-BarretteWilliam Roach-Barrette
Can I just ignore Ids with the starting identifier 00Q?