0

I am following this simple example

List<Account> accounts = [SELECT id, name FROM Account LIMIT 2];
String accountsJSON = JSON.serializePretty(accounts);

I am trying to replicate this exactly but am unable to properly serialize my list. Is it not possible to serialize a custom list? I see that they are using accounts and this, to my knowledge, is the only difference between what I am trying to do and what they did. Any insight would be appreciated.

HTML

<apex:commandButton action="{!pullReport}" value="Pull Report" />

My Code:

public class sendToWebService 
{

public static void pullReport()
{
    getAuth();
    getAccounts();
    reportDate = Datetime.now();
    timeStamped = reportDate;
    helloKitty = getAccounts();
    saveDateToString();

    compName = new List<Doc_Formulas__c>();
    compName = [SELECT Fed_Tax_ID__c,Legal_Name__c,Physical_Address_1__c,Physical_City__c,Physical_State__c,Physical_Zip__c,Signor_City__c,Signor_First_Name__c,Signor_Home_Phone__c,Signor_Last_Name__c,Signor_SSN__c,Signor_State__c,Signor_Street_Address__c,Signor_Zip_Code__c 
                FROM Doc_Formulas__c WHERE Id = '123456789654321'];
    system.debug('this is the list' + compName);

    for (Doc_Formulas__c cn : compName) {
        companyname = cn.Legal_Name__c;
        system.debug('this is the company name: ' + companyname);
    }

    JSONString = JSON.serialize(compName); 
    system.debug('this is the JSON String: ' + JSONString);
}

public static string getAccounts()
{
    HttpRequest req = new HttpRequest();
    req.setEndpoint('https://thisistheendpoint.com');
    Http h = new Http();
    req.setMethod('POST');
    req.setBody(JSONString);
    req.setHeader('Content-Type', 'application/json');
    req.setHeader('Content-Type', 'application/json');
    string d = getAuth();
    string e = 'token ';
    string f = e + d;
    req.setheader('Authorization', f );
    try
    {
        HttpResponse res = h.send(req);
        jsonStr = res.getBody();
        return jsonStr;
    }
    catch(Exception z)
    {
        system.debug('this is the system debug for excception z: ' + z);
        return null;
    }
}

Error Message:

System.NullPointerException: Argument 1 cannot be null

I should add that it works fine when I type in the JSON string like so:

  req.setBody('{"CompanyLegalName": "test","FedTaxId": "123-12-1234","PhysicalAddress1": "123 Main St.","PhysicalAddressCity": "Encinitas","PhysicalAddressState": "MA","PhysicalAddressZip": "111111"}');
  • Can you please add any error messages to your post verbatim? You cannot declare anything static within a method and I suspect that's your issue. – Adrian Larson Sep 27 '16 at 21:27
  • We need to see how you call these two methods. – Adrian Larson Sep 27 '16 at 21:33
  • @AdrianLarson I just put in the static to show that JSON was defined. I actually define it earlier. I updated with the correct code. – Olivia Sep 27 '16 at 21:33
  • I call them with a button called 'pullReport'. It works fine with a hard typed string. I will update with the button code as well. – Olivia Sep 27 '16 at 21:34
  • But what does that button do? How are you calling getAccounts? Your definition of the latter method does not guarantee that a value is set for JSONString. – Adrian Larson Sep 27 '16 at 21:35
0

Figured it out. The JSONString needed to be included inside of the getAccounts() method along with the query list. For some reason it was having difficulty reading the serialized string in the other method even though it is static.

    public static string getAccounts()
    {
        compName = new List<Doc_Formulas__c>();
        compName = [SELECT Fed_Tax_ID__c,Legal_Name__c,Physical_Address_1__c,Physical_City__c,Physical_State__c,Physical_Zip__c,Signor_City__c,Signor_First_Name__c,Signor_Home_Phone__c,Signor_Last_Name__c,Signor_SSN__c,Signor_State__c,Signor_Street_Address__c,Signor_Zip_Code__c 
                    FROM Doc_Formulas__c WHERE Id = '123456789456'];
        system.debug('this is the list' + compName);
        JSONString = JSON.serialize(compName); 
        system.debug('this is the JSON String: ' + JSONString);
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://testendpoint.com');
        Http h = new Http();
        req.setMethod('POST');
        req.setBody(JSONString);........

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.