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
DaNae Graham 1DaNae Graham 1 

Trigger Help - Closed Won Opportunity Creates a Record and Updates Original Opportunity Record

I am trying to write a trigger that will create an implementation record (custom child object in a master-detail relationship) once an opportunity has Closed Won.  Once the implementation record has been created, I need to update the same opportunity that closed won with the implementation record ID so I can use that with an email alert that fires off the opportunity and I am running in to a bunch of errors.  I first tried this with process builders and workflows but since I cannot specify the order of operations, the system is getting confused.  I am trying the trigger now and gets errors when I test.  Please help!

Here is the order that I need everything done in:

1. Opp Closed Won
2. Create Implementation Record (Master-Detail Child of Opp)
3. Update Opportunity with Implementation Record ID (from Step 2)
4. Send Email Alert with link to implementation record (from 2 and 3)

Here is the code I have written:

trigger implementationTrigger on Opportunity (after insert, after update) { 
    
    for(Opportunity opp : [SELECT id, accountId, Gant_Sales_Rep_lookup__c,  Imp_in_a_Box__c, Line_of_Business__c, Sales_Call_Notes__c 
                           FROM Opportunity 
                           WHERE StageName = 'Closed Won' AND RecordTypeID = '01230000001hgdG'] ) {                      
                               
       Implementation__c newImp = new Implementation__c();
          newImp.Gant_Account_Manager__c  = opp.Gant_Sales_Rep_lookup__c;
          newImp.imp_in_a_box__c          = opp.imp_in_a_box__c;
          newImp.Implementation_Status__c = 'Pre-Implementation';
          newImp.Line_of_Business__c      = opp.Line_of_Business__c;
          newImp.Name                     = '1';
          newImp.Sales_Call_Notes__c      = opp.Sales_Call_Notes__c;
          newImp.Opportunity__c           = opp.id;   
          newImp.Account__c               = opp.accountId;     
            
       insert newImp;
       ID newImpId = newImp.Id;
                               
    opp.Implementation_ID__c = newImp.Id;
    update opp;

    }


}

And here is the error I get when I try to save a closed won opportunity:

Error:Apex trigger implementationTrigger caused an unexpected exception, contact your administrator: implementationTrigger: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, implementationTrigger: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, implementationTrigger: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, implementationTrigger: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, implementationTrigger: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, implementationTrigger: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, implementationTrigger: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, implementationTrigger: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, implementationTrigger: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, implementationTrigger: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, implementationTrigger: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, implementationTrigger: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, implementationTrigger: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, implementationTrigger: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, implementationTrigger: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, implementationTrigger: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_EXECUTE_FLOW_TRIGGER, The record couldn’t be saved because it failed to trigger a flow. A flow trigger failed to execute the flow with version ID 3013A000000Cx4N. Flow error messages: An unhandled fault has occurred in this flow
An unhandled fault has occurred while processing the flow. Please contact your system administrator for more information. Contact your administrator for help.: [] Trigger.implementationTrigger: line 17, column 1: [] Trigger.implementationTrigger: line 17, column 1: [] Trigger.implementationTrigger: line 17, column 1: [] Trigger.implementationTrigger: line 17, column 1: [] Trigger.implementationTrigger: line 17, column 1: [] Trigger.implementationTrigger: line 17, column 1: [] Trigger.implementationTrigger: line 17, column 1: [] Trigger.implementationTrigger: line 17, column 1: [] Trigger.implementationTrigger: line 17, column 1: [] Trigger.implementationTrigger: line 17, column 1: [] Trigger.implementationTrigger: line 17, column 1: [] Trigger.implementationTrigger: line 17, column 1: [] Trigger.implementationTrigger: line 17, column 1: [] Trigger.implementationTrigger: line 17, column 1: [] Trigger.implementationTrigger: line 17, column 1: []: Trigger.implementationTrigger: line 17, column 1
v varaprasadv varaprasad
Hi Danae,

Please try once below :

 
trigger implementationTrigger on Opportunity (after insert, after update) { 
  
    list<Opportunity> updOppsWithImpl = new list<Opportunity>();
	
    for(Opportunity opp : [SELECT id, accountId, Gant_Sales_Rep_lookup__c,  Imp_in_a_Box__c, Line_of_Business__c, Sales_Call_Notes__c 
                           FROM Opportunity 
                           WHERE StageName = 'Closed Won' AND RecordTypeID = '01230000001hgdG' AND where Implementation_ID__c != null] ) {                      
                               
       Implementation__c newImp = new Implementation__c();
          newImp.Gant_Account_Manager__c  = opp.Gant_Sales_Rep_lookup__c;
          newImp.imp_in_a_box__c          = opp.imp_in_a_box__c;
          newImp.Implementation_Status__c = 'Pre-Implementation';
          newImp.Line_of_Business__c      = opp.Line_of_Business__c;
          newImp.Name                     = '1';
          newImp.Sales_Call_Notes__c      = opp.Sales_Call_Notes__c;
          newImp.Opportunity__c           = opp.id;   
          newImp.Account__c               = opp.accountId;     
            
       insert newImp;
       ID newImpId = newImp.Id;
                               
    opp.Implementation_ID__c = newImp.Id;
    updOppsWithImpl.add(opp);

    }
	update updOppsWithImpl;


}

Please check once and let me know in case of any other assistance.

Thanks
Varaprasad



 
v varaprasadv varaprasad
To avoid recursion Please use boolean static variables.

More Info :

http://salesforceprasad.blogspot.sg/2017/06/avoid-recursive-trigger-calls.html

Thanks
Varaprasad
Lokesh KumarLokesh Kumar
Why bot create a lookup on implementation record and on new record insert send an email.  And it can be done with no code.