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
sudhirn@merunetworks.comsudhirn@merunetworks.com 

Automatically fire approval process when record is created

Hi, 

  I have a custom object called expense when records is created I need to automatically call the process that is created on expense please suggest me how to create this process. 

  Can we call approvall process using trigger Please suggest 
 

Thanks
Sudhir
Best Answer chosen by sudhirn@merunetworks.com
Abhishek BansalAbhishek Bansal
Hi Sudhir,

If you want that your approval process will be automatically called only when your record is created than you must use only insert action in your trigger.
I have updated your trigger code, please use the below code :
trigger updateforgetexpense on Expenses__c (after insert) {
	for(Exxpenses__c expense : trigger.new){
		if(expense.Expense_Status__c == 'Draft'){
			// create the new approval request to submit
            Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
            req.setComments('Submitted for approval. Please approve.');
            req.setObjectId(expense.Id);
            // submit the approval request for processing
            Approval.ProcessResult result = Approval.process(req);
            // display if the reqeust was successful
            System.debug('Submitted for approval successfully: '+result.isSuccess());
		}
	}
}
Remember that this trigger will only fire when a record is created. It will not fire on update.
If you want approval process to be fired on Update also than please let us know so that we can make the neccessary changes.

Thanks,
Abhishek

All Answers

KaranrajKaranraj
Using Lightning process builder you can able to call the approval process whenever new created or update without writing any code. Check this link to know more about the Lightning Process builder in details - https://developer.salesforce.com/trailhead/business_process_automation/process_builder
Vijay NagarathinamVijay Nagarathinam
Hi,

You can acheive this in two ways.

Using process builder to call the approval process.
Then you can create a apex trigger, if the condition met then approval process will be fired automatically.

http://www.jdope.com/blog/lock-a-record-using-process-builder-and-a-dummy-approval-process/

Thanks,
Vijay
Chandra Sekhar CH N VChandra Sekhar CH N V
In case you have your criterias in place you can go with Karan's suggestion of using process builder.
sudhirn@merunetworks.comsudhirn@merunetworks.com
Thanks for you reply 

I am using below code in trigger to fire approval process its working as expected problem is same process is firing multiple time please suggest me how to make it fire only once
trigger updateforgetexpense on Expenses__c (After Insert,After Update ) {
   

  for (Integer i = 0; i < Trigger.new.size(); i++) {

   if ( Trigger.old[i].Expense_Status__c == 'Draft') 
    {

            // create the new approval request to submit
            Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
            req.setComments('Submitted for approval. Please approve.');
            req.setObjectId(Trigger.new[i].Id);
            // submit the approval request for processing
            Approval.ProcessResult result = Approval.process(req);
            // display if the reqeust was successful
            System.debug('Submitted for approval successfully: '+result.isSuccess());
   }
     
    }


}

Thanks
Sudhir
Abhishek BansalAbhishek Bansal
Hi Sudhir,

If you want that your approval process will be automatically called only when your record is created than you must use only insert action in your trigger.
I have updated your trigger code, please use the below code :
trigger updateforgetexpense on Expenses__c (after insert) {
	for(Exxpenses__c expense : trigger.new){
		if(expense.Expense_Status__c == 'Draft'){
			// create the new approval request to submit
            Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
            req.setComments('Submitted for approval. Please approve.');
            req.setObjectId(expense.Id);
            // submit the approval request for processing
            Approval.ProcessResult result = Approval.process(req);
            // display if the reqeust was successful
            System.debug('Submitted for approval successfully: '+result.isSuccess());
		}
	}
}
Remember that this trigger will only fire when a record is created. It will not fire on update.
If you want approval process to be fired on Update also than please let us know so that we can make the neccessary changes.

Thanks,
Abhishek
This was selected as the best answer