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
Shawn ReichnerShawn Reichner 

Automating Case approval process when Case is created

Hello,

I am very new to working with apex triggers and classes, and I am hoping for some help with creating an automated solution to the following scenerio....

When a case is created with a specific record type, the approval process that has been created will automatically start without the user having to press the submit for approval button.  

Any ideas how I woudl do this on the standard case object and how to write the trigger and class itself???

Help me please, I woudl greatly appreciate it!

Thank you in advance,

Shawn
Arunkumar RArunkumar R
Hi Shawn, 

The below code will helpful for you...! 

trigger CaseApproval on Case (after insert, after update) {

    for (Case currentCase : Trigger.new) {

        if (currentCase.RecordType.Name = 'Your record type Name') 
        {
            Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
            req.setComments('Submitted for approval. Please approve.');
            req.setObjectId(currentCase.Id);
            Approval.ProcessResult result = Approval.process(req);
        }

    }

}

Shawn ReichnerShawn Reichner
Thank you for the code....I have implimented the code, and it saved without errors, however when the case is created, it is not automatically going into the approval process.  Is there something else I need to do other than create the trigger?

Thanks again,

Shawn
Arunkumar RArunkumar R
1. Please make sure your approval process should be active.

2. Insert case record with matching criteria and add this criteria in the above code for example i have checked  "RecordType.Name = 'Your record type Name' "  in IF Condition and if you want multiple condition add those criteria in if condition..


If your problem solved, mark this as a best answer.

Thanks...
Shawn ReichnerShawn Reichner
Yes the test case I created was the record type I specified in the code.   I did have to add another = sign to your code to stop it from erroring due to looking ofr a boolean field.  

Still the record does not go into the approval process.   I am doing this in sandbox if that makes any difference???

Thank you for your help and for helping to resolve this....

Shawn
Shawn ReichnerShawn Reichner
Made the changes you suggested in the code and I now recieve the following error when attempting to save....Error: Compile Error: line 5:12 no viable alternative at character '"' at line 5 column 12
Shawn ReichnerShawn Reichner
I have two different approval processes on the case object...do I have to alert the trigger of which one to use?
Arunkumar RArunkumar R
Can you post your code with all criteria, i will repost code soon.
Shawn ReichnerShawn Reichner
Sure....one moment
Shawn ReichnerShawn Reichner
Here is the code...

trigger CaseApproval on Case (after insert, after update) {

    for (Case currentCase : Trigger.new) {
   
        if (currentCase.RecordType.Name == 'SF_Dev_Requests')
        {
            Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
            req.setComments('Submitted for approval. Please approve.');
            req.setObjectId(currentCase.Id);
            Approval.ProcessResult result = Approval.process(req);
        }   
    }



I have two approval processes on the case object...one is titled - SF Dev Requests (One we shoudl work with on this example) and the other is titled Sym request approval process.   The approval processes have the criteria of the specific record types when these cases are created and submitted. 

Thank you again for your help!
Arunkumar RArunkumar R
Hi Shawn,

This code works fine no issue with this code.
trigger CaseApproval on Case (after insert, after update) {

    for (Case currentCase : Trigger.new) {
   
        if (currentCase.RecordType.Name == 'SF_Dev_Requests')
        {
            Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
            req.setComments('Submitted for approval. Please approve.');
            req.setObjectId(currentCase.Id);
            Approval.ProcessResult result = Approval.process(req);
        }   
    }
}

While copy and paste this into your environment, try to remove single quotation where ever available and type it. This might be problem..

Thanks...

Shawn ReichnerShawn Reichner
Sorry....I typed it exactly as you have it listed, and you are correct the code saves just fine, however, it does not initiate the approval process.  

What coudl I have done wrong, or what coudl be stopping this?  

Liek I said I have two approval processes for the case object, how does this trigger know which one to fire?

Shawn
Shawn ReichnerShawn Reichner
Another question is which apex class is this trigger going to utilize?
Shawn ReichnerShawn Reichner
Arunkumar,

Do you have any additional insight to why the trigger is not firing?  

I thank you for your continued support and help with achieving this goal!

Shawn
Arunkumar RArunkumar R
Shawn, 

1. Please make sure both trigger and approval process must be active.

2. Verify if the trigger or approval process meeting criteria to fire approval process.


Let me know stil face any issue...!

Thanks..
Shawn ReichnerShawn Reichner
Hello, Yes both are looking for the same criteria the record type being sf dev requests. Both are active as well. I am thinking we are not utilizing a class. Thoughts? Shawn