0% found this document useful (0 votes)
27 views

Apex Execution Code

The document details the process of validating and creating an opportunity in the Atlas system from Salesforce. It includes validating required fields on the opportunity, generating a JSON payload, and making a request to the Atlas API to create or update the opportunity record.

Uploaded by

sakumar3195
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Apex Execution Code

The document details the process of validating and creating an opportunity in the Atlas system from Salesforce. It includes validating required fields on the opportunity, generating a JSON payload, and making a request to the Atlas API to create or update the opportunity record.

Uploaded by

sakumar3195
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

global class CreateOpportunityInAtlas{

String searchResponse;
webservice Static String ValidateLead(ID OpptyId)
{
String validmsg ='';
String searchResponse ='';
//boolean flag = false;
// boolean flag = false;
Opportunity OppObj = [SELECT Name,Account.Name,Account.Phone,Account.BillingS
treet,Account.BillingCity,Account.BillingState, Account.BillingCountry, Account.
BillingPostalCode, Account.Website, AtlasId__c, BusinessName__c, MccLoopkup__r.N
ame, leadsource, CreatedById,
GeneratedBy__c, AssisgnedSalesEmail__c, comments__c,(SELECT Contact.FirstNa
me, Contact.LastName, Contact.Email FROM OpportunityContactRoles), CreatedDate,
campaign.Name FROM Opportunity WHERE Id =: OpptyId];

// ******************************* VALIDATION START ************************


*************************************
if(OppObj.Account.Name == NULL || OppObj.Account.Phone==NULL || OppObj.Acc
ount.BillingCity== NULL || OppObj.Account.BillingState== NULL || OppObj.Account.
BillingCountry== NULL || OppObj.Account.BillingStreet== NULL || OppObj.MccLoopku
p__r.Name== NULL || OppObj.Account.BillingPostalCode== NULL || OppObj.Opportunit
yContactRoles.size()== 0 || OppObj.OpportunityContactRoles.get(0).contact.FirstN
ame== NULL || OppObj.OpportunityContactRoles.get(0).contact.LastName == NULL ||
OppObj.OpportunityContactRoles.get(0).contact.Email == NULL)
{
if(OppObj.Account.Name==NULL)
{
validmsg += 'Account Name/ Business Name';
}
else if(OppObj.Account.Phone ==NULL)
{
validmsg += ' Account Phone';
}
else if(OppObj.Account.BillingCity == NULL)
{
validmsg += 'Account City';
}
else if(OppObj.Account.BillingState == NULL)
{
validmsg += 'Account State ';
}
else if(OppObj.Account.BillingCountry == NULL)
{
validmsg += 'Account Country ';
}
else if(OppObj.Account.BillingStreet == NULL)
{
validmsg += 'Account Street ';
}
else if(OppObj.Account.BillingPostalCode== NULL)
{
validmsg += 'Account Zip Code';
}
else if(OppObj.MccLoopkup__r.Name== NULL)
{
validmsg += ' MCC code ';
}
else if(OppObj.OpportunityContactRoles.size()== 0)
{
validmsg += ' Contact Record';
// flag = true;
}
else if(OppObj.OpportunityContactRoles.size()!= 0)
{
if(OppObj.OpportunityContactRoles.get(0).contact.FirstName== NULL || O
ppObj.OpportunityContactRoles.get(0).contact.LastName == NULL || OppObj.Opportun
ityContactRoles.get(0).contact.Email == NULL)
{
validmsg += ' Contact Details First/Last Name or Email ';
}
}
validmsg += ' is Missing Please Check';
// return validmsg;
searchResponse = '{"Validation":"invalid","ValidationErrorMsg":"'+valid
msg+'"}';
return searchResponse;
}
//*************************************VALIDATION ENDS ********************
***********************************************
//********************************* CONNECT TO ATLAS SERVER ***************
***********************************************
String AtlasEndPointURL='';
String AtlasHeaderKeyValue='';
// boolean flag = false;
Map<String, AtlasRestServiceConfig__c> mapAtlasSSettings = AtlasRestServiceC
onfig__c.getAll();
if (mapAtlasSSettings.containsKey('AtlasEndPointURLLeadCreate'))
{
AtlasEndPointURL = mapAtlasSSettings.get('AtlasEndPointURLLeadCreate').Va
lue__c;
}
if (mapAtlasSSettings.containsKey('AtlasHeaderKeyValue'))
{
AtlasHeaderKeyValue= mapAtlasSSettings.get('AtlasHeaderKeyValue').Value__
c;
}
system.debug('AtlasHeaderKeyValue:'+AtlasHeaderKeyValue);
system.debug('AtlasEndPointURL:'+AtlasEndPointURL);
system.debug('I am now tring to create HttpRequest');
HttpRequest req = new HttpRequest();
req.setEndpoint(AtlasEndPointURL);
req.setMethod('PUT');
req.setHeader('X-Hps-App-Token',AtlasHeaderKeyValue);
req.setHeader('Content-Type', 'application/json');
System.debug('Creating Request in process setting up body' +req);
//****************************Generate JSON using JSONGenerator Class********
**********************************************
JsonGenerator gen = JSON.createGenerator(true);
System.debug('JSON is Triggered' +gen);
System.debug('Lead Atlas Id' +OppObj.AtlasId__c);
gen.writeStartObject();
if(OppObj.AtlasId__c!= NULL)
{
System.debug('I am into Lead Atlas Id ' +OppObj.AtlasId__c);
gen.writeStringField('AtlasId', OppObj.AtlasId__c);
System.debug('AtlasId is added ');
}
else
{
gen.writeStringField('AtlasId', '');
System.debug('AtlasId is added ');
}
// For Business Name
gen.writeStringField('BusinessName',OppObj.Account.Name);
System.debug('BusinessName is added');
// For MCC Code Lookup
gen.writeStringField('MccCode', OppObj.MccLoopkup__r.Name);
system.debug('MccCode is added');
if(OppObj.LeadSource != NULL)
{
gen.writeStringField('LeadSource', OppObj.LeadSource );
system.debug('Lead Source is added');
}
else
{
gen.writeStringField('LeadSource', '');
system.debug('Lead Source is added');
}
if(OppObj.campaign.Name!= NULL)
{
gen.writeStringField('CampaignName', OppObj.campaign.Name);
System.debug('Campaign Name is added');
}
else
{
gen.writeStringField('CampaignName', '');
System.debug('Campaign Name is added');
}
//Since Generated By is always Null
gen.writeStringField('GeneratedBy', '');
system.debug('GeneratedBy is added');

// Opportunity Created date


DateTime dT = OppObj.CreatedDate;
Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());
gen.WriteDateField('CreatedOn', myDate);
system.debug('Created On is added');
// For Contact first name
gen.writeStringField('ContactFirstName', OppObj.OpportunityContactRol
es.get(0).Contact.FirstName);
System.debug('Contact FirstName is added');
// for Contact Last name
gen.writeStringField('ContactLastName', OppObj.OpportunityContactRoles
.get(0).Contact.LastName);
system.debug('ContactLastName is added');
// For Phone Field
String fphone = OppObj.Account.Phone.replaceAll('\\D','');
gen.writeStringField('PhoneNumber', fphone);
system.debug('Phone Number is added');
// For Contact Email
gen.writeStringField('EmailAddress', OppObj.OpportunityContactRoles.ge
t(0).contact.Email);
System.debug('EmailAddress is added');
// Website Field
if(OppObj.Account.Website != NULL)
{
gen.writeStringField('Website', OppObj.Account.Website);
system.debug('Lead Website is added');
}
else
{
gen.writeStringField('Website', '');
system.debug('Lead Website is added');
}
// For Billing Street
gen.writeStringField('Address1', OppObj.Account.BillingStreet);
system.debug('Address1 is added');
// For Address Line 2
gen.writeStringField('Address2', '' );
system.debug('Lead Address2 is added');
//For Billing City
gen.writeStringField('City', OppObj.Account.BillingCity);
System.debug('Lead City is added');
// Billing State
gen.writeStringField('State', OppObj.Account.BillingState);
System.debug('Lead State is added');
//For billing Postal Code
gen.writeStringField('Zipcode', OppObj.Account.BillingPostalCode);
system.debug('Lead ZipCode is added');
if(OppObj.AssisgnedSalesEmail__c != NULL)
{
gen.writeStringField('AssisgnedSalesEmail', OppObj.AssisgnedSalesEmail
__c);
system.debug('AssisgnedSalesEmail is added');
}
else
{
gen.writeStringField('AssisgnedSalesEmail', '');
system.debug('AssisgnedSalesEmail is added');
}
if(OppObj.comments__c != NULL)
{
// For comments Field
gen.writeStringField('Comments', OppObj.comments__c);
System.debug('Comments is added');
}
else
{
gen.writeStringField('Comments', '');
System.debug('Comments is added');
}
// Adding AffiliateCodes Field
gen.writeFieldName('AffiliateCodes');
system.debug('AffiliateCodes is added');
//Array for AffiliateCodes Started
gen.writeStartArray();
System.debug('AffiliateCodes ended');
gen.writeEndArray();
//end of the parent JSON object
System.debug('Calling json As String');
String jsonString = gen.getAsString();
System.debug('jsonString:'+jsonString);
//*************************************END OF JSON GENERATOR *****************
*****************************************************
req.setBody(jsonString); //Generate JSON using JSONGenerator Class
req.setTimeout(50000);
Http http = new Http();
system.debug('Request is ' +req);
HTTPResponse response = http.send(req);
System.debug('Response List is ' +response);

// Parse JSON
JSONParser parser = JSON.createParser(response.getBody());
while (parser.nextToken() != null) {
if (parser.getCurrentToken() == JSONToken.FIELD_NAME){
String fieldName = parser.getText();
parser.nextToken();
if(fieldName == 'LeadId'){
String LeadId = parser.getText();
if(LeadId != NULL)
{
OppObj.LeadId__c = parser.getText();
system.debug('Lead Id is ' +parser.getText());
}
}
else if(fieldName == 'CorrelationId'){
String CorrelationId = parser.getText();
if(CorrelationId != NULL)
{
OppObj.CorrelationId__c = parser.getText();
System.debug('Correlation Id is ' + parser.getText());
}
}
}
}
if(response.getStatusCode()==200)
{
AtlasResponse__c AtRes = new AtlasResponse__c();
AtRes.HTTPResponse_Message__c = response.getBody();
AtRes.Lead_Id__c = OppObj.Id;
AtRes.Lead_Name__c = OppObj.Name;
AtRes.Object_Name__c = 'Oportunity Object';
AtRes.Operation_Method_Invoked__c = 'Opportunity === Lea
dCreateInAtlas()';
AtRes.Status__c = response.getStatus();
AtRes.StatusCode__c = response.getStatusCode();
AtRes.Opportunity__c = OppObj.Id;
Insert AtRes;
OppObj.AtlasResponse__c = AtRes.Id;
update OppObj;
// searchResponse = '{"Validation":"valid","StausCode":200
,"Status":"OK","AtlasResponseID":"'+AtRes.ID+'","Msg":"callout sucess"}';
searchResponse = '{"Validation":"valid", "Message": "call
out sucess"}';
System.debug('Atlas Search Response is ' +searchResponse)
;
}
else
{
AtlasResponse__c AtRes = new AtlasResponse__c();
AtRes.HTTPResponse_Message__c = response.getBody();
AtRes.Name = OppObj.AtlasId__c;
AtRes.Lead_Id__c = OppObj.Id;
AtRes.Lead_Name__c = OppObj.Name;
AtRes.Object_Name__c = 'Opportunity Object';
AtRes.Operation_Method_Invoked__c = 'Opportunity =======
LeadCreateInAtlas()';
AtRes.Status__c = response.getStatus();
AtRes.StatusCode__c = response.getStatusCode();
AtRes.Opportunity__c = OppObj.Id;
Insert AtRes;
OppObj.AtlasResponse__c = AtRes.Id;
update OppObj;
searchResponse = '{"Validation":"valid", "Message": "call
out Error"}';
System.debug('Atlas Search Response is ' +searchResponse)
;
}
return searchResponse;
}
}

You might also like