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
skiptomylou11skiptomylou11 

Fire trigger on record creation

Hi,

 

We have a trigger which submits an oportunity automatically for approval if the 'Stage' is changed to either 'Won' or 'Lost' from some other stage status.

This part of the trigger works fine.

 

What we would like to implement as well is that when an opportunity is entered with the Stage set to 'Lost' (or 'Won') directly the submission to approval should also be triggered. So on creation the trigger should check if Stage equals 'Won' or 'Lost'

 

I am not sure how to write the IF statement for this instance.

It would need to include something like the ISNEW( ) function which however is only available in formulas.

 

Ideally if possible both functionalities, the existing and the check on record entry would be dealt with in one trigger.

 

Any help is appreciated!

Many thanks...

 

 

Trigger OpportunitySubmitForOrderListApproval on Opportunity (after update) {
 
    for(Integer i = 0; i < Trigger.new.size(); i++) 
    
     {
  if((Trigger.new[i].StageName == 'Won' && Trigger.old[i].StageName <> 'Won') ||

     (Trigger.new[i].StageName == 'Lost' && Trigger.old[i].StageName <> 'Lost'))

        {
 
            Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
            req.setComments('Submitted for approval. Please approve.');
            req.setObjectId(Trigger.new[i].Id);
            Approval.ProcessResult result = Approval.process(req);
            System.assert(result.isSuccess());  
 
        }
       
    }
 }

 

Best Answer chosen by Admin (Salesforce Developers) 
SFAdmin5SFAdmin5

try this

 

Trigger OpportunitySubmitForOrderListApproval on Opportunity (after update,after insert) 
{
    if(trigger.isafter && trigger.isinsert)
    {
        for(Integer i = 0; i < Trigger.new.size(); i++)    
        
            if((Trigger.new[i].StageName == 'Won') ||(Trigger.new[i].StageName == 'Lost'))
            { 
                Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
                req.setComments('Submitted for approval. Please approve.');
                req.setObjectId(Trigger.new[i].Id);
                Approval.ProcessResult result = Approval.process(req);
                System.assert(result.isSuccess());  
            }      
        
    }

    if(trigger.isafter && trigger.isupdate)
    {
        for(Integer i = 0; i < Trigger.new.size(); i++)    
        
            if((Trigger.new[i].StageName == 'Won' && Trigger.old[i].StageName <> 'Won') ||
            (Trigger.new[i].StageName == 'Lost' && Trigger.old[i].StageName <> 'Lost'))
            { 
                Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
                req.setComments('Submitted for approval. Please approve.');
                req.setObjectId(Trigger.new[i].Id);
                Approval.ProcessResult result = Approval.process(req);
                System.assert(result.isSuccess());  
            }
       
        
    }


}

 

All Answers

SFAdmin5SFAdmin5

try this

 

Trigger OpportunitySubmitForOrderListApproval on Opportunity (after update,after insert) 
{
    if(trigger.isafter && trigger.isinsert)
    {
        for(Integer i = 0; i < Trigger.new.size(); i++)    
        
            if((Trigger.new[i].StageName == 'Won') ||(Trigger.new[i].StageName == 'Lost'))
            { 
                Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
                req.setComments('Submitted for approval. Please approve.');
                req.setObjectId(Trigger.new[i].Id);
                Approval.ProcessResult result = Approval.process(req);
                System.assert(result.isSuccess());  
            }      
        
    }

    if(trigger.isafter && trigger.isupdate)
    {
        for(Integer i = 0; i < Trigger.new.size(); i++)    
        
            if((Trigger.new[i].StageName == 'Won' && Trigger.old[i].StageName <> 'Won') ||
            (Trigger.new[i].StageName == 'Lost' && Trigger.old[i].StageName <> 'Lost'))
            { 
                Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
                req.setComments('Submitted for approval. Please approve.');
                req.setObjectId(Trigger.new[i].Id);
                Approval.ProcessResult result = Approval.process(req);
                System.assert(result.isSuccess());  
            }
       
        
    }


}

 

This was selected as the best answer
skiptomylou11skiptomylou11

Thanks Ross,

 

Seems to be working perfectly!

 

Best regards,

Patrick