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
Adam RycroftAdam Rycroft 

Help Testing Trigger for After Insert

Hi,

I need help writing a test for the following trigger which submits a record for approval if the created_from_onboarding__c checkbox field is marked true.
trigger autoapprovenewhire on Change_of_statusnew__c (after insert) {
    for(Change_of_statusnew__c changeofstatus : trigger.new){
        if(changeofstatus.created_from_onboarding__c == TRUE){
            // create the new approval request to submit
            Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
            req.setComments('Submitted for approval. Please approve.');
            req.setObjectId(changeofstatus.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());
        }
    }
}
Best Answer chosen by Adam Rycroft
Sainath VenkatSainath Venkat
@Adam Rycroft,

Why you went with a  trigger for submiting a record for approval process, you can do it simply with a process builder.

in process builder, in object select "Change_of_statusnew__c " object and in criteria select "created_from_onboarding__c = true" then in immediate actions select submit for approval then select the desired approval process.

User-added image

Hope it helps, marks best if it really helps you.
 

All Answers

Sainath VenkatSainath Venkat
@Adam Rycroft,

Why you went with a  trigger for submiting a record for approval process, you can do it simply with a process builder.

in process builder, in object select "Change_of_statusnew__c " object and in criteria select "created_from_onboarding__c = true" then in immediate actions select submit for approval then select the desired approval process.

User-added image

Hope it helps, marks best if it really helps you.
 
This was selected as the best answer
Adam RycroftAdam Rycroft
Thank you - that's a much easier solution!