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
Chuck HarveyChuck Harvey 

Trigger: Insert Works but Update statement giving an exception on a Custom Object

I am trying to create a trigger that will take information from one customer object (Customer_Fact_Sheet__c) and insert or update into another object (Status_Report__c).   The insert part of the trigger is working properly but the Update statement is giving me an exception.  

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

I have checked to make sure there is a record is in the Status_Report__c table to update and there is only one record in Status_Report__c object with the same Status_Report_ID.



trigger InsandUpdStatusReport on Customer_Fact_Sheet__c   (after insert, after update)

    list<Status_Report__c> listSTA = new List<Status_Report__c>();
     for (Customer_Fact_Sheet__c cfs : Trigger.new){ 
       List<Status_Report__c > dupes =[ Select Status_Unique_ID__c from  Status_Report__c 
       where Status_Unique_ID__c = :cfs.Status_Unique_ID__c];
           if (dupes.size() != null) { 
            Status_Report__c sta = new Status_Report__c();
          // Insert new Status record
            system.debug(cfs.Project_c__c);
            sta.Status_Unique_ID__c = cfs.Status_Unique_ID__c;
            sta.Project__c = cfs.Project_c__c;
            sta.Report_Date__c  = cfs.Report_Date__c;
            sta.Critical_Defects__c  = cfs.Criteria_Defect__c;
            sta.Critical_Enhancements__c  = cfs.Critical_Enhancements__c;
            sta.Next_Action__c  = cfs.Next_Action__c;
            sta.Current_Issues_or_Impediments__c  = cfs.Current_Issues_or_Impedients__c;
            sta.Services_Personnel_Involved__c  = cfs.Current_Services_Personnel_Involved__c;

           listSTA.add(sta);
  try{
     insert listSTA;
  }
 catch (Exception e) {
      system.debug('Exception'+e);
   }
         
   }
       else
       { Status_Report__c updsta = new Status_Report__c(Status_Unique_ID__c = dupes[0].Status_Unique_ID__c );
           
          // Update new Status record
            system.debug(dupes[0].Status_Unique_ID__c);
            updsta.Project__c = cfs.Project_c__c;
            updsta.Report_Date__c  = cfs.Report_Date__c;
            updsta.Critical_Defects__c  = cfs.Criteria_Defect__c;
            updsta.Critical_Enhancements__c  = cfs.Critical_Enhancements__c;
            updsta.Next_Action__c  = cfs.Next_Action__c;
            updsta.Current_Issues_or_Impediments__c  = cfs.Current_Issues_or_Impedients__c;
            updsta.Services_Personnel_Involved__c  = cfs.Current_Services_Personnel_Involved__c;

          listSTA.add(updsta);}
    }
try{
     update listSTA;
  }
 catch (Exception e) {
      system.debug('Exception'+e);
   }
}
Chuck HarveyChuck Harvey
Thank you for modifying the code.  I will test the code.
Chuck HarveyChuck Harvey
I tried the code and I was getting an error:

trigger InsandUpdStatusReport on Customer_Fact_Sheet__c   (after insert, after update){
    list<Status_Report__c> listSTA = new List<Status_Report__c>();
    List<String>Status_Unique_IDs=new List<String>();

//Assuming Status_Unique_ID__c as a String Type
    Map<String,Status_Report__c> StatusReportMap;
    Status_Report__c StatusReportRecord;
         for (Customer_Fact_Sheet__c cfs : Trigger.new){
                Status_Unique_IDs.add(cfs.Status_Unique_ID__c);
           }
            
           // Writing this loop to help bulkify the trigger so that the code does not hit governor limit
           For(Status_Report__c statusReport:[Select Name, Status_Unique_ID__c,
                                              Project__c, Report_Date__c,Critical_Defects__c, 
                                              Critical_Enhancements__c, Next_Action__c, 
                                              Current_Issues_or_Impediments__c, Services_Personnel_Involved__c 
                                              From  Status_Report__c where Status_Unique_ID__c IN:Status_Unique_IDs]){
                                         
          StatusReportMap.put(statusReport.Name);
           }
           // By end of this loop we will have all the status report records which has the unique id we are dealing with
            
            
            for (Customer_Fact_Sheet__c cfs : Trigger.new){
                 
                StatusReportRecord=new Status_Report__c(Status_Unique_ID__c =cfs.Status_Unique_ID__c,
                                                        Project__c = cfs.Project_c__c,
                                                        Report_Date__c =cfs.Report_Date__c,
                                                        Critical_Defects__c = cfs.Criteria_Defect__c,
                                                        Critical_Enhancements__c =cfs.Critical_Enhancements__c,
                                                        Next_Action__c = cfs.Next_Action__c,
                                                        Current_Issues_or_Impediments__c =cfs.Current_Issues_or_Impedients__c,
                                                        Services_Personnel_Involved__c =cfs.Current_Services_Personnel_Involved__c,
                                                        id=StatusReportMap.get(Status_Unique_ID__c).id);
                                                         
                if(StatusReportMap.keyset().contains(cfs.Status_Unique_ID__c)){ 
                
                // Checking if the record exist or not and assigning the id of record
                 
                StatusReportRecord.id=StatusReportMap.get(cfs.Status_Unique_ID__c).Id;
                
                }
                StatusReportMap.put(cfs.Status_Unique_ID__c,StatusReportRecord);
                 
                 
           }// By end of this loop we prepared all the status report records now its time to Upsert which inserts if id not found or updates if id found
           listSTA.addAll(StatusReportMap.values);
            
           try{
           Upsert listSTA;
           }catch(Exception e){
           //handle your errors here
           }
          }                               
            
           Line: id=StatusReportMap.get(Status_Unique_ID__c).id); Error:
Compile Error: Variable does not exist: Status_Unique_ID__c at line 34 column 80
Chuck HarveyChuck Harvey
Thanks for the update.  I tried to save the code and I received an error:

Line 20:  StatusReportMap.put(statusReport.Name);  

Error: Compile Error: Method does not exist or incorrect signature: [MAP<String,Status_Report__c>].put(String) at line 20 column 11

Do I need to make a class called StatusReportMap?
Chuck HarveyChuck Harvey
I do not want direction I need to go with my issue.  I have added a new Map:     Map<String,Status_Report__c> StatusReportMap = new Map <String,Status_Report__c>();
and added a add method:
List<Status_Report__c> statusReport=[Select Name, Status_Unique_ID__c,
                                              Project__c, Report_Date__c,Critical_Defects__c,
                                              Critical_Enhancements__c, Next_Action_
                                              Current_Issues_or_Impediments__c, Services_Personnel_Involved__c
                                              From  Status_Report__c where Status_Unique_ID__c IN:Status_Unique_IDs];
          statusReport.add(statusReport.Name);                              
          StatusReportMap.put(statusReport.Status_Unique_ID__c);
           


But nothing seems to work