-2
  1. I'm new in Apex and we have a requirement, Need to send a record from Salesforce to an external system using Rest API whenever a record is created in Salesforce, I need to send a couple of SF fields to the external system along with the External system required field's

  2. I created a class that has both JSON generator and HTTP post method however this code is not with only one record, 3.I'm not sure how to create a JSON generator in the apex, struggling with JSON generator for making an HTTP request.

  3. I don't whether I made the correct JSON generator. can you please suggest me easy method with the below JSON body as JSON generator.

I have tried below it throws error that Can not start an object, expecting fields name

JSON data

{
  "CustomerID": 0,
  "CustomerType": 1,
  "Name": "callum, john",
  "BranchID": 1,
  "CustomerAddress": [
    {
      "CustomerAddressID": 0,
      "CustomerID": 0,
      "Address": {
        "Address1": "Test",
        "TownCityID": 41037,
        "CountyID": null,
        "StateID": 41036,
        "CountryID": 4,
        "Country": "Australia"
      },
      "AddressTypeID": 1,
      "DateMovedIn": "11/09/2019",
      "IsPrimary": false
    }
  ],
  "Persons": [
    {
      "PersonID": 0,
      "Title": 1,
      "Gender": 0,
      "ForenamePartOne": "callum",
      "ForenamePartTwo": null,
      "ForenamePartThree": null,
      "Surname": "john",
      "DateOfBirth": "11/09/1970",
      "SocialSecurityNumber": "35",
      "telephone1": "",
      "telephone2": "",
      "telephone3": "6598307315"
    }
  ],
  "CorrespondenceName": "callum, john",
  "DateJoined": "11/09/2019"
}

Apex class

public class API {
    private String recId;
    @InvocableMethod(label = 'state') public static void Stagingsapi(List < id > appid) {
        abacuStagingsapiFuture(appid);
    }
    public static void StagingsapiFuture(List < Id > appId) {
            {
                try {
                    Test__c app = [select Id, Name, Contact__r.FirstName, Contact__r.LastName, Contact__r.MobilePhone, Contact__r.MailingPostalCode, Contact__r.MailingCountry, Contact__r.MailingCity, Contact__r.MailingState, Contact__r.Birthdate, Contact__r.Gender__c, Contact__r.MailingStreet, Loan_State__c, CCR_Report_SOAP__c, Test__c.Contact_Name__cfrom Test__c WHERE Id IN: appId];
                    JSONGenerator gen = JSON.createGenerator(true);
                    gen.writeStartObject();
                    gen.writeStringField('CustomerID', '0');
                    gen.writeStringField('CustomerType', ' 1');
                    gen.writeStringField('Name', app.Contact_Name__c, app.Contact__r.LastName);
                    gen.writeStringField('BranchID', '1');
                    gen.writeFieldName('CustomerAddress');
                    gen.writeStringField('Address1', 'test');
                    gen.writeStringField('TownCityID', '41037');
                    gen.writeStringField('CountyID', 'null');
                    gen.writeStringField('StateID', '41036');
                    gen.writeStringField('CountryID', '4');
                    gen.writeStringField('Country', 'vfdssds');
                    gen.writeStringField('AddressTypeID', '1');
                    gen.writeStringField('DateMovedIn', '11/09/2019');
                    gen.writeStringField('IsPrimary', 'false');
                    gen.writeFieldName('Persons');
                    gen.writeStringField('PersonID', '0');
                    gen.writeStringField('Title', '1');
                    gen.writeStringField('Gender', '0');
                    gen.writeStringField('ForenamePartOne', app.Contact__r.FirstName);
                    gen.writeStringField('ForenamePartTwo', 'null');
                    gen.writeStr ingField('ForenamePartThree', 'null');
                    gen.writeStringField('Surname', app.Contact__r.LastName);
                    gen.writeStringField('DateOfBirth', '1/09/1970');
                    gen.writeStringField('SocialSecurityNumber', '35');
                    gen.writeStringField('telephone1', '');
                    gen.writeStringField('telephone1', '');
                    gen.writeStringField('telephone1', '6598307315');
                    gen.writeStringField('CorrespondenceName', app.Contact__r.LastName, Contact__r.LastName);
                    gen.writeStringField('DateJoined', '11/09/2019');
                    gen.writeEndObject();
                    String jsonData = gen.getAsString();
                    System.debug('jsonData-' + jsonData);
                    string loginbody = '{' + ' "username": "administrator  ",' + ' "password": "whatever",' + '    "deviceId": "a12bc"' + '}';
                    String loginendpoint = 'https://xxxx.staging.sample.com/api/login';
                    Http http1 = new Http();
                    HttpRequest request1 = new HttpRequest();
                    HttpResponse res1 = new HttpResponse();
                    request1.setEndpoint(loginendpoint);
                    request1.setMethod('POST');
                    request1.setHeader('Accept', 'application/json');
                    request1.setHeader('Content-Type', 'application/json; charset=utf-8');
                    request1.setBody(gen.getAsString());
                    request1.setTimeout(40000);
                    HttpResponse response1 = http1.send(request1);
                    res1 = http1.send(request1);
                    String GetToken = '';
                    String resp = response1.getBody();
                }
            }
9
  • Please format your code so that it is not on a single line. It's impossible to read.
    – David Reed
    Jul 20 at 16:16
  • 1
    I've linked a canonical question that covers typical approaches to serializing and deserializing JSON.
    – David Reed
    Jul 20 at 16:17
  • 1
    Please do not delete questions and then later recreate nearly identical posts. You should instead undelete the original post and make any edits there. It's too late now, but doing it that way instead is a lot more likely to avoid getting your account banned from asking further questions.
    – Adrian Larson
    Jul 20 at 16:31
  • Okay thanks for corner...
    – Catherine
    Jul 20 at 16:36
  • Hi @Davin, can you please modify the above json data as json generator.. Because am struggling on it.. I don't know what to do for array braces
    – Catherine
    Jul 20 at 16:39
2

for question #3: For an easy way to get basic JSON to an APEX parser you can save some time and start with http://json2apex.herokuapp.com/ and that should get you a working starting point and you can then modify it for your specific needs.

1

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