1

Hi I have a code which makes an apex callout to a external system. I query a list of records and loop it through to make a callout. Example code:

try{
List<object__c> s= [select.....records];
for(object__c rr : s){

var1 = rr.field1__C;
var2 = rr.field2__c;
var3 = rr.field3__c;

}

}
catch(Exception e){
system.debug('****' + e.getMessage());
}

In my above code I am looping through all the records that were queried and setting it up in the variable which are part of the callout request.

My problem: Var1 field is a required field by external system. There is an exception if the field1__c value is missing any record. It gives a null pointer exception if the value is not there. If one record does not have this value the programs goes into the exception block and stops the process. I want the program to process the other records which has the field1__c value available. How can I make other record process even though one record fails to make a callout?

2 Answers 2

2

1st Approach

You can simply do not send the record which is null.

try{
    List<object__c> s= [select.....records];
    for(object__c rr : s){

        var1 = rr.field1__C;
        var2 = rr.field2__c;
        var3 = rr.field3__c;
        if(var1 !=null)
        {
            //make the callout
        }
    }
}
catch(Exception e){
    system.debug('****' + e.getMessage());
}

2nd Approach

Consume the error in the catch block and proceed with the loop.

try {
    List<object__c> s= [select.....records];
    for(object__c rr : s)
    {
        try{

            var1 = rr.field1__C;
            var2 = rr.field2__c;
            var3 = rr.field3__c;

        }
        catch(System.NullPointerException e){
         //catch the error and consume.
        }
    }
catch(Exception e)
{
    //for all other error.  

}

Ideally, NullPointerException should not be handled in the catch block, rather do the null check and take necessary actions.

2
  • thanks, I would have to check the null condition, but I have a complex logic with various for loops so when I come to callout line I do not have the var1 field visible at that line. But I think this should be the approach to handle a null pointer exception
    – SfdcBat
    Commented Feb 3, 2017 at 4:34
  • I have updated my ans to 2nd approach for better exception handling Commented Feb 3, 2017 at 4:46
1

You could try doing the callouts from an asynchronous method. You would be limited to 50 future methods, but the results of any one callout wouldn't effect the others.

4
  • Is this asynchronous method generated from wsdl2apex tool?
    – SfdcBat
    Commented Feb 6, 2017 at 2:30
  • Wsdl2Apex creates a continuation version of the SOAP web service callouts, but it is only intended to be used from Visualforce. You would need to create a method using the @future annotation. It should also include the attribute to indicate that a callout will be performed. Commented Feb 6, 2017 at 3:18
  • Thanks appreciate a response from you always. I have almost completed all the development using the normal wsdl2apex class which was generated using wsdl2apex tool. I call the function which has invoke method in it from a apex callout class that i created. If there is an error in one record which is used when calling the web service will other records fail as well? Is there anything I can do now without going back to scratch of development.
    – SfdcBat
    Commented Feb 6, 2017 at 3:23
  • It really depends on how the external web service is implemented. Certainly calling the wsdl2apex generated class from the @future method would prevent errors from any one callout from affecting the others. Otherwise straight exception handling should be sufficient. Commented Feb 6, 2017 at 7:30

You must log in to answer this question.

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