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
Stephanie Boggs 17Stephanie Boggs 17 

Tweak Trigger for my Instance

Hello!

My goal is to require 2 fields prior to a user rejecting an approval process. A validation rule is not working, and other resources are stating to use a trigger. I am purely declarative, so my attempting to tweak the trigger base that I found is not perfect. Can someone assist in correcting it? Please feel free to explain what's being updated and why as I have no issue with learning, I just don't know where all the characters and special nuances go. 

Object = MQL_SQL__c
Required Fields = Reject_Reason__c & Reject_Reason_Notes__c
Field that tracks the approval status = SAL_Approval_Status__c

Let me know if anything else is needed! Thank you!

Provided base code:
for(Object cr : Trigger.New)
    {
        String oldStatus=Trigger.oldMap.get(cr.Id).Status__c;
        if(cr.Status__c==CR_STATUS_APPROVED && oldStatus!=CR_STATUS_APPROVED)
        {
            // Effective Date is required if approved

            if(cr.effective_date==null) 
            {

                isValid = false;
                cr.addError('You must supply an effective date in order to approve the object.);
            }
            
My attempt at updating:  
for(MQL_SQL__c cr : Trigger.Rejection)
    {
        String oldStatus=Trigger.oldMap.get(cr.Id).SAL_Approval_Status__c;
        if(cr.SAL_Approval_Status__c==CR_STATUS_APPROVED && oldSAL_Approval_Status__c!=CR_STATUS_APPROVED)
        {
            // Reject Reason and Reject Notes are required if rejected

            if(cr.Reject_Reason__c==null) &&
            if(cr.Reject_Reason_Notes__c==null) 
            {

                isValid = false;
                cr.addError('Reject Reason and Reject Notes must be completed prior to Rejecting the SQL.);
            }            
Best Answer chosen by Stephanie Boggs 17
Stephanie Boggs 17Stephanie Boggs 17
Final working trigger:


trigger RejectRequire on MQL_SQL__c (after update){
for(MQL_SQL__c cr : Trigger.New)
{
String oldStatus=Trigger.oldMap.get(cr.Id).SAL_Approval_Status__c;
if(cr.SAL_Approval_Status__c=='Rejected' && oldStatus!='Rejected')
{
// Reject Reason and Reject Notes are required if rejected

if(cr.Reject_Reason__c==null || cr.Reject_Reason_Notes__c==null || cr.SRL_Next_Steps__c==null) 
{

cr.addError('All fields in the SRL Details section must be completed prior to Rejecting the SQL.');
}
}
}}


My best attempt at a breakdown explanation for those not familiar with code (like me):
  • RejectRequire = name of trigger
  • MQL_SQL__c = object name
  • (after update) = the error will fire after the user attempts to update
  • String oldStatus=Trigger.oldMap.get(cr.Id).SAL_Approval_Status__c;
    if(cr.SAL_Approval_Status__c=='Rejected' && oldStatus!='Rejected') = not entirely sure how to say this in laymens terms, but if SAL Approval Status is Rejected
  • if(cr.Reject_Reason__c==null || cr.Reject_Reason_Notes__c==null || cr.SRL_Next_Steps__c==null) = If Reject Reason OR Reject Reason Notes OR SRL Next Steps fields are blank then fire the error
  • cr.addError('All fields in the SRL Details section must be completed prior to Rejecting the SQL.') = the pop up error will read "All fields in the SRL Details section must be completed prior to Rejecting the SQL."

Heads up that you will still need a test code for coverage. I'm still working through that.

All Answers

Prakhar Saxena 19Prakhar Saxena 19
Hi Stephanie,

You can try the following code:
for(MQL_SQL__c cr : Trigger.New)
    {
        String oldStatus=Trigger.oldMap.get(cr.Id).SAL_Approval_Status__c;
        if(cr.SAL_Approval_Status__c==CR_STATUS_APPROVED && oldSAL_Approval_Status__c!=CR_STATUS_APPROVED)
        {
            // Reject Reason and Reject Notes are required if rejected

            if(cr.Reject_Reason__c==null && cr.Reject_Reason_Notes__c==null) 
            {

                isValid = false;
                cr.addError('Reject Reason and Reject Notes must be completed prior to Rejecting the SQL.);
            }
        }
    }

1) Changed Trigger.Rejection to Trigger.New. (Only Trigger Context Variables can be used with Trigger) 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm

2) Included two if conditions inside one if by using && (You don't need to write if twice)

Let me know if there are any issues.

Thanks.

 
Stephanie Boggs 17Stephanie Boggs 17
Thank you!!

I got a handful of different errors with your tweaks. I spoke with a developer friend and ended up with the following, but am stuck at error "Error: Compile Error: Variable does not exist: oldSAL_Approval_Status__c at line 5 column 45":

trigger RejectRequire on MQL_SQL__c (after update){
for(MQL_SQL__c cr : Trigger.New)
{
String oldStatus=Trigger.oldMap.get(cr.Id).SAL_Approval_Status__c;
if(cr.SAL_Approval_Status__c=='Rejected' && oldSAL_Approval_Status__c!='Rejected')
{
// Reject Reason and Reject Notes are required if rejected

if(cr.Reject_Reason__c==null && cr.Reject_Reason_Notes__c==null) 
{

isValid = false;
cr.addError('Reject Reason and Reject Notes must be completed prior to Rejecting the SQL.');
}
}
}}

I am assuming "oldSAL_Approval_Status__c!='Rejected'" is meant to be a field and it's value. The base code had "oldStatus!=CR_STATUS_APPROVED" but I'm not truly understanding what this part is supposed to be doing/saying.
Stephanie Boggs 17Stephanie Boggs 17
Final working trigger:


trigger RejectRequire on MQL_SQL__c (after update){
for(MQL_SQL__c cr : Trigger.New)
{
String oldStatus=Trigger.oldMap.get(cr.Id).SAL_Approval_Status__c;
if(cr.SAL_Approval_Status__c=='Rejected' && oldStatus!='Rejected')
{
// Reject Reason and Reject Notes are required if rejected

if(cr.Reject_Reason__c==null || cr.Reject_Reason_Notes__c==null || cr.SRL_Next_Steps__c==null) 
{

cr.addError('All fields in the SRL Details section must be completed prior to Rejecting the SQL.');
}
}
}}


My best attempt at a breakdown explanation for those not familiar with code (like me):
  • RejectRequire = name of trigger
  • MQL_SQL__c = object name
  • (after update) = the error will fire after the user attempts to update
  • String oldStatus=Trigger.oldMap.get(cr.Id).SAL_Approval_Status__c;
    if(cr.SAL_Approval_Status__c=='Rejected' && oldStatus!='Rejected') = not entirely sure how to say this in laymens terms, but if SAL Approval Status is Rejected
  • if(cr.Reject_Reason__c==null || cr.Reject_Reason_Notes__c==null || cr.SRL_Next_Steps__c==null) = If Reject Reason OR Reject Reason Notes OR SRL Next Steps fields are blank then fire the error
  • cr.addError('All fields in the SRL Details section must be completed prior to Rejecting the SQL.') = the pop up error will read "All fields in the SRL Details section must be completed prior to Rejecting the SQL."

Heads up that you will still need a test code for coverage. I'm still working through that.
This was selected as the best answer