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
jleehjleeh 

Apex Trigger Code Error - Approval Process auto-Submit

I'm trying to code a trigger to auto-Submit a record through the Approval Process on a Custom Object. I want to do this to make the record Read Only (I know there are others ways to do this, but I'm up for a challenge!). 

 

My Approval Process criteria is only: when "Locked__c" (checkbox) is "True". I also have it set to auto-reject by default and lock the record

 

The code I'm using is below, but I'm receiving error message: 
Error: Compile Error: Variable does not exist: Locked__c at line 3 column 12

 

I'm new to Apex coding, so any help is greatly appreciated! If any other information is needed, just let me know. 

 

trigger Record_Lock on Plan_and_Profile__c (after update, after insert) {
for (Plan_and_Profile__c a: Trigger.new) { 
    if (a.(Locked__c == 'True')) {
      // Create an approval request for the account
      Approval.ProcessSubmitRequest req1 =
      new Approval.ProcessSubmitRequest();
      req1.setComments('Automatic record lock.');
      req1.setObjectID(a.id);
      
      // Submit the approval request for the account
      Approval.ProcessResult result = Approval.process (req1);
    }
  }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
magicforce9magicforce9

Hi....Sorry my bad..I can see the Locked__c  field is of type boolean, so you don't need to put the comparision values in single quotes. you'll need to compare it like this..

if (a.Locked__c == True) {

or

if(a.Locked__c)

All Answers

magicforce9magicforce9

Hi..On Line # 3 change it to. The (.) dot notation has to be between object & field.

if (a.Locked__c == 'True') {

 

jleehjleeh

magicforce9, when I use that, I receive this error message : 

 

Error: Compile Error: Comparison arguments must be compatible types: Boolean, String at line 3 column 9

magicforce9magicforce9

Hi....Sorry my bad..I can see the Locked__c  field is of type boolean, so you don't need to put the comparision values in single quotes. you'll need to compare it like this..

if (a.Locked__c == True) {

or

if(a.Locked__c)
This was selected as the best answer
jleehjleeh
Works great. Thank you!