Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I JSON.serialize an opportunities line items?

I need to create a single json file, that holds an oppty as well as it's line items.

If I just JSON.serialize(opportunity), I get all the Oppty's fields, but none of the line item data

"Probability": 100,
"IsWon": true,
"Type": "New Account - FIRST  (4 more) ...",
"OwnerId": "00AAI",
"QTY_of_Licences__c": 0,
"sfcloud__Expected_Re (8 more) ...": 2000.00,
"LastModifiedDate": "2014-03-10T13:12:54.000Z",
"Data_Quality_Score__ (1 more) ...": 60,
"support_end_date__c": "2015-03-05T00:00:00.000Z",
"Pricebook2Id": "01sD000000IAU",
"IsPrivate": false,
"testte__c": "2014-04-04T00:00:00.000Z",
"IsClosed": true,
"Hidden_Courier_URL__ (1 more) ...": "http://www.tex.c (2 more) ...",
"SystemModstamp": "2014-03-10T13:12:54.000Z",
"ForecastCategoryName": "Closed",
"ForecastCategory": "Closed",
"StageName": "Closed Won",
"Amount": 2000.00,
"CurrencyIsoCode": "USD",
"sfcloud__Bundle_Amou (5 more) ...": 0.00,
"LastModifiedById": "005200000I",
"sfcloud__Total_Amoun (4 more) ...": 2000.00,
"Direct_Sale__c": true,
"Payment_Due_Date__c": "2014-03-09T00:00:00.000Z",
"PO_Processed_Date__c": "2014-03-05T00:00:00.000Z",
"ExpectedRevenue": 2000.00,
"Opportunity_Number__ (1 more) ...": "201403-13167",
"Order_Processed__c": true,
"TotalOpportunityQuantity": 1.00,
"Name": "new name test",
"HasOpportunityLineIt (2 more) ...": true,
"Payment_Terms__c": "In Advance",
"Grand_Total__c": 2000.00,
"AccountId": "001M000000W1",
"CreatedById": "005200I",
"CreatedDate": "2014-03-05T09:58:59.000Z",
"VAT_Total__c": 0.00,
"CST_Total__c": 0.00,
"Opportunity__c": 1,
"Taxes_total__c": 0.00,
"IsDeleted": false,
"Id": "006M000Q",
"Data_Quality_Descrip (7 more) ...": "Missing: Lead Source (12 more) ...",
"CloseDate": "2014-03-05T00:00:00.000Z"

The only similar pair is:

"TotalOpportunityQuantity": 1.00

I need these as an array in my JSON that I send out.

so, how do I go by this?

share|improve this question
Did you query the line items ? – Samuel De Rycke Mar 10 '14 at 13:39
No I did not query it. – Saariko Mar 10 '14 at 13:46
@Saariko So that was an answer. Please, post your results when you are ready. – Andrew Muza Mar 10 '14 at 13:58
up vote 8 down vote accepted

If you do not explicitly query relation data, it will not be present in the sObject within apex, and thus not show up in the json version. If the data has been queried, the apex serialize method will include it.

Simplified example:

opportunity op = [SELECT id, (SELECT id
                              FROM opportunitylineitems) 
                 FROM opportunity 
                 WHERE name = 'TESTopp']; //dummy example in my dev org


this results:

    "attributes": {
        "type": "Opportunity",
        "url": "/services/data/v29.0/sobjects/Opportunity/006d0000006b2Iuxxx"
    "OpportunityLineItems": {
        "totalSize": 2,
        "done": true,
        "records": [{
            "attributes": {
                "type": "OpportunityLineItem",
                "url": "/services/data/v29.0/sobjects/OpportunityLineItem/00kd0000004u29xxxx"
            "OpportunityId": "006d0000006b2xxxx",
            "Id": "00kd0000004u2xxxx"
        }, {
            "attributes": {
                "type": "OpportunityLineItem",
                "url": "/services/data/v29.0/sobjects/OpportunityLineItem/00kd000000BVrdxxxx"
            "OpportunityId": "006d0000006b2xxx",
            "Id": "00kd000000BVrdxxx"
    "Id": "006d0000006b2Iuxx"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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