0

I have a requirement where the data from external system is fetched and upserted back in Salesforce.

below is my apex class:

public class RESTClass {
    @future(callout=true)
    
    public static void RESTClass() {   
        
        String jsonbody = '{ ' +
            '"username": "<username>",' +
            '"password": "<password>"' +
            '}' ;
        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
        req.setTimeout(120000);
        req.setHeader('Content-Type', 'application/json');
        req.setHeader('Accept','application/json');
        req.setEndpoint('<user auth endpoint>');
        req.setBody(jsonBody);   
        Http http = new Http();
        HTTPResponse res = http.send(req);
        String response = res.getBody();
        UserAuthJSONParser jsondes = (UserAuthJSONParser) JSON.deserialize(response, UserAuthJSONParser.class);
        HttpRequest policyreq = new HttpRequest();
        policyreq.setMethod('GET');
        policyreq.setTimeout(120000);
        policyreq.setEndpoint('<main endpoint>'); 
        policyreq.setHeader('Authorization', 'Bearer ' + jsondes.value.token);
        Http policyhttp = new Http();
        HTTPResponse policyres = policyhttp.send(policyreq);
        String policyresponse = policyres.getBody();
        
        JsonParser objJsonParser = (JsonParser) JSON.deserialize(policyresponse, JsonParser.class);
        
        JsonParser.cls_value clsValue = objJsonParser.value;
        Map<String, JsonParser.cls_data> clsDataMap = new Map<String, JsonParser.cls_data>();
        
      
        for(JsonParser.cls_data objClsData: clsValue.data){
            clsDataMap.put(objClsData.id, objClsData);
        }
        list<policy__c> updatelist = new list<policy__c>();
        
      
        for (String eachIdFromMap : clsDataMap.keySet()){
            
            policy__c policy = new policy__c(
                unique_id__c = clsDataMap.get(eachIdFromMap).id,
                policy_date__c = clsDataMap.get(eachIdFromMap).policydDate);
            updatelist.add(policy);  
        }
        upsert updatelist unique_id__c;
    }    
    
}

Now, from the line clsDataMap.get(eachIdFromMap).policydDate, I am getting the error as

System.NullPointerException: Argument cannot be null.

Even though I have null checked that line as follows, I could see the same error.

string poldate;
if(String.isNotBlank(clsDataMap.get(eachIdFromMap).policyDate)){
               poldate = clsDataMap.get(eachIdFromMap).policyDate;
            }

Below is the debug log of poldate:

poldate From the above debug log, few rows have values and few are having null.

Can anyone please suggest any changes to the above logic so that I can get this error resolved.

Thanks!

8
  • It probably is your eachIdFromMap variable that is null, I'd check it first... – Emmanuel BRUNO Nov 26 '20 at 8:12
  • It has values and other fields are getting updated. But for policyDate, there are few rows which are null and which has values. and that is causing problem here. Is there any way to null check that? – SFDCUser Nov 26 '20 at 8:33
  • Where is your error when you check ? Is it on the line "poldate = clsDataMap.get(eachIdFromMap).policyDate;", is it on the line where you create the new policy__c, or is it on the update statement line ? Just to be sure, can you change your line "policy_date__c = clsDataMap.get(eachIdFromMap).policydDate " to "policy_date__c = (eachIdFromMap!=null) ? clsDataMap.get(eachIdFromMap).policydDate : null" ? Are there triggers on you policy__c object ? Did you try to check the log in the developer console ? – Emmanuel BRUNO Nov 26 '20 at 8:48
  • @EmmanuelBRUNO, no there are no triggers and the error is from line poldate = clsDataMap.get(eachIdFromMap).policyDate. As suggested, even though I have updated the line to policy_date__c = (eachIdFromMap!=null) ? clsDataMap.get(eachIdFromMap).policydDate : null, I am seeing the error. Few rows have values and few have value as null. Wondering what is the issue behing this error – SFDCUser Nov 26 '20 at 8:58
  • 1
    Can you paste the stacktrace? And that is not how you check null in maps . First you check if your map contains key , then check if the key contains the value, then check if the value has the required parameter. clsDataMap.containsKey(eachIdFromMap) && clsDataMap.get(eachIdFromMap) != null && clsDataMap.get(eachIdFromMap).policyDate != null – Nagendra Singh Nov 26 '20 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.