function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
New_DeveloperNew_Developer 

Displaying validation rule error message

Hi,

 

I have an after update trigger and iam getting the below error

 

System.DmlException: Update failed. First exception on row 0 with id a0zc0000001JnZLAA0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, You are above your limit to select "Pay All" Action: []: Trigger.updateAlertItems: line 47, column 1

 

How can i get only the red highlighted error message which is a validation rule error message instead of that whole trigger FIELD_CUSTOM_VALIDATION_EXCEPTION error

 

Below is the trigger

 

trigger updateAlertItems on Alerts__c (after update) {

Set<ID> ids = Trigger.newMap.keySet();
List<Alerts__c> updatedParents = [SELECT Id, Calculated_Balance__c ,
(SELECT Id, Action__c , Pay_Validation_Rule_Check_1__c , Pay_Validation_Rule_Check_2__c , Pay_Validation_Rule_Check_3__c
from Alert_Items__r ) FROM Alerts__c
WHERE Id in :ids];


List<Alert_Items__c> childrenToUpdate = new List<Alert_Items__c>();

for ( Alerts__c p : updatedParents)
{

if(trigger.oldMap.get(p.id).Action__c!= p.Action__c)
{
system.debug('Enter here 1');
for(Alert_Items__c kid : p.Alert_Items__r)
{

if(p.Action__c == 'Pay All')
{
System.debug('Enter here 2');
kid .Action__c= 'Pay';
}

if(p.Action__c == 'Return All')
{
kid .Action__c= 'Return';

}

childrenToUpdate.add(kid);
}
}
}

if( !childrenToUpdate.isEmpty())
{

System.debug('Enter here 3');
update childrenToUpdate;

}

}

 

Thanks

 

Vijaya Kumar RegantiVijaya Kumar Reganti

Hi,

 

try it in the following way.

 

Try {

 

         update childrenToUpdate;

}

 

Catch(exception e) {

 

          ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'An error occoured. '+e.getMessage()));

}

 

It will help you.You can give me Kudos if it helps.

 

Thanks,

 

Vijay

New_DeveloperNew_Developer

Thanks vijay. i tried it and its giving me below error now.

 

Error:Apex trigger updateAlertItems caused an unexpected exception, contact your administrator: updateAlertItems: execution of AfterUpdate caused by: System.FinalException: ApexPages.addMessage can only be called from a Visualforce page:

 

 

Thanks

sushant sussushant sus

hi ,

you have some validation rule which is showing this error first check that

and

ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'An error occoured. '+e.getMessage()));

this we use in apex class to show msg in vf page
In trigger we use
object.adderror();

Vijaya Kumar RegantiVijaya Kumar Reganti

Hi,

 

Please try this.it may help u out.

 

Catch(exception e) {

 

  if(ApexPages.CurrentPage()  != null) {

         

         ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'An error occoured. '+e.getMessage()));

 

       }

}