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

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' ]){
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 (,;
insert mem;

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. 

Vivek Deepak11Vivek Deepak11
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