0

I have Master detail relation ship. I have written a trigger on child object to prevent the deletion of child when parent is in Approved or Inreview status and throw error message which is working fine.

My question is how do I show error message on parent object? which I am not able to do as I have written trigger on child object.

Any help would be Appreciated.

Below is my trigger logic.

trigger PreventQLDeletion on SBQQ__QuoteLine__c (before delete) {

PreventQLDeletionTriggerHandler PQLth = new PreventQLDeletionTriggerHandler(); 

      if(Trigger.isBefore){
         if(Trigger.isdelete){
                 PQLth.deleteQL(trigger.old);
            }
      }
    }

public with sharing class PreventQLDeletionTriggerHandler{

 public void deleteQL(List<SBQQ__QuoteLine__c> oldQLList){

           Set <ID> QLIdSet = New Set <ID> ();
            for (SBQQ__QuoteLine__c ql: oldQLList) {
                QLIdSet.add(ql.SBQQ__Quote__c);
            }

             set <id> QuoteIdSet = new set <id>();
             for(SBQQ__Quote__c q:[Select Id,SBQQ__Status__c From SBQQ__Quote__c Where (SBQQ__Status__c = 'In Review' OR SBQQ__Status__c = 'Approved') and Id IN: QLIdSet]){
              QuoteIdSet.add(q.id);
             }

            For(SBQQ__QuoteLine__c ql: oldQLList) {
                If(QuoteIdSet.contains(ql.SBQQ__Quote__c))
                ql.addError('You cannot delete a quotelineitem when approved or reviewed.');
            }


        }

        }
  • where the error appears has a lot to do with how/where the quote line is deleted. Use edit and add a screen shot of where the delete is initiated from – cropredy Nov 8 '19 at 21:59
0

Since there is a master-detail relationship between the parent and child object, here is an alternative solution without code:

-Create a roll-up summary field on the parent object, to count the number of child records (let's call the field Count_Quote_Line__c)

-Then on the parent object create a validation rule as follows :

 Count_Quote_Line__c < PRIORVALUE(Count_Quote_Line__c ) &&
(ISPICKVAL(SBQQ__Status__c , "In Review") || ISPICKVAL(SBQQ__Status__c , "Approved"))

The validation rule criteria is checking this: If the roll-up count value has decreased when comparing with prior value, then this means a user is attempting to delete a child record and if the parent object status match 'In Review or 'Approved' then fire the validation rule error message on parent record and prevent the deletion of the child record.

Hope this helps

  • Thanks for the response. It worked fine.. but error message comes in different page not on the parent record detail page. Any directions here? – sfdcuser Nov 8 '19 at 21:17
  • You mean the error displays in a modal window? This is a normal behavior because the error is being triggered by an action at a different level in this case on the child record, so the error cannot be displayed on the parent record detail page. – Shamina Nov 9 '19 at 5:31

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.