I am new to coding. I need help to write an apex trigger to display an error message(You must add atleast 1 product on the record to submit it for approval) before submitting a record for approval.
Object A(api name - Opportunity_Support_Request__c) is the master object and Product(api name - OSR_Product__c) is the detail object and OSR_ID__c is the master detail field created on detail object.
Suppose there is an object A and on its related list, there is option to add Products(also an object). If I create a new record on object A and its status(field on Object A) is "Not Submitted" and has no product on it and if I click on the button(Submit for Approval), I should get an error message at the top(You must have atleast 1 product to submit it for approval). Seeing the error message, we will come to know why we are not able to submit the record for approval.
Trigger for this :
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');
}
}
}
}
}
}
Tried to explain the whole scenario. Thanks in Advance !