Skip to main content

To display an error message when a record is submitted for approval using apex trigger

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 Quantity__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 !

Deep
  • 9
  • 1
  • 3
  • 11