0

System.DmlException: Update failed. First exception on row 0 with id a2E4D00000056nGUAQ; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Campaign Quantity is less than Sum of Placements Quantity: []: Class.MediaAllocationCampaignUtilities.updateMediaAllocationPlacementsMethod: line 95, column 1

Below is My Trigger:

trigger ValidateQunat on Media_Allocation__c (before update) {

Decimal x= 0;
Decimal y= 0;
List<Id> MedIDLst = new List<Id>();
 for(Media_Allocation__c medAloc : Trigger.new)
 {
     MedIDLst.add(medAloc.Id);
 }   
List<Media_Allocation_Placement__c> plcLst = [SELECT Id,Quantity__c FROM Media_Allocation_Placement__c WHERE Media_Allocation_Campaign__c =: MedIDLst];
// List<Media_Allocation__c> MedLst = [Select id, quantity__c from Media_Allocation__c where id = 'a2E4D00000056nG'];    
 List<Media_Allocation__c> MedLst = [Select id, quantity__c from Media_Allocation__c where id =: MedIDLst];

     for(Media_Allocation__c medAloc : Trigger.new)
    {

        for(Media_Allocation_Placement__c plc : plcLst)
        {
          if(plc.Quantity__c !=NULL)
          x+= plc.Quantity__c;
          system.debug('****************'+x);
        }
        for(Media_Allocation__c Med : MedLst)
        {
            y = Med.Quantity__c;
            system.debug('$$$$$$$$$$$$$$$$$$$$$'+y);

        }
        try{ 
        if(y<x)
        {
         medAloc.addError('Campaign Quantity is less than Sum of Placements Quantity');
        }
        }catch(Exception e){

        //medAloc.adderror(e);
        }


    }
}

**Note: I want to handle that exception and populate User Understandable Error like "Campaign Quantity is less than Sum of Placements Quantity"**alone

  • do you want to display this message on standard layout, or custom VF page, lighting component? – Oleksandr Berehovskyi Jun 29 '17 at 10:11
0

To give user friendly error message out of exception will require little bit of try catch game in your code at the place where the error is actually rising. So your trigger will look like this after proper try catch combination in the red zone.

trigger ValidateQunat on Media_Allocation__c(before update) {

 Decimal x = 0;
 Decimal y = 0;
 List<Id> MedIDLst = new List<Id>();

 for (Media_Allocation__c medAloc: Trigger.new) {
  MedIDLst.add(medAloc.Id);
 }

 List <Media_Allocation_Placement__c> plcLst = [SELECT Id, Quantity__c FROM Media_Allocation_Placement__c WHERE Media_Allocation_Campaign__c =: MedIDLst];
 List <Media_Allocation__c> MedLst = [Select id, quantity__c from Media_Allocation__c where id =: MedIDLst];

 for (Media_Allocation__c medAloc : Trigger.new) {

  for (Media_Allocation_Placement__c plc : plcLst) {
   if (plc.Quantity__c != NULL)
    x += plc.Quantity__c;
    system.debug('****************' + x);
  }//end 1st for

  for (Media_Allocation__c Med: MedLst) {    
    try {
       y = Med.Quantity__c;
       System.debug('$$$$$$$$$$$$$$$$' + y);

       if(y < x) 
         throw new DMLException();

    } catch (DMLException e) {    
        Med.Quantity__c.addError('Campaign Quantity is less than Sum of Placements Quantity');    
    }    
  }//end 2nd for

 }//end main for

}//end trigger
| improve this answer | |
  • not sure about the total solution but addError can't be added like this medAloc.plc.Quantity__c.addError see here developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…. – Avijit Chakraborty Jun 29 '17 at 14:19
  • 1
    @Avijit Chakraborty. 'Plc' was just a typo. But if we want to show error next to a field of an object. We can use it and it works. – LetMeCodeYou Jun 29 '17 at 14:45
  • I don't see how an error is thrown and thus the catch will never happen... – Eric Jun 29 '17 at 17:59

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.