function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Brent WhitneyBrent Whitney 

Extracting fields from a trigger to create a custom JSON

I have created a trigger for Order Object and only want specific fields to be pulled from that object to creat a custom JSON data structure. I have tried saving the fields that I need from the object into local variables, placing them into a list and then serializing them but the output is wrong. The curly brackets are missing and commas are not being added in the proper locations. Please reference my code below.
 
trigger ActiveOrder on Order (after insert, after update) 
{
            
/***********************************************************************/
/*          *Order Object* variables decleration Start:			   	   */ 
/***********************************************************************/

   	private String ordAccountId;
    private String ordBillingStreet;
    private String ordBillingCity;
    private String ordBillingState;
    private String ordBillingPostalCode;
    private String ordBillingCountry;
    private String ordDescription;
    private Double ordTotalAmount;
    private String ordOrderNumber;
    private String ordShipToContact;
    private String ordShipToMethod;
    private String ordShippingStreet;
    private String ordShippingCity;
    private String ordShippingState;
    private String ordShippingPostalCode;
    private String ordShippingCountry;
    private String ordBillToAttn;
    private String ordCustomerPO;
    private String ordShipToAttn;
            
/***********************************************************************/
/*  		*Order Object* variables declearation End:				   */ 
/***********************************************************************/

    for(Order o : Trigger.New)
    {
        
        If(o.Status == 'Activated')
        {
            
            ordAccountId = o.AccountId;
            ordBillingStreet = o.BillingStreet;
            ordBillingCity = o.BillingCity;
            ordBillingState = o.BillingState;
            ordBillingPostalCode = o.BillingPostalCode;
            ordBillingCountry = o.BillingCountry;
            ordDescription = o.Description;
            ordTotalAmount = o.TotalAmount;
            ordOrderNumber = o.OrderNumber;
            ordShipToContact = o.Ship_To_Name__c;
            ordShippingStreet = o.ShippingStreet;
            ordShippingCity = o.ShippingCity;
            ordShippingState = o.ShippingState;
            ordShippingPostalCode = o.ShippingPostalCode;
            ordShippingCountry = o.ShippingCountry;
            ordBillToAttn = o.BillToAttn__c;
            ordCustomerPO = o.Customer_PO__c;
            ordShipToAttn = o.Ship_to_Attention__c;
            
            
            List<String> ordInvoice = new List<String>();
            

			ordInvoice.add('Account Id: ' + ordAccountId + ', ' 
                           + 'Billing Street: ' + ordBillingStreet + ', ' 
                           + 'Billing City: ' + ordBillingCity + ', ' 
                           + 'Billing State: ' + ordBillingState + ', ' 
                           + 'Billing Postal Code: ' + ordBillingPostalCode + ', ' 
                           + 'Billing Country: ' + ordBillingCountry + ', ' 
                           + 'Description: ' + ordDescription + ', ' 
                           + 'Total Amount: ' + ordTotalAmount + ', ' 
                           + 'Order Number: ' + ordOrderNumber + ', ' 
                           + 'Ship To Contact: ' + ordShipToContact + ', ' 
                           + 'Shippin Street: ' + ordShippingStreet + ', ' 
                           + 'Shipping City: ' + ordShippingCity + ', ' 
                           + 'Shipping State: ' + ordShippingState + ', ' 
                           + 'Shipping Postal Code: ' + ordShippingPostalCode + ', ' 
                           + 'Shipping Country: ' + ordShippingCountry + ', ' 
                           + 'Bill To Attn: ' + ordBillToAttn + ', ' 
                           + 'Customer PO: ' + ordCustomerPO + ', ' 
                           + 'Ship To Attn: ' + ordShipToAttn);

            System.debug('Pre JSON: ' + ordInvoice);
            
            
            String ordInvoiceJSON = JSON.serialize(ordInvoice);
            
            System.debug('Post JSON: ' + ordInvoice);
            System.debug('Post JSON: ' + ordInvoicePostJSON);

            
        }
    }
}

Any help would be much appreciated. Thank you all in advance.
shailesh_rawteshailesh_rawte
Hi Brent,

The data which you trying to serialize that is in string format, serializing generate json properly when your data in key value pair like wrapper  etc. if you don't want to go with wrapper you need to prepare a string in JSON format.

Let me know if you want example.

Regards,
Shailesh Rawte
Danish HodaDanish Hoda
Hey Brent,
For creating JSON, you need Map.

You can do something like --

string str = 'Account Id';
Map<String, String> mapJson = new Map<String, String>();
mapJson.put(str, ordAccountId);
.
.
.
and others.