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
Karthikeyan KannanKarthikeyan Kannan 

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

I am Updating Some fields in a SiteContracts__c Object from Engineer_Checklist__c object , while performing this Operation getting the Above mentioned Error, Can any one suggest me where should I Specify the ID and How ?

Here is my Code:

public List<id> sitecontractids = new List<id>();
public List<Engineer_Checklist__c> ssnewList = new List<Engineer_Checklist__c>();
public List<SiteContracts__c> scnewlist = new List<SiteContracts__c>();
public List<SiteContracts__c> scList =[SELECT id, name From SiteContracts__c LIMIT 200];

for(SiteContracts__c scnewid :scList ){
         sitecontractids.add(scnewid.id);
   }

  ssnewList =[Select Id, X6L_Water_Qty__c, X9L_Water_Qty__c, X3L_Water_Mist_Qty__c, X6L_Water_Mist_Qty__c, X9L_Water_Mist_Qty__c
                                      From Engineer_Checklist__c
                                                  Where Fire_Extinguisher_Service_complete__c=true AND Site_Contract__c !=NULL AND
                                                                   Site_Contract__c IN :sitecontractids  Order By lastmodifieddate desc LIMIT 200];

for(Engineer_Checklist__c ssli:ssnewList)
{
    SiteContracts__c newsc = new SiteContracts__c();

newsc.X6L_Water__c=ssli.X6L_Water_Qty__c;
newsc.X9L_Water__c=ssli.X9L_Water_Qty__c;
newsc.X3L_Water_Mist__c=ssli.X3L_Water_Mist_Qty__c;
newsc.X6L_Water_Mist__c=ssli.X6L_Water_Mist_Qty__c;
newsc.X9L_Water_Mist__c=ssli.X9L_Water_Mist_Qty__c;

scnewlist.add(newsc);
}
update scnewlist;
Best Answer chosen by Karthikeyan Kannan
@anilbathula@@anilbathula@
Hi Karthikeyan Kannan,

You need to pass id for which record you want to update.

please use the below part of code changes in your code.

for(Engineer_Checklist__c ssli:ssnewList)
{
SiteContracts__c newsc = new SiteContracts__c();
newsc.id=ssli.Site_Contract__c; //i hope this is the relation ship field between both the objects
newsc.X6L_Water__c=ssli.X6L_Water_Qty__c;
newsc.X9L_Water__c=ssli.X9L_Water_Qty__c;
newsc.X3L_Water_Mist__c=ssli.X3L_Water_Mist_Qty__c;
newsc.X6L_Water_Mist__c=ssli.X6L_Water_Mist_Qty__c;
newsc.X9L_Water_Mist__c=ssli.X9L_Water_Mist_Qty__c;

scnewlist.add(newsc);
}
update scnewlist;

Thanks
Anil.B

All Answers

Anoop yadavAnoop yadav
Hi,

Use insert scnewlist;
instead of update scnewlist;
@anilbathula@@anilbathula@
Hi Karthikeyan Kannan,

You need to pass id for which record you want to update.

please use the below part of code changes in your code.

for(Engineer_Checklist__c ssli:ssnewList)
{
SiteContracts__c newsc = new SiteContracts__c();
newsc.id=ssli.Site_Contract__c; //i hope this is the relation ship field between both the objects
newsc.X6L_Water__c=ssli.X6L_Water_Qty__c;
newsc.X9L_Water__c=ssli.X9L_Water_Qty__c;
newsc.X3L_Water_Mist__c=ssli.X3L_Water_Mist_Qty__c;
newsc.X6L_Water_Mist__c=ssli.X6L_Water_Mist_Qty__c;
newsc.X9L_Water_Mist__c=ssli.X9L_Water_Mist_Qty__c;

scnewlist.add(newsc);
}
update scnewlist;

Thanks
Anil.B
This was selected as the best answer
Karthikeyan KannanKarthikeyan Kannan
Hi ,
    I need to update existing records and I don want to insert a new record into the Object.
@anilbathula@@anilbathula@
Hi Karthikeyan,

Do the changes in your code with the code i have given it will work for updating the existing records.

Thanks
Anil.B
Mani RenusMani Renus
+1 you can get with anil code

and specify Site_Contract__c field in your query
Karthikeyan KannanKarthikeyan Kannan
Hi Anil.B,
                             Thank you, that works fine for me and can u suggest me how to use set to avoid Duplicate Id's, I am little confused about set.
chris jordan 66chris jordan 66
Make the necessary adjustments to your code, and the code I've provided will work to update existing records.
@anilbathula@ (https://waterfilterinsight.com/best-faucet-water-filter-reviews/)
Thank you, that works good for me. Could you maybe explain how to use set to eliminate duplicate IDs? I'm not sure what set is.