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
RPalRPal 

Can someone please tell me how should I generate JSON for standard fields.

Can someone please tell me how should I generate JSON for standard fields. I get error When i do it for standard or lookup fields:

Opportunity Opp = Trigger.new[0] ; 
JSONGenerator gen = JSON.createGenerator(true);    
    gen.writeStartObject();      
    gen.writeStringField('Agency Code', Opp.Agency_Code__c);
    gen.writeStringField('Account',Opp.Account);
    gen.writeStringField('Implementation Contact',Opp.Implementation_Contact__c);
    gen.writeStringField('Implementation Contact Email',Opp.Implementation_Contact__c.Email);
    gen.writeStringField('Implementation Contact ID',Opp.Implementation_Contact__c.Record_ID_full__c);
    gen.writeStringField('Opportunity Owner',Opp.Owner);
    gen.writeStringField('Opportunity Owner Email',Opp.Owner.Email;
    gen.writeStringField('Last Modified By Name',Opp.LastModifiedBy);
    gen.writeStringField('Opportunity ID',Opp.Record_ID_Full__c);
    gen.writeStringField('Account',Opp.Account);
    gen.writeStringField('Account',Opp.Account);
    gen.writeEndObject();    
    String jsonS = gen.getAsString();
System.debug('jsonMaterials'+jsonS);
String endpoint = 'http://www.example.com/service';
HttpRequest req = new HttpRequest();
req.setEndpoint(endpoint);
req.setMethod('POST');
req.setbody(jsonS);
Http http = new Http();
HTTPResponse response = http.send(req);

All the custom fields on opprtunity are working just fine but the lookup fields and standard field shows syntax error.

User-added image
Can anyone please suggest?
Sandeep YadavSandeep Yadav
Hi Rpal
use JSON.serialize() method for generating JSON for standard field
RPalRPal
I am a newbie. Can you please suggest how should i use this method? i can't find this method in the developers guide:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_JsonGenerator.htm#apex_class_System_JsonGenerator