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
Brad007Brad007 

System.DmlException: Upsert failed. First exception on row 0

Hi ,

System.DmlException: Upsert failed. First exception on row 0 with id a1WV00000001HBKMA2; first error: MISSING_ARGUMENT, External_ID__c not specified: []

I have my upsert opration failing. Can any one please suggest whats wrong in the below code and how help fix the code.

public with sharing class APImageUpdatesGlobal {
    public void APFilesMissingGlobal()
        {
          string ServiceResult = '';  
          System.debug('Entered!');
      
          // Create list for Available Aupairs
          List<AP_Application__c> APFiles = [Select a.Photo4__c, a.Photo3__c, a.Photo2__c, a.Photo1__c, a.Id, a.External_ID_Photo4__c, a.External_ID_Photo3__c, a.External_ID_Photo2__c, a.External_ID_Photo1__c, a.Au_Pair__c From AP_Application__c a where a.Au_Pair__r.AP_Status__c ='Available' AND a.Au_Pair__r.Record_Type_Name__c= 'Au Pair'];      
          
          string XmlString = '<FilesData>';                
          for(AP_Application__c APFile: APFiles)
            {    
              XmlString += '<APFiles>';
              XmlString += '<ApplicationId>'+APFile.Id+'</ApplicationId>';
              XmlString += '<APID>'+APFile.Au_Pair__c+'</APID>';
              XmlString += '<Path>'+APFile.Photo1__c+'</Path>';  
              XmlString += '<External_ID>'+APFile.External_ID_Photo1__c+'</External_ID>';
              XmlString += '<Inactive_Dt></Inactive_Dt>';  
              XmlString += '</APFiles>';    
              
                XmlString += '<APFiles>';
              XmlString += '<ApplicationId>'+APFile.Id+'</ApplicationId>';
              XmlString += '<APID>'+APFile.Au_Pair__c+'</APID>';
              XmlString += '<Path>'+APFile.Photo2__c+'</Path>';
              XmlString += '<External_ID>'+APFile.External_ID_Photo2__c+'</External_ID>';
              XmlString += '<Inactive_Dt></Inactive_Dt>';
              XmlString += '</APFiles>';  
              
              XmlString += '<APFiles>';
              XmlString += '<ApplicationId>'+APFile.Id+'</ApplicationId>';
              XmlString += '<APID>'+APFile.Au_Pair__c+'</APID>';
              XmlString += '<Path>'+APFile.Photo3__c+'</Path>';
              XmlString += '<External_ID>'+APFile.External_ID_Photo3__c+'</External_ID>';
              XmlString += '<Inactive_Dt></Inactive_Dt>';
              XmlString += '</APFiles>';  
              
              XmlString += '<APFiles>';
              XmlString += '<ApplicationId>'+APFile.Id+'</ApplicationId>';
              XmlString += '<APID>'+APFile.Au_Pair__c+'</APID>';
              XmlString += '<Path>'+APFile.Photo4__c+'</Path>';  
              XmlString += '<External_ID>'+APFile.External_ID_Photo4__c+'</External_ID>';
              XmlString += '<Inactive_Dt></Inactive_Dt>';                
              XmlString += '</APFiles>';            
            }
              //Close root Xml
            XmlString += '</FilesData>';         
            GAPOrg.AjaxSoap Servicestub = new GAPOrg.AjaxSoap();
            ServiceResult = Servicestub.GAPAPFilesGlobalRun(XmlString);          
            Dom.Document docx = new Dom.Document();
              docx.load(ServiceResult);
              Dom.Xmlnode root = docx.getRootElement();
              List<Files__c> upsertfiles = new List<Files__c>(); 
              for(Dom.Xmlnode child : root.getChildElements())    {             
               Files__c file = null;           
               for(Dom.Xmlnode node : child.getChildElements())      {                  
                   if(node.getName() == 'External_ID') {                    
                     string str = node.getText();               
                          if( str != ''){
                             file = new Files__c();        file = [Select f.Path__c, f.Inactive_Date__c, f.Id, f.External_ID__c, f.Contact__c, f.AP_Application__c From Files__c f where f.External_ID__c =: node.gettext() limit 1 ];
                     }
                 } 
                 else if(node.getName() == 'Inactive_Dt')
                 {
                   if(file != null) {
                     if(node.getText() != ' ')
                     {
                      String[] stringDate = node.gettext().split('/');                     
                       Integer m =  Integer.valueOf(stringDate[0]);
                       Integer d = Integer.valueOf(stringDate[1]);
                       Integer y = Integer.valueOf(stringDate[2]);  
                      file.Inactive_Date__c = date.newinstance(y,m,d);                   
                     }
                   }
                 }
               }
               if (file == null) {  
               for(Dom.Xmlnode node : child.getChildElements())
               {
                  string str = node.getName();                 
                  file = new Files__c();                     
                 if(str == 'ApplicationId')
                 {    file.AP_Application__c = node.gettext();     }
                 else if(str == 'APID')
                 {  file.Contact__c = node.gettext();    }
                 else if(str == 'Path')
                 {   file.Path__c = node.gettext();   }
                 else if(str == 'Inactive_Dt')
                {
                 if(str != ' ')
                   {
                    String[] stringDate = node.gettext().split('/');                  
                     Integer m =  Integer.valueOf(stringDate[0]);
                     Integer d = Integer.valueOf(stringDate[1]);
                     Integer y = Integer.valueOf(stringDate[2]);  
                    file.Inactive_Date__c = date.newinstance(y,m,d);
                   }
                 }
               }
           }      
          upsertfiles.add(file);   
          file =null;                    
        }         
        try {   upsert upsertfiles External_ID__c;      }
        
          catch (DmlException e) {
                System.debug(e.getMessage());
           }    
     }
}

Ispita_NavatarIspita_Navatar

Hi,

I think you are not passing the external id with the upsert call. As you know we have to pass the external id with upsert call and it should be a unique value. So if one goes by the message generated you are not providing the "external id" , do provide it and check if the error is resolved or not.

 

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

Brad007Brad007

 

Hi, Ispita thank you for responding to my post. I fyou look in the code i am do passing the External id to upsert command try { upsert upsertfiles External_ID__c; }