0

Please help me with this code, Writing a Trigger to display an error message on Opportunity_Support_Request__c record which has no OSR Product and if it is submitted for Approval(by clicking on Submit for Approval button) it will display the error message(You must add at least one osr Product to your Request).

Opportunity_Support_Request__c is Master object OSR_Product__c is Detail obj OSR_ID__c is the master detail field created on detail object. And the Trigger that I have written is :

trigger OSRapprovalTrigger on Opportunity_Support_Request__c (before insert,before update)
{        
    if(trigger.isUpdate && trigger.isBefore)
    {        
        for(Opportunity_Support_Request__c s : Trigger.new){                        
            if(s.Status__c == 'Not Submitted')
            {
                //Get list of osr products
                List <OSR_Product__c> SampleProductList = [Select CAT_logic_Description__c,Material_Description__c from OSR_Product__c where OSR_ID__c =: s.id ];//
                if(SampleProductList.size() == 0)
                {
                    if(!Test.isRunningTest()){
                        s.addError('You must add at least one osr Product to your Request');
                    }
                }
            }
        }
    }
}

                                                                                            

Please Help. Thanks in advance !

6
  • What is the issue, did you put debug logs to check what is the size of SampleProductList? And please remove SOQL from for loop. Dec 4, 2020 at 10:01
  • Did you check that the Customize Error Messages for Invalided Approval Process is still an idea? It's not clear if your approval process just sets the Status to 'Not Submitted'.
    – Chun
    Dec 4, 2020 at 11:19
  • @NagendraSingh Hello, thanks for your reply. Anyhow I wrote this trigger as I am the beginner. Not able to save the trigger. Haven't checked the logs. Can you please share the code that fulfil my requirement ?
    – Deep
    Dec 4, 2020 at 12:04
  • This is not how this forum works. We are here for providing code. If you face any issues while developing then please paste your question so that we can take a look. Dec 4, 2020 at 12:13
  • @NagendraSingh , Apologies as I am new here...!! -- Here is the error message I am facing. " Select Quantity__c from OSR_Product__c ^ ERROR at Row:1:Column:8 No such column 'Quantity__c' on entity 'OSR_Product__c'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.". Though OSR product is the child of Opportunity Support Request Obj, but I think still it is not able to detect it.
    – Deep
    Dec 4, 2020 at 12:22

1 Answer 1

0

Just to give a sample and one of the ways to implement this:

I created an approval process, just added an action to update any field. In your case it will be your specific field.

DummyApprovalprocess

After that just wrote a trigger on Opportunity beforeUpdate like this. You can add your conditions.

trigger OpportunityTrigger on Opportunity (before update) {

    if(trigger.isUpdate && trigger.isBefore) {        
        for(Opportunity s : Trigger.new){                        
            s.addError('You must add at least one osr Product to your Request');
        }
    }

}

And when submitting for approval:

enter image description here

got this message:

enter image description here

Now you will have to fix your code for compilation errors and all, but this was just to show that this flow does work and allows us to put a validation in approval process.

1
  • a useful field to update in the Approval Process would be a checkbox called Is_Ready_For_Approval_Submission_Validation__c that if validation checks pass, is switched off within the before update
    – cropredy
    Dec 5, 2020 at 5:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .