0

I'm just trying to call a post method if a multipicklist value is selected at the time of update. Not sure where exactly I'm making mistake

trigger RadarFirst on Patient_Satisfaction__c (before update) {
    for (Patient_Satisfaction__c cc : Trigger.new){
        if(cc.Patient_Relation__c.contains('Referred to Privacy Office')){
            RadarUpdate.postcallout(cc.id);
        }
    }
}
public class RadarUpdate {
    @future (callout=true)
    public static void postcallout(string Id) { 
    Patient_Satisfaction__c c = [select id, Name, 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];
    JSONGenerator gen = JSON.createGenerator(true);
    gen.writeStartObject();
    gen.writeObjectField('name', c.Name);
    gen.writeObjectField('incident_group_id', 7387);
    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();
    String jsonS = gen.getAsString(); 
    System.debug('jsonMaterials'+jsonS);
    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 eyJhbGciOiJSU0EtT0FFUCIsImVuYyI6IkExMjhHQ00ifQ.ayMT8mfFcCqkLyRGZsmodZfjcDpm-cGV0f3EJ_5NF-KU-DJ9DVWrUEzO4XUj4LjKLlU9ub9793bJuZdNkAGFJbUmecq7k3bfvVbJmuBwzlJny3wHnSLpf0khXR_UT5FIiKHji5iSmbE8Ojc-qfPwwn2B5S7SAZ44LAB-5ag7AErALqEOgSLYm0CKIGNvYGFmMsyJZbmgm4TD8FTfS3pcoC13lFo7V4U5kJOyFvEGqVAZLdT3p-VdxRIJ9qfbP8AlKrcIS4Znm-bq3c-iYqk7JfujalXBYgA_YltelAGrzFVjDMXcahFkaK71rGSFyaRv_O3xPqP3P9RG9Ga5ohx9vw.vCTJwd85Lq9TTslt.i6A8KSwPc6Nole_lwpo8pNDucVW0ENFbCSvTPajl3DpS0H6eb3C7ccyJYAT1Oi_s4RFtfluMHPOXl6CBKM_Qfj8RUQuLgnVBWj16CJeZm1fXvt0.wJKuy-m5GsNs4d_ecc2e5A');
    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());
    }
    }
}
5
  • There's likely not enough context here to provide you with any help. When discussing errors, it's helpful to include the full text of the error message along with the stack trace (which contains a list of line numbers and classes that led to the error). About the only thing that could throw an NPE here is cc.Patient_Relation__c.contains(), if Patient_Relation__c happens to be null. – Derek F Apr 13 '20 at 23:53
  • Yes it gives me error on that line.RadarFirst: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.RadarFirst: line 3, column 1 – Salman Shaik Apr 14 '20 at 0:02
  • I'm new in Apex, how do i do a null check on Patient_Relation__c ? on which line i should add? Thanks! – Salman Shaik Apr 14 '20 at 0:04
  • @SalmanShaik Please post code for RadarUpdate class to help with null pointer. Also please post the full error message. – Mohith Shrivastava Apr 14 '20 at 0:18
  • Also here is how you can handle null exception help.salesforce.com/… – Mohith Shrivastava Apr 14 '20 at 0:21
1

Null pointer exceptions occur when we try to call a method or access a variable on something that is null.

So when you run into an NPE, the first question you need to ask yourself is "what on this line can possibly be null?"

In this case, it's relatively easy. The thing that must be null here is the Patient_Relation__c field.

Once you figure out what is null, the next step is usually to figure out why it is null. Again, that's easy in this case. It's null because it's a field, and on the record your trigger is failing on, that field has no assigned value.

Finally, we can either fix the issue (go back to the source of the null data, and make sure it is not null) or mitigate the risk of having null data by performing a null check. In your case, mitigation is the appropriate approach.

A null check is really simple. Literally just if(variable == null) or if(variable != null). We can also rely on the fact that Salesforce will "short-circuit" the evaluation of a boolean statement.

In other words, if you perform your null check earlier in the statement than where you use the thing that could be null, you will be safe from an NPE.

if(variable != null && variable.contains('a')) will never evaluate the variable.contains('a') portion if variable is null because null != null is false and false && <anything> evaluates to false.

Since we know the entire statement will evaluate to false, we don't need to (and Salesforce, and many other languages as well, won't) evaluate the rest of the statement.

1
  • Thank you for explanation. Appreciate your help! – Salman Shaik Apr 14 '20 at 1:02
1

When a string has no value, it will appear as "null". If you try to call a method on a null object, you'll get a NullPointerException. To fix this, check for the null value first:

trigger RadarFirst on Patient_Satisfaction__c (before update) {
    for (Patient_Satisfaction__c cc : Trigger.new){
        if(cc.Patient_Relation__c != null && cc.Patient_Relation__c.contains('Referred to Privacy Office')){
            RadarUpdate.postcallout(cc.id);
        }
    }
}
1
  • Thank you!! That worked. Thats a good learning for me. – Salman Shaik Apr 14 '20 at 0:32

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.