function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
msb-appsupport1.3905906418879758E12msb-appsupport1.3905906418879758E12 

How to stop SF convert a lead to a contact when I assign a campaign to the lead using code?

When I insert a lead, I want to assign a campaign to the lead. However, after I do it, the SF will convert the lead to a contact automatically. How could I solve this using Apex code or Visualforce?

The code I use is as following:

Map<String,Id> recordtypeMap  = new Map<String,Id>();
for(RecordType leadRT :  [select id, Name from RecordType where SObjectType = 'Lead' ]){
recordtypeMap.put(leadRT.Name,leadRT.Id);
}
Lead l = new Lead(firstname='testInsertLead-1-0326', lastname='test-1-0326',company='Temp Company',
                  RecordTypeId = recordtypeMap.get('Temp Lead Type'));
insert l;
Campaign c = [Select id from Campaign limit 1];
CampaignMember mem = new CampaignMember (campaignid=c.id, leadid=l.id);
insert mem;

Thanks.
Ashish_SFDCAshish_SFDC
HI , 


Apex code will do the trick, 

Put the code in the If block - if the condition is satisfied only then the code will execute. 

Else throw an exception and an Error. 

Try and check if the Query is working fine and returning the exact results. 


Regards,
Ashish 
Vivek Deepak11Vivek Deepak11
Hi
This will not be a accurate one but you can flag a lead which you are creating by code like " In Campaign " and write a trigger on contact to delete the respective contact if created by a Campaign lead on After Insert