+ Start a Discussion
prabhat jhaprabhat jha 

Error message shows extra message "Validation Errors While Saving Record(s) There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Please provide Rejection Reason!".

Hi Experts,
I want to show only Custom error message "Please provide reason for Rejection"if Some user rejects an approval request and must provide comments,but it shows also validation error which i dont want to display and i am confused from where this error is thrown.
public class salesInvoiceRevenueSplitTriggerHandler{
    
    Public static void updateCommentsandDisplayError (List<Sales_Invoice_Revenue_Split__c> newSalesInvoiceRevenueList, Map<id, Sales_Invoice_Revenue_Split__c> oldMap){

    Map<Id, Sales_Invoice_Revenue_Split__c> rejectedStatements 
             = new Map<Id, Sales_Invoice_Revenue_Split__c>{};

  for(Sales_Invoice_Revenue_Split__c sirsObj: newSalesInvoiceRevenueList)
  {
    /* 
      Get the old object record, and check if the approval status 
      field has been updated to rejected. If so, put it in a map 
      so we only have to use 1 SOQL query to do all checks.
    */
    Sales_Invoice_Revenue_Split__c oldsirsObj = oldMap.get(sirsObj.Id);

    if (oldsirsObj .Margin_Approval_Status__c != 'Rejected' 
     && sirsObj.Margin_Approval_Status__c == 'Rejected')
    { 
      rejectedStatements.put(sirsObj.Id, sirsObj);  
    }
  }
   
  if (!rejectedStatements.isEmpty())  
  {
    // UPDATE 2/1/2014: Get the most recent approval process instance for the object.
    // If there are some approvals to be reviewed for approval, then
    // get the most recent process instance for each object.
    List<Id> processInstanceIds = new List<Id>{};
    
    for (Sales_Invoice_Revenue_Split__c invs : [SELECT (SELECT ID
                                              FROM ProcessInstances
                                              ORDER BY CreatedDate DESC
                                              LIMIT 1)
                                      FROM Sales_Invoice_Revenue_Split__c
                                      WHERE ID IN :rejectedStatements.keySet()])
    {
        processInstanceIds.add(invs.ProcessInstances[0].Id);
    }
      
    // Now that we have the most recent process instances, we can check
    // the most recent process steps for Comments.  
    for (ProcessInstance pi : [SELECT TargetObjectId,
                                   (SELECT Id, Comments  
                                    FROM Steps
                                    ORDER BY CreatedDate DESC
                                    LIMIT 1 )
                               FROM ProcessInstance
                               WHERE Id IN :processInstanceIds
                               ORDER BY CreatedDate DESC])   
    {                   
      if ((pi.Steps[0].Comments  == null || 
           pi.Steps[0].Comments .trim().length() == 0))
      { 
        rejectedStatements.get(pi.TargetObjectId).addError('Please provide Rejection Reason!');
   //pi.addError('Please provide Rejection Reason!');
      }
        else{rejectedStatements.get(pi.TargetObjectId).Comments__c=pi.Steps[0].Comments; 
          }
    }  
  }

    }

 



}
This method (updateCommentsandDisplayError) is called from Trigger on which approval process is written.
Please Suggest the possible Reason/Problem in my code.
Thanks
 
jigarshahjigarshah
Prabhat,

You are recieving the error because of addError() statement in your Apex class at line # 55. If your question is the reason for receiving the error then it could possibly be because a logical issue in the code flow.

Check the code and its behaviour using Debug Logs to understand why the respective if block is being executed.