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
AmphitriteAmphitrite 

Controller Method array is updating 2x with try-catch

can I try and catch errors without the statement 'updates getcSelected;' actually running?

 

//Save and Next method
public PageReference SaveNext(){

 

for(Case a :getcSelected()){

a.InEditUserId__c=InEditUserId;
a.InEditUser__c=InEditUserName;
a.UserLock__c=InEditUserName + ' '+InEditTime;
a.Cancellation_Reason__c=cancelClick;
a.UserActions__c=userClick;
a.AdminActions__c=adminClick;

 

}

 

try{
Update getcSelected();
}


catch(DmlException dmle){
If(dmle.getDmlType(0) == StatusCode.FIELD_CUSTOM_VALIDATION_EXCEPTION){
ApexPages.addMessages(dmle);
return null;
}
else throw dmle;
}

Update getcSelected();

 

 

PageReference pageRef = page.BulkUpdateCases2;
pageRef.setRedirect(true);
return pageRef;

}

Best Answer chosen by Admin (Salesforce Developers) 
AmphitriteAmphitrite

This solution acted as expected. I moved the final DML update statement inside the 'else' statement.

 

 try{ 
              Update getcSelected();
          }
          catch(DmlException dmle){ 
             If(dmle.getDmlType(0) == StatusCode.FIELD_CUSTOM_VALIDATION_EXCEPTION){
              ApexPages.addMessages(dmle);
              return null;
             }
             else throw dmle;
             Update getcSelected();
          }