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 

Id not specified in an update cal

I tested my class in the sandbox environment and my trigger reported 94%.  But when I tried to move it to production, am geting an error.

StatusID.insertID(), Details: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: [] Class.StatusID.insertID: line 28, column 1 IandUStatusReport, Details: Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required

The insert works but the Update does not.  I thought the ID would be carried over to the update.

Class:  

@isTest
public class StatusID {
  static testmethod void insertID() {
  Date myDate = Date.today();
  // List<Customer_Fact_Sheet__c> listSTA = new List<Customer_Fact_Sheet__c>();
  // List <Customer_Fact_Sheet__c> cid = [Select Name, Project_c__c from Customer_Fact_Sheet__c   
  //where Name = 'A'];
  //insert data for insert and update status report
    Customer_Fact_Sheet__c cfs = new Customer_Fact_Sheet__c (Name = 'A',Project_c__c = 'a0kd000000ApbI3AA5',Criteria_Defect__c = 'No Defects Know'
       );
//    cfs.Name = 'A';
//    cfs.Project_c__c = 'a0kJ0000001tENQIA2';
//    cfs.Report_Date__c = myDate;
//    cfs.Criteria_Defect__c = 'No Defects Know';
//    cfs.Critical_Enhancements__c = 'Customer wants the Query Editor to run through all tables';
//    cfs.Next_Action__c = 'Testing is starting next week';
//    cfs.Current_Issues_or_Impedients__c = 'No current issues reported at this time';
//    cfs.Current_Services_Personnel_Involved__c = 'John Doe is on the project 75% of the time';
    
//     listSTA.add(cfs);
  try{
     insert (cfs);
  }
 catch (Exception e) {
      system.debug('Exception'+e);}
   //Update data  
     cfs.Criteria_Defect__c = 'N/A';
    update (cfs);     
   }
 }

Trigger:

trigger IandUStatusReport on Customer_Fact_Sheet__c   (after insert, after update)
{   
    List<Status_Report__c> listSTA = new List<Status_Report__c>();
    List<Status_Report__c> updsta = new List<Status_Report__c>();
    MAP <String, Customer_Fact_Sheet__c> objMap = new Map<String, Customer_Fact_Sheet__c>();
     for (Customer_Fact_Sheet__c cfs : Trigger.new){ 
       List<Status_Report__c > dupes =[ Select Status_Unique_ID__c, Name from  Status_Report__c 
       where Status_Unique_ID__c = :cfs.Status_Unique_ID__c LIMIT 1];
           if (dupes.size() == 0) { 
            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);
   }
         
   }
       
     
       
       for(Status_Report__c sr: dupes)
       {
          Customer_Fact_Sheet__c cfs1 = objMap.get(sr.Status_Unique_ID__c);
          // Update new Status record
            system.debug(sr.Status_Unique_ID__c);
            sr.Status_Unique_ID__c = cfs.Status_Unique_ID__c;
            sr.Report_Date__c  = cfs.Report_Date__c;
            sr.Critical_Defects__c  = cfs.Criteria_Defect__c;
            sr.Critical_Enhancements__c  = cfs.Critical_Enhancements__c;
            sr.Next_Action__c  = cfs.Next_Action__c;
            sr.Current_Issues_or_Impediments__c  = cfs.Current_Issues_or_Impedients__c;
            sr.Services_Personnel_Involved__c  = cfs.Current_Services_Personnel_Involved__c;

          updsta.add(sr);}  
  if(updsta.size() > 0){ 
  try{
     update updsta;
  }
 catch (Exception e) {
      system.debug('Exception'+e);} }}

}
 
Best Answer chosen by Chuck Harvey
MithunPMithunP
Hi Chuck Harvey,

Generally Record ID's are not SAME in sandbox and production, but you are hard coded Project id (a0kd000000ApbI3AA5) in your test class, it will work fine in sandbox but it won't work in production since Project id (a0kd000000ApbI3AA5) not available in production. 

So, normally we should create test data in test class itself, don't hard code any ID's in your code. 

For your test class, try to create Project_c__c record in your test class and then pass Project_c__c Id to Customer_Fact_Sheet__c object instead of hard coded Id. Here is the simillar format. 
 
@isTest
public class StatusID {
  static testmethod void insertID() {
  Date myDate = Date.today();
  // List<Customer_Fact_Sheet__c> listSTA = new List<Customer_Fact_Sheet__c>();
  // List <Customer_Fact_Sheet__c> cid = [Select Name, Project_c__c from Customer_Fact_Sheet__c   
  //where Name = 'A'];
  //insert data for insert and update status report
 Project_c__c  prj = new Project_c__c(name = 'test prj');
 insert prj;
  Customer_Fact_Sheet__c cfs = new Customer_Fact_Sheet__c (Name = 'A',Project_c__c = prj.id ,Criteria_Defect__c = 'No Defects Know');

    /////Customer_Fact_Sheet__c cfs = new Customer_Fact_Sheet__c (Name = 'A',Project_c__c = //////'a0kd000000ApbI3AA5',Criteria_Defect__c = 'No Defects Know');
//    cfs.Name = 'A';
//    cfs.Project_c__c = 'a0kJ0000001tENQIA2';
//    cfs.Report_Date__c = myDate;
//    cfs.Criteria_Defect__c = 'No Defects Know';
//    cfs.Critical_Enhancements__c = 'Customer wants the Query Editor to run through all tables';
//    cfs.Next_Action__c = 'Testing is starting next week';
//    cfs.Current_Issues_or_Impedients__c = 'No current issues reported at this time';
//    cfs.Current_Services_Personnel_Involved__c = 'John Doe is on the project 75% of the time';
    
//     listSTA.add(cfs);
  try{
     insert (cfs);
  }
 catch (Exception e) {
      system.debug('Exception'+e);}
   //Update data  
     cfs.Criteria_Defect__c = 'N/A';
    update (cfs);     
   }
 }
Best Regards,
Mithun.

All Answers

MithunPMithunP
Hi Chuck Harvey,

Generally Record ID's are not SAME in sandbox and production, but you are hard coded Project id (a0kd000000ApbI3AA5) in your test class, it will work fine in sandbox but it won't work in production since Project id (a0kd000000ApbI3AA5) not available in production. 

So, normally we should create test data in test class itself, don't hard code any ID's in your code. 

For your test class, try to create Project_c__c record in your test class and then pass Project_c__c Id to Customer_Fact_Sheet__c object instead of hard coded Id. Here is the simillar format. 
 
@isTest
public class StatusID {
  static testmethod void insertID() {
  Date myDate = Date.today();
  // List<Customer_Fact_Sheet__c> listSTA = new List<Customer_Fact_Sheet__c>();
  // List <Customer_Fact_Sheet__c> cid = [Select Name, Project_c__c from Customer_Fact_Sheet__c   
  //where Name = 'A'];
  //insert data for insert and update status report
 Project_c__c  prj = new Project_c__c(name = 'test prj');
 insert prj;
  Customer_Fact_Sheet__c cfs = new Customer_Fact_Sheet__c (Name = 'A',Project_c__c = prj.id ,Criteria_Defect__c = 'No Defects Know');

    /////Customer_Fact_Sheet__c cfs = new Customer_Fact_Sheet__c (Name = 'A',Project_c__c = //////'a0kd000000ApbI3AA5',Criteria_Defect__c = 'No Defects Know');
//    cfs.Name = 'A';
//    cfs.Project_c__c = 'a0kJ0000001tENQIA2';
//    cfs.Report_Date__c = myDate;
//    cfs.Criteria_Defect__c = 'No Defects Know';
//    cfs.Critical_Enhancements__c = 'Customer wants the Query Editor to run through all tables';
//    cfs.Next_Action__c = 'Testing is starting next week';
//    cfs.Current_Issues_or_Impedients__c = 'No current issues reported at this time';
//    cfs.Current_Services_Personnel_Involved__c = 'John Doe is on the project 75% of the time';
    
//     listSTA.add(cfs);
  try{
     insert (cfs);
  }
 catch (Exception e) {
      system.debug('Exception'+e);}
   //Update data  
     cfs.Criteria_Defect__c = 'N/A';
    update (cfs);     
   }
 }
Best Regards,
Mithun.
This was selected as the best answer
Chuck HarveyChuck Harvey
Thanks for the information.  The problem I was having is project is a required field but when I do New in projects, all the triggers seem to slow me down.  The only problem I am having now is I get the code coverage about 58%.