You need to sign in to do that
Don't have an account?
nununi
Account Records to JSON format
Hello! I would like to display my account records on a VF page in the following JSON format. I need help with the class and the VF page. Thanks a bunch!
[
{
"AccountId": 1,
"AccountName": "account1",
"AddressBook": [
{
"Id": 1,
"Name": "test",
"Attn": "test",
"StreetName": "test",
"CityName": "test",
"State": "TT",
"PostCode": 89098,
"Tel": "(000)- 000-0000",
"Favorite": true,
"AccountId": 1
},
{
"Id": 2,
"Name": "test2",
"Attn": "test2",
"StreetName": "test2",
"CityName": "test2",
"State": "TU",
"PostCode": 3000,
"Tel": "(000)- 000-0000",
"Favorite": true,
"AccountId": 1
},
]
},
{
"AccountId": 2,
"AccountName": "Account2",
"AddressBook": [
{
"Id": 1,
"Name": "Test3",
"Attn": "Test3",
"StreetName": "Test3",
"CityName": "Test3",
"State": "TI",
"PostCode": 3000,
"Tel": "(000)- 000-0000",
"Favorite": true,
"AccountId": 1
},
{
"Id": 2,
"Name": "Test4",
"Attn": "Test4",
"StreetName": "Test4",
"CityName": "Test4",
"State": "TO",
"PostCode": 75000,
"Tel": "(000)- 000-0000",
"Favorite": false,
"AccountId": 1
},
]
},
]
[
{
"AccountId": 1,
"AccountName": "account1",
"AddressBook": [
{
"Id": 1,
"Name": "test",
"Attn": "test",
"StreetName": "test",
"CityName": "test",
"State": "TT",
"PostCode": 89098,
"Tel": "(000)- 000-0000",
"Favorite": true,
"AccountId": 1
},
{
"Id": 2,
"Name": "test2",
"Attn": "test2",
"StreetName": "test2",
"CityName": "test2",
"State": "TU",
"PostCode": 3000,
"Tel": "(000)- 000-0000",
"Favorite": true,
"AccountId": 1
},
]
},
{
"AccountId": 2,
"AccountName": "Account2",
"AddressBook": [
{
"Id": 1,
"Name": "Test3",
"Attn": "Test3",
"StreetName": "Test3",
"CityName": "Test3",
"State": "TI",
"PostCode": 3000,
"Tel": "(000)- 000-0000",
"Favorite": true,
"AccountId": 1
},
{
"Id": 2,
"Name": "Test4",
"Attn": "Test4",
"StreetName": "Test4",
"CityName": "Test4",
"State": "TO",
"PostCode": 75000,
"Tel": "(000)- 000-0000",
"Favorite": false,
"AccountId": 1
},
]
},
]
[http://www.salesforce.com/us/developer/docs/dbcom_soql_sosl/Content/sforce_api_calls_soql_relationships.htm]
All Answers
and a controller that does
That should get you started. If you have any specific questions, please let me know.
NOTE: This code was not tested so it may have typographical errors. Also, it is just a snippet so the full controller and page will need to be written
public String getJsonData()
{
List<Account> accounts = [SELECT ID, Name, Account.Ship_To__r.ID, Account.Ship_To__r.Name, Account.Ship_To__r.Attention__c, Account.Ship_To__r.Street__c, Account.Ship_To__r.City__c, Account.Ship_To__r.State__c, Account.Ship_To__r.Zip__c, Account.Ship_To__r.Phone__c, Account.Ship_To__r.Account__c FROM Account LIMIT 3];
return JSON.serializePretty(accounts);
}
I get the error: Error: Compile Error: Didn't understand relationship 'Ship_To__r' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 5 column 30
List<Account> accounts = [SELECT ID, Name, Ship_Tos__r.ID,
Ship_Tos__r.Name,
Ship_Tos__r.Attention__c,
Ship_Tos__r.Street__c,
Ship_Tos__r.City__c,
Ship_Tos__r.State__c,
Ship_Tos__r.Zip__c,
Ship_Tos__r.Phone__c,
Ship_Tos__r.Account__c FROM Account LIMIT 3];
But I get the same error.
[http://www.salesforce.com/us/developer/docs/dbcom_soql_sosl/Content/sforce_api_calls_soql_relationships.htm]