2

I have a class which i called from a custom button to send the record for approval. Before sending the record for approval I need to validate the record values and if any error is there I want to display it on standard page of the record. For this I have sobjectname.addError(). But is throwing a error :-

System.FinalException: SObject row does not allow errors

My code :-

global class SpecialPricingApproved_Controller{

webService static string SpecialPricingApproved_Request(ID oppid){

    List<opportunity> oppList = new List<opportunity>();

    for(Opportunity oppResult : [SELECT OwnerId, Manager_of_Manager__c, Max_Discount__c FROM opportunity WHERE ID = :oppid]){
        if(oppResult.Max_Discount__c == 10.00 || oppResult.Max_Discount__c == null){

            Opportunity oppError = new Opportunity();
            oppError = oppResult;
            oppError.addError('Max discount should be greater than 10 % for Approval');

            return null;
        }


        if(oppResult.Manager_of_Manager__c == null){
            oppList.add(oppResult);
            Opportunity_Helper.addManagerofManager(oppList, true);
        }
    }

    //Sending record for approval 
    Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
    req1.setComments('Submitted for Approval');
    req1.setObjectId(oppid);
    Approval.ProcessResult res = Approval.Process(req1);   

    return null;

}
}

My error :

A problem with the OnClick JavaScript for this button or link was encountered:{faultcode:'soapenv:Client', faultstring:'System.FinalException: SObject row does not allow errors Class.SpecialPricingApproved_Controller.SpecialPricingApproved_Request: line 22, column 1', }

Line no 22 is :

oppError.addError('Max discount should be greater than 10 % for Approval');

Can anyone help me to solve this error.

4
  • Can you please refer below link? srinivas4sfdc.blogspot.in/2013/12/… Apr 17, 2015 at 14:45
  • Can we show error message as javascript alert? Apr 17, 2015 at 14:46
  • @PankajGanwani made the changes as per refer link. But getting the same error.
    – Anu
    Apr 20, 2015 at 6:04
  • @PankajGanwani updated my code with error.
    – Anu
    Apr 20, 2015 at 6:17

2 Answers 2

1

You need to do 2 things.

1) Return the result back to your javascript button.

var result = myJSWebcallhere();
if (result != '')
  alert(result);

2) catch the errors in your service and return them as the result.

webService static string SpecialPricingApproved_Request(ID oppid)
{
  try
  {
    List<opportunity> oppList = new List<opportunity>();
    Opportunity errorHandler = new Opportunity();

    oppList = [SELECT OwnerId, Manager_of_Manager__c, Max_Discount__c FROM opportunity WHERE ID = :oppid];      

    if(oppList.size() > 0){
      if(oppList[0].Max_Discount__c == 10.00 || oppList[0].Max_Discount__c == null){
        return 'Max discount should be greater than 10 % for Approval';
      }

      if(oppList[0].Manager_of_Manager__c == null){
        Opportunity_Helper.addManagerofManager(oppList, true);
      }
    }

    //Sending record for approval 
    Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
    req1.setComments('Submitted for Approval');
    req1.setObjectId(oppid);
    Approval.ProcessResult res = Approval.Process(req1);
  }
  catch (Exception e)
  {
    return 'Error = ' + e.getMessage());
  }
  return '';
}
2
  • made the changes as per your code getting error : A problem with the OnClick JavaScript for this button or link was encountered: Unexpected identifier
    – Anu
    Apr 20, 2015 at 6:18
  • @Anu I forgot, Javascript doesn't use 'String'. It uses 'Var'.
    – dphil
    Apr 20, 2015 at 14:15
0

Do not do the validation in the apex class, do it on the page button itself.

Just call the class for Approval process execution.

Class should be like

Public class Class Name{


webService static void SpecialPricingApproved_Request(ID oppid)

{

    List<opportunity> oppList = new List<opportunity>();
    Opportunity errorHandler = new Opportunity();

    oppList = [SELECT OwnerId, Manager_of_Manager__c, Max_Discount__c FROM opportunity WHERE ID = :oppid];         

      if(oppList[0].Manager_of_Manager__c == null){
        Opportunity_Helper.addManagerofManager(oppList, true);
      }
    }

    //Sending record for approval 
    Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
    req1.setComments('Submitted for Approval');
    req1.setObjectId(oppid);
    Approval.ProcessResult res = Approval.Process(req1);

}

}

Button should have

var x ='{!Max_Discount__c}';

if(x==10||x==null){
alert('errormessage');
}

call apex method using sforce.apex.execute and pass id to the method as you did

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .