Skip to content

Instantly share code, notes, and snippets.

@leehildebrand
Last active February 1, 2016 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leehildebrand/f3d97ad9f9631ee73213 to your computer and use it in GitHub Desktop.
Save leehildebrand/f3d97ad9f9631ee73213 to your computer and use it in GitHub Desktop.
Car Auction API Chatter Action Controller
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Created By: Lee Hildebrand (http://leehildebrand.name)
//Created Date: 12/23/14
//Purpose: Use Lead data to connect to ARD web services and retrieve a "quote"
// then create either as "scrap" or "non-scrap assignment
//Revised:
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public with sharing class QuoteController {
private final Lead lead;
public String selectedService {get; set;}
public boolean quote_just_created {get; set;}
public QuoteController (ApexPages.StandardController stdController){
lead = (Lead)stdController.getRecord();
}
public void Get_Quote(){
if([SELECT COUNT() FROM ARD_Quote__c WHERE Lead__c = :lead.Id]==0){
ARD_Quote__c quote = new ARD_Quote__c(Lead__c=lead.Id);
API__C api = API__c.getValues('VPSQuote');
HttpRequest req = new HttpRequest();
//-------------------------------------------HTTP Connection Settings----------------------------------------//
req.setMethod('GET');
req.setTimeout(120000);
req.setEndpoint(api.Endpoint__c+'?VendorKey='+api.Vendor_Key__c+'&ZIPCode='+lead.Pickup_Zip__c+'&Make='+lead.Make__c+'&Model='+lead.Model__c+'&Year='+lead.Year__c);
String response = '';
try {
//capture response
response = new Http().send(req).getBody();
} catch(System.Exception e) {
//if no connection, display error
System.debug('Callout error: '+ e);
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Error,'There was an error reaching the VPS quote web service: '+e));
}
finally{
try{
//process response
System.debug('ARD said: '+ response);
JSONParser parser = JSON.createParser(response);
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)){
String fieldName = parser.getText();
parser.nextToken();
if(fieldName == 'ARDBidID')
quote.Name = parser.getText();
if(fieldName == 'Quote')
quote.Amount__c = Decimal.ValueOf(parser.getText());
}
}
} catch(System.Exception e) {
System.debug('JSON Parse error: '+ e);
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Error,'Wait a minute! VPS says: "'+response+'". Check to make sure your lead has the right data.'));
}
if(quote.Amount__c!=null){
lead.ARD_Quote__c = quote.Name;
lead.ARD_Price__c = quote.Amount__c;
update lead;
insert quote;
quote_just_created = true;
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Confirm,'A quote for $'+quote.Amount__c+' was created with BidID: '+quote.Name));
}
}
}
}
public void Assign(){
ARD_Quote__c quote = [SELECT Name,Amount__c FROM ARD_Quote__c WHERE Lead__c = :lead.Id LIMIT 1];
API__C api = API__c.getValues(selectedService);
HttpRequest req = new HttpRequest();
//-------------------------------------------HTTP Connection Settings----------------------------------------//
req.setMethod('GET');
req.setTimeout(120000);
String endpoint = api.Endpoint__c+'?VendorKey='+api.Vendor_Key__c+'&Debug='+!URL.getSalesforceBaseUrl().getHost().equalsignorecase('carbidnow.my.salesforce.com')+assignment(lead,api.Name);
if(selectedService=='ARD_Assignment__c'){
endpoint = endpoint+'&ARDBidID='+quote.Name;
}
req.setEndpoint(endpoint);
try {
//capture response
sObject assignment = Schema.getGlobalDescribe().get(selectedService).newSObject();
assignment.put('name' , new Http().send(req).getBody());
if(String.ValueOf(assignment.get('name')).isNumericSpace()){
insert assignment;
quote.put(selectedService,assignment.id);
update quote;
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Info,'Claim #'+assignment.get('name')+' created.'));
}else{
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Info,'ARD said: '+assignment.get('name')));
}
} catch(System.Exception e) {
//if no connection, display error
System.debug('********** Web Service Error: '+ e);
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Error,'There was an error with the web service: '+e));
}
}
//use values in lead and "Assignment Service Lead Field Mapping" Custom Setting record to create a string that can build an assignment
public String assignment(Lead lead,String service){
try {
String assignment = '';
//iterate collection of Assignment_Service_Lead_Field_Mapping__c fields
for(Schema.SObjectField field : Schema.getGlobalDescribe().get('Assignment_Service_Lead_Field_Mapping__c').getDescribe().fields.getMap().values())
{
//ensure no required field is blank
if (!field.getDescribe().isNillable() && field.getDescribe().isCustom() && String.isBlank(String.ValueOf(lead.get(field.getDescribe().getLabel()))))
throw new QuoteException('The following required field was blank: '+field.getDescribe().getLabel());
//if the lead has a value, append it to the GET string
else if(field.getDescribe().isCustom() && !String.isBlank(String.ValueOf(lead.get(field.getDescribe().getLabel())))){
//if the field is boolean, replace true / false with yes / no
if(Lead.getSObjectType().getDescribe().fields.getMap().get(field.getDescribe().getLabel()).getDescribe().getType().Name() == Schema.DisplayType.Boolean.Name()){
//since the service takes HasNoLiens, we need the opposite of what was in Lien__c
if(field.getDescribe().getLabel()=='Lien__c') lead.put(field.getDescribe().getLabel(),!Boolean.ValueOf(String.ValueOf(lead.get(field.getDescribe().getLabel()))));
if(Boolean.ValueOf(lead.get(field.getDescribe().getLabel())))
assignment = assignment+'&'+Assignment_Service_Lead_Field_Mapping__c.getValues(service).get(Lead.getSObjectType().getDescribe().fields.getMap().get(field.getDescribe().getLabel()).getDescribe().isCustom() ? field.getDescribe().getLabel() : field.getDescribe().getLabel()+'__c')+'=YES';
else
assignment = assignment+'&'+Assignment_Service_Lead_Field_Mapping__c.getValues(service).get(Lead.getSObjectType().getDescribe().fields.getMap().get(field.getDescribe().getLabel()).getDescribe().isCustom() ? field.getDescribe().getLabel() : field.getDescribe().getLabel()+'__c')+'=NO';
//RunsAndDrives can take yes or no, so Mechanical_Status__c picklist needs converted
}else if(field.getDescribe().getLabel()=='Mechanical_Status__c'){
if(String.ValueOf(lead.get(field.getDescribe().getLabel())).equalsIgnoreCase('Runs / Drives'))
assignment = assignment+'&'+Assignment_Service_Lead_Field_Mapping__c.getValues(service).get(Lead.getSObjectType().getDescribe().fields.getMap().get(field.getDescribe().getLabel()).getDescribe().isCustom() ? field.getDescribe().getLabel() : field.getDescribe().getLabel()+'__c')+'='+'Yes';
else
assignment = assignment+'&'+Assignment_Service_Lead_Field_Mapping__c.getValues(service).get(Lead.getSObjectType().getDescribe().fields.getMap().get(field.getDescribe().getLabel()).getDescribe().isCustom() ? field.getDescribe().getLabel() : field.getDescribe().getLabel()+'__c')+'='+'No';
}else
assignment = assignment+'&'+Assignment_Service_Lead_Field_Mapping__c.getValues(service).get(Lead.getSObjectType().getDescribe().fields.getMap().get(field.getDescribe().getLabel()).getDescribe().isCustom() ? field.getDescribe().getLabel() : field.getDescribe().getLabel()+'__c')+'='+String.ValueOf(lead.get(field.getDescribe().getLabel()));
}
}
return assignment;
} catch(System.Exception e) {
system.debug('*********** GET string processing error: '+e);
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Error,'Error creating request to send: '+String.ValueOf(e)));
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment