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
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.
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();
}
}