0

I am trying to send data from salesforce to external system and have written web service but even after properly encoding every parameter, i am getting argument cannot be null due to which data is not getting sent. Please help me with the possible cause of error.

below is the code snippet:

 @future(Callout=true)    
     public static void makePostCallout2(String first, String last, String em, String conID, String acctID, String utype, String prod, String expDate, String acctOwner, String lmd, String flag, String createdBy, String origin) {

        Http http = new Http();

        HttpRequest request = new HttpRequest();
         request.setEndpoint('***********************');
        request.setMethod('PUT');
       request.setHeader('Content-Type', 'application/x-www-form-urlencode');
        request.setHeader('Authorization', '*****************');
         request.setBody('firstname='+EncodingUtil.urlEncode(first,'UTF-8')+'&lastname='+EncodingUtil.urlEncode(last,'UTF-8')+'&email='+EncodingUtil.urlEncode(em,'UTF-8')+'&contactID='+EncodingUtil.urlEncode(conID,'UTF-8')+'&accountID='+EncodingUtil.urlEncode(acctID,'UTF-8')+'&userType='+EncodingUtil.urlEncode(uType,'UTF-8')+'&product='+EncodingUtil.urlEncode(prod,'UTF-8')+'&expiryDate='+EncodingUtil.urlEncode(expDate,'UTF-8')+'&accountOwner='+EncodingUtil.urlEncode(acctOwner,'UTF-8')+'&lastModifiedDate='+EncodingUtil.urlEncode(lmd,'UTF-8')+'&operation='+EncodingUtil.urlEncode(flag,'UTF-8')+'&createdBy='+EncodingUtil.urlEncode(createdBy,'UTF-8')+'&creationOrigin='+EncodingUtil.urlEncode(origin,'UTF-8'));        
        HttpResponse response = http.send(request);
         system.debug('this is after processing response');
        // Parse the JSON response
        if (response.getStatusCode() != 200) {
            System.debug('The status code returned was not expected: ' +response.getStatusCode() + ' ' + response.getStatus());
        } else {
            System.debug(response.getBody());
        }
    }       

the argument cannot be null is occuring in initializing end point and method in headers.. for privacy reason, endpoint and token has been masked.

4
  • 1
    Just to confirm, is the error happening on request.setBody? If so, you should implement some sanity checks on your parameters before trying to encode them, or introduce error handling. Feb 19, 2020 at 17:14
  • Which specific line throws the exception? Are the masked parameters provided as literal strings or sourced from the database?
    – David Reed
    Feb 19, 2020 at 17:14
  • error is happening after request.setBody and masked parameters are provided as literal strings only. Feb 19, 2020 at 17:27
  • 1
    @DrewKennedy This should have been an answer. EncodingUtil methods don't like null values.
    – sfdcfox
    Feb 19, 2020 at 17:39

1 Answer 1

2

You're receiving this error because you're providing null as values for encoding. To better handle these situations, you can either perform sanity checks on your parameters, or introduce some error handling.

An example of sanity checks would be

@future(Callout=true)    
public static void makePostCallout2(String first, String last, String em, String conID, String acctID, String utype, String prod, String expDate, String acctOwner, String lmd, String flag, String createdBy, String origin) {

    if (String.isBlank(first)) {
        //do something with "first"
    }

    if (String.isBlank(last)) {
        //do something with "last"
    }

This isn't the cleanest approach, but has its place. The more recommended approach would be to introduce some basic error handling:

@future(Callout=true)    
public static void makePostCallout2(String first, String last, String em, String conID, String acctID, String utype, String prod, String expDate, String acctOwner, String lmd, String flag, String createdBy, String origin) {
    try {
        Http http = new Http();

        HttpRequest request = new HttpRequest();
        request.setEndpoint('***********************');
        request.setMethod('PUT');
        request.setHeader('Content-Type', 'application/x-www-form-urlencode');
        request.setHeader('Authorization', '*****************');
        request.setBody('firstname='+EncodingUtil.urlEncode(first,'UTF-8')+'&lastname='+EncodingUtil.urlEncode(last,'UTF-8')+'&email='+EncodingUtil.urlEncode(em,'UTF-8')+'&contactID='+EncodingUtil.urlEncode(conID,'UTF-8')+'&accountID='+EncodingUtil.urlEncode(acctID,'UTF-8')+'&userType='+EncodingUtil.urlEncode(uType,'UTF-8')+'&product='+EncodingUtil.urlEncode(prod,'UTF-8')+'&expiryDate='+EncodingUtil.urlEncode(expDate,'UTF-8')+'&accountOwner='+EncodingUtil.urlEncode(acctOwner,'UTF-8')+'&lastModifiedDate='+EncodingUtil.urlEncode(lmd,'UTF-8')+'&operation='+EncodingUtil.urlEncode(flag,'UTF-8')+'&createdBy='+EncodingUtil.urlEncode(createdBy,'UTF-8')+'&creationOrigin='+EncodingUtil.urlEncode(origin,'UTF-8'));        
        HttpResponse response = http.send(request);
        system.debug('this is after processing response');
        // Parse the JSON response
        if (response.getStatusCode() != 200) {
            System.debug('The status code returned was not expected: ' +response.getStatusCode() + ' ' + response.getStatus());
        } else {
            System.debug(response.getBody());
        }
    } catch (IllegalArgumentException iae) {
        //do something with iae
    } catch (Exception e) {
        //do something with e
    }
}

Also, wherever this is being called from, you may also want to check why you're feeding in bad data to begin with.

3
  • after applying all the null checks, i am getting 415 Unsupported Media Type error.. Feb 19, 2020 at 18:03
  • This was happening before also when i was trying to send parameters in body without encoding methods.. dont why its happening. in postman its returning data but in apex its returning unsupported media type error Feb 19, 2020 at 18:04
  • That sounds like a completely different issue. To avoid this becoming an XY problem, I'd recommend creating another question for that particular issue. For your benefit though, here's the MDN documentation on 415 errors. Feb 19, 2020 at 18:12

You must log in to answer this question.

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