Skip to content

Instantly share code, notes, and snippets.

@BradRuderman
Created September 29, 2014 07:00
Show Gist options
  • Save BradRuderman/5d8e83fa0e0a5a725455 to your computer and use it in GitHub Desktop.
Save BradRuderman/5d8e83fa0e0a5a725455 to your computer and use it in GitHub Desktop.
Lead Conversion Rest
@RestResource(urlMapping='/ConvertLead/')
global with sharing class RestLeadConversion {
public static Boolean THROW_EXCEPTION = false;
public class UnhandledException extends Exception {}
public static void generateExceptionForTesting() {
if (Test.isRunningTest() && THROW_EXCEPTION) {
throw new UnhandledException('Exception for testing');
}
}
@HttpPost
global static LeadConversionResult doConvertLead(string LeadEmail) {
LeadConversionResult result = new LeadConversionResult();
result.converted = False;
System.debug('Staring Rest Lead Convert ' + LeadEmail);
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
List<Lead> OpportunityLeads = [SELECT Id, Status, ConvertedOpportunityId, ConvertedAccountId, ConvertedContactId, IsConverted FROM Lead WHERE Email__c = :LeadEmail LIMIT 1];
if (OpportunityLeads.isEmpty()){
result.reason = string.valueOf('lead not found');
return result;
} else {
Lead OpportunityLead = OpportunityLeads[0];
if (OpportunityLead.IsConverted){
result.reason = 'lead already converted';
result.opportunityId = OpportunityLead.ConvertedOpportunityId;
result.accountId = OpportunityLead.ConvertedAccountId;
result.contactId = OpportunityLead.ConvertedContactId;
result.leadId = OpportunityLead.Id;
} else {
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(OpportunityLead.id);
LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
lc.setConvertedStatus(convertStatus.MasterLabel);
Database.LeadConvertResult lcr ;
try{
generateExceptionForTesting();
lcr = Database.convertLead(lc);
if (lcr.isSuccess()){
generateExceptionForTesting();
result.converted = True;
result.opportunityId = lcr.getOpportunityId();
result.leadId = lcr.getLeadId();
result.accountId = lcr.getAccountId();
result.contactId = lcr.getContactId();
} else {
List<LeadConversionError> lcrErrors = new List<LeadConversionError>();
Database.Error[] dbErrors = lcr.getErrors();
for (Database.error e: dbErrors){
LeadConversionError lcrError = new LeadConversionError();
lcrError.message= e.getMessage();
lcrError.statusCode = string.valueOf(e.getStatusCode());
lcrError.fields = e.getFields();
lcrErrors.add(lcrError);
}
result.errors = lcrErrors;
}
}
catch (exception ex){
List<LeadConversionError> lcrErrors = new List<LeadConversionError>();
LeadConversionError lcrError = new LeadConversionError();
lcrError.message= ex.getMessage();
lcrError.statusCode = ex.getStackTraceString();
lcrErrors.add(lcrError);
result.errors = lcrErrors;
}
}
return result;
}
}
global class LeadConversionResult {
global Boolean converted { get; set; }
global String reason {get; set;}
global Id opportunityId {get;set;}
global Id leadId {get;set;}
global Id contactId {get;set;}
global Id accountId {get;set;}
global LeadConversionError[] errors {get;set;}
}
global class LeadConversionError {
global String statusCode {get;set;}
global String message {get;set;}
global String[] fields {get;set;}
}
}
/* Test Class */
@isTest
private class RestLeadConversionTest {
static testMethod void testDoConvertLead(){
Test.startTest();
//As Per Best Practice it is important to instantiate the Rest Context
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI = '/ConvertLead/'; //Request URL
req.httpMethod = 'POST';//HTTP Request Type
req.requestBody = Blob.valueof('{"LeadEmail":"test@test.com"}');
RestContext.request = req;
RestContext.response= res;
RestLeadConversion.LeadConversionResult resp;
resp=RestLeadConversion.doConvertLead('test@test.com'); //Call the Method of the Class with Proper Constructor
System.assert(resp.reason == 'lead not found');
System.assert(resp.converted == false);
insert new Lead(Company = 'Test Lead', FirstName = 'Test', LastName = 'Test', Email__c = 'test2@test.com');
resp=RestLeadConversion.doConvertLead('test2@test.com'); //Call the Method of the Class with Proper Constructor
System.assert(resp.converted == true);
resp=RestLeadConversion.doConvertLead('test2@test.com'); //Call the Method of the Class with Proper Constructor
System.assert(resp.converted == false);
insert new Lead(Company = 'Test Lead', FirstName = 'Test', LastName = 'Test', Email__c = 'test123@test.com');
RestLeadConversion.THROW_EXCEPTION = true;
resp=RestLeadConversion.doConvertLead('test123@test.com');
Test.stopTest();
}
static testMethod void testLeadConversionResultError(){
RestLeadConversion.LeadConversionError leadError = new RestLeadConversion.LeadConversionError();
leadError.message = 'lead not found';
}
static testMethod void testLeadConversionResult(){
RestLeadConversion.LeadConversionResult leadResult = new RestLeadConversion.LeadConversionResult();
leadResult.converted = true;
leadResult.reason = 'sweetBabyRays';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment