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
bikla78bikla78 

Approval Process- APEX

I have created a custom object called PTO. I have also created an approval process for it.  How can I create a  trigger so that it automically executed the approval process i have setup as soon as a new PTO record is created(inserted).  This would behave the same way as if I manually pressed the submit approval button.

 

trigger PTO_Trigger on PTO__c (after insert) 
{
for(PTO__c pto: [select id from pto__c])
{
Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
req1.setObjectId(pto.id);
}
}

 

 

 

Message Edited by bikla78 on 07-15-2009 11:49 PM
Best Answer chosen by Admin (Salesforce Developers) 
bikla78bikla78

Ok, here we go. I got it to work: Here it is, if anybody else wants to see the solution to bypass the submit approval button

 

trigger PTO_Trigger on PTO__c (after insert) 
{
for(PTO__c pto: Trigger.new)
{
PTO = [ select ID from PTO__c where id in :Trigger.new ];

Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
req1.setComments('Submitting request for approval.');
req1.setObjectId(pto.id);
Approval.ProcessResult result = Approval.process(req1);
System.assert(result.isSuccess());


}
}

 

All Answers

hisrinuhisrinu

You need to add the below line.

 

Approval.ProcessResult result = Approval.process(req1);

bikla78bikla78

I added the line but I am getting error below:

 

 

trigger PTO_Trigger on PTO__c (after insert) 
{
for(PTO__c pto: [select id from pto__c])
{
Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
req1.setObjectId(pto.id);
Approval.ProcessResult result = Approval.process(req1);

}
}

 

 

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger PTO_Trigger caused an unexpected exception, contact your administrator: PTO_Trigger: execution of AfterInsert caused by: System.DmlException: Process failed. First exception on row 0; first error: ALREADY_IN_PROCESS, Cannot submit object already in process.: []: Trigger.PTO_Trigger: line 7, column 36

Message Edited by bikla78 on 07-17-2009 03:34 PM
bikla78bikla78

Ok, here we go. I got it to work: Here it is, if anybody else wants to see the solution to bypass the submit approval button

 

trigger PTO_Trigger on PTO__c (after insert) 
{
for(PTO__c pto: Trigger.new)
{
PTO = [ select ID from PTO__c where id in :Trigger.new ];

Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
req1.setComments('Submitting request for approval.');
req1.setObjectId(pto.id);
Approval.ProcessResult result = Approval.process(req1);
System.assert(result.isSuccess());


}
}

 

This was selected as the best answer
brountrebrountre
Thanks!  That saved me a lot of time figuring it out.
pmozz01pmozz01

Thank you for posting this code.  I am using the lock part of the approval process to lock down closed opportunities so that no one can change them except the admin.  Is there also a way to trigger automatic approval following the automatic submittal?

 

Thanks!

ylandraylandra

This is perfect!  Thanks for sharing :smileyvery-happy:

Jerry HongJerry Hong

Great, time saving

sadasiva07sadasiva07

Hi ,

 

But same pice of code when i using After Update event not working and same error im getting.

Nirmal ChristopherNirmal Christopher

i dont think this is the right answer how the problem is solved if we add system.assert equals...its just a assert statement

Matt FieldMatt Field

It isn't a solution.  If you use after update, you still receive this error.  If you use after insert, it doesn't go thru the approval process (at least mine didn't).