0

I'm unable to run the below code as it throws me an exception null argument for JSONgenerator.writestringfield(), i tried adding multiple where clauses to check null exception for Reported first name, last name, phone and email, unfortunately it didn't work.

Patient_Satisfaction__c c = [select id,Name, Patient_First_Name__c,Patient_Last_Name__c, Reporter_Phone__c,Reporter_First_Name__c,Reporter_Last_Name__c, Reporter_Email__c,
Description_of_Feedback__c from Patient_Satisfaction__c where Patient_Relation__c ='Referred to Privacy Office'  order by lastmodifiedDate desc limit 1];
//generating JSON 
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeObjectField('name', c.Patient_Last_Name__c + ', ' + c.Patient_First_Name__c);
gen.writeObjectField('incident_group_id', 3479);
gen.writeObjectField('description',c.Description_of_Feedback__c);
gen.writeFieldName('submitted_by');
gen.writeStartObject();
gen.writeStringField('given_name',c.Reporter_First_Name__c);
gen.writeStringField('surname', c.Reporter_Last_Name__c);
gen.writeStringField('phone',c.Reporter_Phone__c);
gen.writeStringField('email',c.Reporter_Email__c);
gen.writeEndObject();
gen.writeFieldName('custom');
gen.writeStartObject();
gen.writeStringField('TRIPSS ID',c.Name);
gen.writeEndObject();
String jsonS = gen.getAsString(); 
System.debug('jsonMaterials'+jsonS);
//generating Http request
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.radarfirst.com/incidents');
request.setMethod('POST');
request.setHeader('Content-Type','application/json;charset=UTF-8');
request.setHeader('User-agent', 'Salesforce-integration-client');
request.setHeader('Authorization','Bearer xyz');
request.setBody(jsonS);
// Set the body as a JSON object
HttpResponse response = http.send(request);
if (response.getStatusCode() != 201) {
System.debug('The status code returned was not expected: ' +
    response.getStatusCode() + ' ' + response.getStatus());
} else {
System.debug(response.getBody());
}
1
  • Please edit your post - which line is throwing the error? What if statement did you use to check for null exception? Jun 24, 2020 at 18:49

1 Answer 1

3

This is one of the many reasons why I suggest never using JSONGenerator. It's too finicky, especially since you could just use a much easier method of serializing the data:

String jsonS = JSON.serialize(
    new Map<String, Object> {
        'name' => c.Patient_Last_Name__c + ', ' + c.Patient_Last_Name__c,
        'incident_group_id' => 3479,
        'description', c.Description_of_Feedback__c,
        'submitted_by' => new Map<String, Object> {
            'given_name' => c.Reporter_First_Name__c,
            'surname' => c.Reporter_Last_Name__c,
            'phone', c.Reporter_Phone__c,
            'email', c.Reporter_Email__c
        },
        'custom' => new Map<String, Object> {
            'TRIPSS ID' => c.Name
        }
    }, 
    false // emit nulls. Change to true to suppress nulls. //
);

This is easier to read, supports nulls without checking every field for null values, requires far less typing, and uses far less CPU time. JSONGenerator should generally only be used to port Java code to Apex that already uses this.

To fix your original code, you need to check every possible field that may be null:

if(c.Description_of_Feedback__c != null) {
  gen.writeObjectField('description',c.Description_of_Feedback__c);
} else {
  gen.writeNullField('description');
}
...
if(c.Reporter_First_Name__c != null) {
  gen.writeStringField('given_name',c.Reporter_First_Name__c);
} else {
  gen.writeNullField('given_name');
}
...

Which adds even more bulk to an already lengthy piece of code.

You must log in to answer this question.

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