0

I am getting error on the following lines

<apex:pageBlockTable value="{!npiCallout.Results}" var="rslt" width="100%">

Apex:-

**

public NPI_Wrapper_Class getNPIcallout() {
        
        string fname = phy.First_Name__c;
        string lname = phy.Last_Name__c;
        string state = phy.State__c;
        string orgname=phy.Org_Name_LBN_DBA_Former_LBN_or_Other__c;
       
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://npiregistry.cms.hhs.gov/api/?number=&enumeration_type=&first_name=' + fname + '*&last_name=' + lname + '*&organization_name='+orgname+'*&city=&state=&postal_code=&limit=100&pretty=on&version=2.1');
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        NPI_Wrapper_Class APIresult = NPI_Wrapper_Class.parse(response.getBody());
        
        for (NPI_Wrapper_Class.Results rslt: APIresult.results) {
            System.debug('Received the following NPI:');
            System.debug(rslt.number_Z);
        }
        return APIresult;
           
    }
2
  • 1
    Welcome to SFSE! please guide yourself and visit How to Ask. Not all code in your question is relevant to the problem you are facing, so please include the relevant code snippets only. Also, please specify exactly where you are facing the error. – Rahul Gawale Mar 30 at 7:02
  • Sure and thank you rahul – sai Mar 30 at 7:05
1

You really need to be checking the response status code - it's likely you are getting an error on that query, but since you are not checking, when you parse the response - it breaks because it's not in the JSON format you are expecting.

Check something like this:

if (response.getStatusCode() > 299 ) {

    //this is indicative only - you may want to do this differently.
    NPI_Wrapper_Class APIresult errResult = new NPI_Wrapper_Class APIresult();
    errResult.error = true;
    errResult.message = 'some message';
    return errResult
}

It's actually likely that you have not allowed this remote site in remote site settings - so check that first.

From the dev guide:

  • From Setup, enter Remote Site Settings in the Quick Find box, then
  • select Remote Site Settings.
  • Click New Remote Site.
  • Enter a descriptive term for the Remote Site Name.
  • Enter the URL for the remote site.
  • Optionally, enter a description of the site.
  • Click Save

Ref here

1
  • Hi Caspar, I have already added the url in the remote site settings. It is working fine when i am passing two parameters and it is throwing error if i pass three parameters public NPI_Wrapper_Class getNPIcallout() { request.setEndpoint('npiregistry.cms.hhs.gov/api/…' + fname + '*&last_name=' + lname + '*&organization_name='+orgname+'*&city=&state=&postal_code=&limit=100&pretty=on&version=2.1'); – sai Mar 30 at 10:40

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