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
jpbenjpben 

Exception Handling

Hello,

 

I need some help with incorporating exception handling (try/catch) on my code below.  I have a trigger that auto updates the stage on the Opportunity to "Feasibility", when a Feasibility (custom object) record is created. We have a validation rule on the Opportunity that all the required fields will need to be filled in before you can change the Opportunity Stage to "Feasibility".  How can I update the dml error message to "Fill in all required fields on the Opportunity" so that our user doesnt see this long text.  

 

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger OpptyStageUpdate_Feasibility caused an unexpected exception, contact your administrator: OpptyStageUpdate_Feasibility: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 006J0000007V2QeIAK; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Field is required.: [Budget__c]: Trigger.OpptyStageUpdate_Feasibility: line 31, column 1

 

 

 

trigger OpptyStageUpdate_Feasibility on Feasibility__c (after insert, after update) {

// Updates the Opportunity Stage to Feasibility when Feasibility record is created

//Lists the Opportunities from Proposals that meet criteria

List<ID> feasIds = new List<ID>();
for (Feasibility__c f: Trigger.new){
if(f.Stage__c <> null){
feasIds.add(f.Opportunity__c);
}
}

//Pulls the SOQL list and updates the Oppty Stage
List<Opportunity> opptyList = [SELECT ID, StageName, Opportunity_Type__c, RecordTypeId
FROM Opportunity
WHERE ID in:feasIds];



for (Feasibility__c f: Trigger.new){
for (Opportunity opp: opptyList){

if (f.Stage__c <> 'Closed Rejected'){
opp.StageName = 'Feasibility' ;
}
}

}

update opptyList;

}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Kiran  KurellaKiran Kurella

You can add error handling code to your trigger as follows:

 

try {
       update opptyList;
} catch (DmlException ex) {
        trigger.new[0].addError(ex.getDmlMessage(0));
        //throw ex;
}

All Answers

Kiran  KurellaKiran Kurella

You can add error handling code to your trigger as follows:

 

try {
       update opptyList;
} catch (DmlException ex) {
        trigger.new[0].addError(ex.getDmlMessage(0));
        //throw ex;
}

This was selected as the best answer
jpbenjpben

This might help other users out there.  Here is my final code.  

 

trigger OpptyStageUpdate_Feasibility on Feasibility__c (after insert) {

// Updates the Opportunity Stage to Feasibility when Feasibility record is created

//Lists the Opportunities from Feasibility that meet criteria
try {
List<ID> feasIds = new List<ID>();
for (Feasibility__c f: Trigger.new){
if(f.Stage__c <> null){
feasIds.add(f.Opportunity__c);
}
}

//Pulls the SOQL list and updates the Oppty Stage
List<Opportunity> opptyList = [SELECT ID, StageName, Opportunity_Type__c, RecordTypeId
FROM Opportunity
WHERE ID in:feasIds];



for (Feasibility__c f: Trigger.new){
for (Opportunity opp: opptyList){

if (f.Stage__c <> 'Closed Rejected'){
opp.StageName = 'Feasibility' ;
}
}

}


update opptyList;


}catch (System.DmlException e){
trigger.new[0].addError('Fill in all of the required fields on the Opportunity before creating the Feasibility');
}
}

jpbenjpben
Thank you for your solution. I just tested it and this works.