0

I've been trying to create a List from the Account object, filtered by their record type, to be sent to our website using the Apex Class. I managed to create the query correctly and it returned the organisations that I wanted, and I managed to send the result of that query to our endpoint using the below code:

public class WPPost {
public static HttpResponse WPPost() {
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint('the_endpoint');
    request.setMethod('POST');
    request.setHeader('Content-Type', 'application/json;charset=UTF-8');
    request.setBody(JSON.serialize(Query.findAll()));
    HttpResponse response = http.send(request);
    System.debug(response); 
    return response;
}

The below is the query code itself in Apex Class.

public class Query {
public static List<Account> findAll(){
        List<Account> results = (List<Account>) [SELECT Id,Name,Public_Description__c,Website,ShippingCountry, FROM Account WHERE RecordTypeId='0120I000000ygO6QAI'];
return results;
}

Despite the POST itself being successful, the format of the result that I got in the endpoint wasn't what I expected:

(
[0] => [{"attributes":{"type":"Account","url":"/services/data/v45.0/sobjects/Account/0010w000005QkRNAA0"},"Id":"0010w000005QkRNAA0","Name":"Member 2","Public_Description__c":"
Lorem Ipsum Dolor si amet Numero 2

","Website":"www.membernumber2.asia","ShippingCountry":"Indonesia","RecordTypeId":"0120I000000ygO6QAI","CurrencyIsoCode":"USD"},{"attributes":{"type":"Account","url":"/services/data/v45.0/sobjects/Account/0010w000005QvtqAAC"},"Id":"0010w000005QvtqAAC","Name":"Member 4","Website":"www.membernumero4.asia","ShippingCountry":"Philippines","RecordTypeId":"0120I000000ygO6QAI","CurrencyIsoCode":"USD"},{"attributes":{"type":"Account","url":"/services/data/v45.0/sobjects/Account/0010w000005Qki9AAC"},"Id":"0010w000005Qki9AAC","Name":"Member 3","Public_Description__c":"
Lorem Ipsum Dolor si amet Numero 3

","Website":"www.membernumber3.asia","ShippingCountry":"Cambodia","RecordTypeId":"0120I000000ygO6QAI","CurrencyIsoCode":"USD"},{"attributes":{"type":"Account","url":"/services/data/v45.0/sobjects/Account/0010w000005QkIHAA0"},"Id":"0010w000005QkIHAA0","Name":"Member 1","Public_Description__c":"
Lorem Ipsum Dolor si met Numero 1

","Website":"www.membernumber1.asia","ShippingCountry":"Singapore","RecordTypeId":"0120I000000ygO6QAI","CurrencyIsoCode":"USD"}]
)

I'm really unsure what else to change since I thought using JSON.serialize would help me get the format that I need, which is something like:

{
   "Id":"0010w000005QkIHAA0"
   "Name":"Member 1"
   "Public_Description__c":"Lorem Ipsum Dolor si met Numero 1"
   "Website":"www.membernumber1.asia"
   "ShippingCountry":"Singapore"
   "RecordTypeId":"0120I000000ygO6QAI"
}

I wonder if anyone can help me realise if the wrong part was my Apex code or something else...?

2
  • Can you try setting header Accept? request.setHeader('Accept', 'application/json');
    – kvor
    Commented Apr 26, 2019 at 10:15
  • Are you expecting a single record? Your Apex Code is clearly returning a list of records, which you appear to be parsing in PHP. I think it would be beneficial if you described what your end goal is. Can you please edit your question to include more information?
    – sfdcfox
    Commented Apr 26, 2019 at 11:51

1 Answer 1

0

What you're seeing is normal when serializing an SObject.

The only difference between what you're seeing, and what you want is that the serialization is including "attributes":{...}. This extra information helps Salesforce determine what kind of SObject you're working with (in case you ever want to deserialize back into an SObject).

It's been a while since I did any work in PHP, but I suspect the [0] => <JSON string> is just PHP sticking the entirety of your JSON into the $_POST superglobal array. PHP isn't auto-deserializing (which sounds like a terrible idea from a security standpoint), so you only have the one item in the array.

Honestly, I don't think there's anything that you really need to do here. PHP's json_decode() should be able to take what you have here, and you can just simply pretend that deserialized[0]->attributes can't be used.

If you just can't bring yourself to ignore the attributes, then the easiest route to go would probably be to make a simple inner class to hold your data as Strings.

You must log in to answer this question.

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