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 !

  • Hi and welcome to SFSE. Please edit your question to add more detail about the relationship between object A and Product. If the products are master detail children you can use a rollup summary field that counts the number of products and simply include a validation rule that detects the change in status and only allows it if the count is greater than one. – Phil W Dec 4 '20 at 8:02
  • When I create a new record automatically its status should be ''not submitted'' and at that point, if a create a validation rule on product count field to be created should be greater than 0 as per validation but there is no point of adding the products while we create a new record. Firstly, record is created with not submitted status value. Only after saving it, we can add products to it. So, validation rule won't help to fulfil the condition. – Deep Dec 4 '20 at 10:29
  • Note that I said a validation rule that "detects the change in status" - you would use ISCHANGED and PRIORVALUE as part of the validation. – Phil W Dec 4 '20 at 11:26

The trigger is not automatically invoked upon the record approval unless there is a field updated during the approval step.

So what you can do is in the approval step Initial Submission Actions trigger a field update, for eg: create a new field SubmittedForApproval and try to update that field to true in approval step. This will initiate a trigger action. In before insert check if the SubmittedForApproval is true and also check if there is any product attached to it.

enter image description here

Then just use .addError method to add an error message accordingly.

  • Thanks for your reply, but I dont understand what is the need of field update ? I mean, I have to check that atleast 1 product(on its related list) must be there before submitting it for approval. – Deep Dec 4 '20 at 9:01
  • Approval processes does not invoke trigger by itself, and I believe you needed to write an apex trigger that displays error. – Nagendra Singh Dec 4 '20 at 9:37
  • Can you please explain what datatype field I should create and then add to the initial submission action in approval steps ? If this much is done, then I will set the field to true in approval steps(this I understood). But how to do in trigger this field update thing. I just edited my post and added the trigger code. Can you please help ? – Deep Dec 4 '20 at 11:00
  1. Add a count-based rollup summary field to object A that counts the products associated with it, e.g. ProductCount__c.
  2. Assuming the Status__c changes to "Open" on submission, add a validation rule like:
ISCHANGED(Status__c) && ISPICKVAL(Status__c, 'Open') && ProductCount__c == 0

The violation message would be "You cannot submit for approval without products".

  • thanks for your reply. Though status is a picklist field, we cannot use "ISCHANGED". However I have used another syntax in validation rule -- ISPICKVAL(Status__c , 'In Process') && OSR_Product_Count__c == 0. but it is not working. Also, PRIOR and ISCHANGED are used for multi-select picklist fields. Can you help with the triggers ? – Deep Dec 4 '20 at 11:56
  • Why can't you use ISCHANGED? – Phil W Dec 4 '20 at 12:08
  • Because it cannot be used with picklist data type field. ISCHANGED can be used with multi-select picklist field . Tried and found error. Link -- help.salesforce.com/… – Deep Dec 4 '20 at 12:12
  • Strange. We have successfully used ISCHANGED with a picklist field (albeit one that uses a global value set). – Phil W Dec 4 '20 at 12:17
  • @Deep what error did you get? – Phil W Dec 4 '20 at 12:45

today is my first day on sharing doubts/question on this forum and I am glad to share that the trigger is working for me. Yes!! it is fulfilling my requirements.

At first, status is 'Not Submitted' but when we submit it for approval it gets changed to "Open". And when the status is changed to "Open", trigger worked.

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 == 'Open') { 
                //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) { 
                        s.addError('You must add at least one OSR Product to your Request.'); 

Thanks to everyone !! and SPECIAL THANKS TO @Nagendra Singh and @Phil for your time.

  • This trigger uses a query in a loop and will fail when processing more than 100 support request updates at a time. – Phil W Dec 4 '20 at 15:13
  • Additionally, this query is unnecessarily returning all the rows, instead of using a count query. You really would be better off using a rollup count as this will avoid performing the query at all. – Phil W Dec 4 '20 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.