You need to sign in to do that
Don't have an account?
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.);
}
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.);
}
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):
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
Heads up that you will still need a test code for coverage. I'm still working through that.
All Answers
You can try the following code:
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.
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.
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):
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
Heads up that you will still need a test code for coverage. I'm still working through that.