I'm trying to use/modify the code in this free app to be able to convert/move a Custom Object associated with a Lead to the converted Account & Contact.
https://appexchange.salesforce.com/listingDetail?listingId=a0N30000003GuisEAC
Its not working for me and I can't figure out why. I reached out to the developer awhile ago and still haven't heard back.
I've installed in my sandbox and updated the code to reference my custom object "Qualification Criteria". I've created a sample record related to a lead, but when I convert a Lead record - the related "Qualification Criteria" record does not get converted over to the Account/Contact. (It just remains associated with the Lead, which has since been converted of course)
Any ideas?
Thanks!
TRIGGER
trigger UpdateCustomeObject_Trigger on Lead (before update) { //This trigger will associate a Custom Object record with the contact and opportunity associated to the //lead after it has been converted. //The Custom Object is associated to an opportunity only if an opportunity record exist on the Lead.
for (Integer i = 0; i < Trigger.new.size(); i++){
if (Trigger.new[i].IsConverted == true && Trigger.old[i].isConverted == false){
Set<Id> leadIds = new Set<Id>();
for (Lead lead : Trigger.new)
leadIds.add(lead.Id);
Map<Id, Qualification_Criteria__c> entries = new Map<Id, Qualification_Criteria__c>([select Contact__c, Opportunity__c, Account__c, Lead__c from Qualification_Criteria__c where lead__c in :leadIds]);
if(!Trigger.new.isEmpty()) {
for (Lead lead : Trigger.new) {
for (Qualification_Criteria__c Qualification_Criteria : entries.values()) {
if (Qualification_Criteria.Lead__c == lead.Id) {
Qualification_Criteria.contact__c = lead.ConvertedContactId;
Qualification_Criteria.opportunity__c = lead.ConvertedOpportunityId;
Qualification_Criteria.account__c = lead.ConvertedAccountId;
update Qualification_Criteria;
}
}
}
}
}
}
}
TEST CLASS
@isTest
//This is a test case for a situation where a lead will be converted. The developer must explicitly call the convert lead
//method to simulate the user action.
private class TestTriggerCustomObjectUpdate {
static testMethod void TestReferralUpdate() {
// Insert the Lead
List<Lead> leads = new List<Lead>();
Lead leadt = new Lead (FirstName ='fname', LastName ='test', Company ='myCompany');
insert leadt;
// Insert the custom object Record
Qualification_Criteria__c Qualification_Criteria = new Qualification_Criteria__c (Lead__c = leadt.Id);
insert Qualification_Criteria;
//Convert the Lead
Database.LeadConvert lc = new database.LeadConvert();
lc.setLeadId(leadt.Id);
LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
lc.setConvertedStatus(convertStatus.MasterLabel);
Database.LeadConvertResult lcr = Database.convertLead(lc);
//Requery for the referral record to see if it is updated
Qualification_Criteria__c ref_upd = [select Account__c, Contact__c, Opportunity__c from Qualification_Criteria__c where Lead__c = :leadt.Id];
//Check that the test passed
System.assertEquals(ref_upd.Account__c,[Select ConvertedAccountId From Lead Where Id = :Qualification_Criteria.Lead__c].ConvertedAccountId);
System.assertEquals(ref_upd.Contact__c,[Select ConvertedContactId From Lead Where Id = :Qualification_Criteria.Lead__c].ConvertedContactId);
System.assertEquals(ref_upd.Opportunity__c,[Select ConvertedOpportunityId From Lead Where Id = :Qualification_Criteria.Lead__c].ConvertedOpportunityId);
//Test if no opty is created
string NoOpty = 'Y';
if (NoOpty =='Y'){
Lead leadto = new Lead (FirstName ='fnameo', LastName ='testo', Company ='myCompanyo');
insert leadto;
// Insert the custom object record
Qualification_Criteria__c Qualification_Criteriao = new Qualification_Criteria__c (Lead__c = leadto.Id);
insert Qualification_Criteriao;
Database.LeadConvert lco = new database.LeadConvert();
lco.setLeadId(leadto.Id);
lco.isDoNotCreateOpportunity();
lco.setDoNotCreateOpportunity(true);
LeadStatus convertStatuso = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
lco.setConvertedStatus(convertStatuso.MasterLabel);
Database.LeadConvertResult lcro = Database.convertLead(lco);
Qualification_Criteria__c ref_updo = [select Account__c, Contact__c, Opportunity__c from Qualification_Criteria__c where Lead__c = :leadto.Id];
//Check that the test passed
System.assertEquals(ref_updo.Account__c,[Select ConvertedAccountId From Lead Where Id = :Qualification_Criteriao.Lead__c].ConvertedAccountId);
System.assertEquals(ref_updo.Contact__c,[Select ConvertedContactId From Lead Where Id = :Qualification_Criteriao.Lead__c].ConvertedContactId);
System.assert(ref_updo.Opportunity__c == null);
}
}
static testMethod void testBulkUpdate() {
List<Lead> leads = new List<Lead>();
for (Integer i=0;i<5;i++) {
Lead l = new Lead (FirstName ='bulk', LastName ='Test', Company ='myCompanyo');
insert l;
// Insert the custom Record
Qualification_Criteria__c r = new Qualification_Criteria__c (Lead__c = l.Id);
insert r;
//Convert the Lead
Database.LeadConvert lcb = new database.LeadConvert();
lcb.setLeadId(l.Id);
LeadStatus convertStatusb = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
lcb.setConvertedStatus(convertStatusb.MasterLabel);
Database.LeadConvertResult lcrb = Database.convertLead(lcb);
Qualification_Criteria__c bulkup = [select Account__c, Contact__c, Opportunity__c from Qualification_Criteria__c where Lead__c =:l.Id];
//Check that the test has passed
System.assertEquals(bulkup.Account__c,[Select ConvertedAccountId From Lead Where Id = :r.Lead__c].ConvertedAccountId);
System.assertEquals(bulkup.Contact__c,[Select ConvertedContactId From Lead Where Id = :r.Lead__c].ConvertedContactId);
System.assertEquals(bulkup.Opportunity__c,[Select ConvertedOpportunityId From Lead Where Id = :r.Lead__c].ConvertedOpportunityId);
}
}
}
Attribution to: OfficeGnome
Possible Suggestion/Solution #1
I think your logic is not Salesforce oriented. Avoid to put SQL query into your loop, that's really not best practice. There is a lot of useless steps in your code. Try more something like that :
trigger UpdateCustomeObject_Trigger on Lead (before update)
{
List<Qualification_Criteria__c> entries = new List<Qualification_Criteria__c>();
for (Qualification_Criteria__c Qualification_Criteria : [ select Contact__c, Opportunity__c, Account__c, Lead__c
from Qualification_Criteria__c
where Lead__c in :Trigger.newMap.keySet()])
{
Lead lead = Trigger.newMap.get(Qualification_Criteria.Lead__c);
if (lead != null && lead.isConverted == true && Trigger.oldMap.get(lead.Id).isConverted == false)
{
Qualification_Criteria.contact__c = lead.ConvertedContactId;
Qualification_Criteria.opportunity__c = lead.ConvertedOpportunityId;
Qualification_Criteria.account__c = lead.ConvertedAccountId;
entries.add(Qualification_Criteria);
}
}
update entries;
}
Attribution to: Cloud Ninja
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/31578