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
JvalleeJvallee 

Error in Opportunity Before Insert/Update trigger

I'm currently getting this error:

 

Severity and Description    Path    Resource    Location    Creation Time    Id
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OpportunityBeforeInsertBeforeUpdate: execution of BeforeInsert

caused by: System.StringException: Invalid id:

Trigger.OpportunityBeforeInsertBeforeUpdate: line 33, column 34        Production2    line 1    1241789768961    3500

 

 

I'm guessing that it is actually line 36 that is causing the error, but I'm not sure.  the code that is causing the problem starts on line 27 and end on 69.  In this code, we are trying to decide if anything has changed in fields Opp_Last_Result_Detail__c, Opp_Last_Result_Category__c, Notes_Update__c, which are custom.  And save this to a list in a helper class "WithinATrigger" to save for the Aftet Insert/Update trigger any ideas would be helpful.

 

 

 <CODE>

 

1  trigger OpportunityBeforeInsertBeforeUpdate on Opportunity (before insert, before update) {
2   
3     Opportunity savedOpp = null;
4     //List of Ids that should be updated.
5     List<ID> oppsToSaveNotes = new List<ID>();
6    
7     //Loop  through all Opportunities being inserted or updated.  
8      for( Opportunity currentOpp : Trigger.new){
9         
10           //set the Default Stage if none is set.
11          if( currentOpp.StageName == null ){
12              
13             currentOpp.StageName = 'New';    
14           }
15          
16           //Handle upload scenario
17           if( currentOpp.upload__c == true){
18             
19              //Set the already updated flag and clear upload
20              currentOpp.upload__c = false;
21             
22              //Set the value to show we started a trigger already
23            WithinATrigger.setAlreadyUpdatedOpportunity( true );
24                 
25           }
26          
27           if( currentOpp.upload__c == null ||
28               currentOpp.upload__c != true){
29             
30              //Save list of Opp Ids which the LR Category or LR Detail have changed.
31              //Get Saved version of
32              if( currentOpp != null &&
33                  currentOpp.Id != null ){
34                                                                 
35               //Get savedOpp the version current in the DB
36               savedOpp = [SELECT Opp_Last_Result_Detail__c, Opp_Last_Result_Category__c,             Notes_Update__c FROM Opportunity WHERE id = :currentOpp.Id LIMIT 1];

37
38              
39               //if we have found a saved version, we can compare.

40               if( savedOpp != null ){
41                  
42                      //If the Category or Id have Changed, add Id to list.
43                      if( (currentOpp.Opp_Last_Result_Category__c != null &&
44                           currentOpp.Opp_Last_Result_Detail__c != null &&
45                           savedOpp.Opp_Last_Result_Category__c != null &&
46                           savedOpp.Opp_Last_Result_Detail__c != null) &&
47                           currentOpp.Opp_Last_Result_Category__c != savedOpp.Opp_Last_Result_Category__c ||
48                        currentOpp.Opp_Last_Result_Detail__c != savedOpp.Opp_Last_Result_Detail__c){
49                  
50                          //Add current opp's id to the list
51                          oppsToSaveNotes.add(savedOpp.Id);
52                          
53                   }
54                     
55               }
56               else{ //Didn't find one, but we should write anyway.
57                  
58                  //Add current opp's id to the list
59                  if( currentOpp.Id != null){
60                     oppsToSaveNotes.add(currentOpp.Id);
61                  }
62               }
63            }
64            
65           
66            //Save list to helper class
67            if( !oppsToSaveNotes.isEmpty() ){
68               WithinATrigger.setIdsToUpdateNotes(oppsToSaveNotes);
69            }
70           }
71         
72      }
73    
74 }

 

</CODE>

 

 

 

 

kerwintangkerwintang

I haven't encountered this, but the error says "Invalid id:". Maybe it means that the currentOpp.Id value is an empty string "". Can you try to add that condition in line 33 ("currentOpp.Id !=null && currentOpp.Id !="")?

 

Thanks and Regards,
Kerwin

JvalleeJvallee

Hi Kerwin,

 

Thanks for your response.  I think that you on the right track, but just so yo know, comparing an Id to "" (empty string) can through an exception. I'm still lokking in this area, I do believe that when moving this around, I move some of the test out of order.

 

Thanks again,

 

Jason