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 delete the Lead Converted Account ID using Trigger

i  created trigger for Lead Conversion. i created two cusotm obejcts i.e  Agency and Broker custom objects.

1. once lead is converted lead company name goes to Agency  and lead name goes to broker object..

2. once lead is converted Salesforce automatically created account and contact records.But i want no need to created account and contact records once lead is converted. i want delete the account and contact records.

how can solve this problem. i write the trigger for lead conversion to agency and broker objects that's workng fine but account and contact records not deleted. pls help me..................................



trigger ConvertLead on Lead (after update) {
    List<Lead> leadIds = new List<Lead>();
    List<ID> LIds = new List<ID>();
    List<ID> accountIds= new List<ID>();
    List<ID> cids = new List<ID> {};
    List<Account> aclist = new List<Account> {};
    List<Contact> contactids = new List<Contact>();
    List<Task> tasklist = new List<Task> {};
    Id lId;
    List<RecordType> rtypes = [Select Name, Id From RecordType where sObjectType='Lead' and isActive=true];
    Map<String,String> leadRecordTypes = new Map<String,String>{};
    for(RecordType rt: rtypes){
    for(Lead led: Trigger.New){
        System.debug('Lead Details:'+led);
         if(led.isConverted == true && led.Lead_type_picklist__c =='Broker'&& led.RecordTypeId==leadRecordTypes.get('Broker')){
       // if(led.isConverted == true && led.RecordTypeId =='012G0000000GNaJIAW'){
    for(Account ac: [select id from account where id in : accountIds]) {
        //ac.RecordTypeId = '012Z00000004KO6';

    Map<Id,Lead> leadMap = new Map<Id,Lead>([Select id,Name,Company,LeadSource,Street,City,State,County__c,PostalCode,Website,Title,Email,Phone  from Lead Where id in:LIds]);
    List<Agency__c > agencyinsert = new List<Agency__c >();
    List<Broker__c> brokers = new List<Broker__c>();
    for(integer i=0; i<leadIds.size(); i++){
        lId = leadIds[i].Id;
        Lead leadobj = leadMap.get(lId);
        Agency__c ag = new Agency__c();
        ag.Name = leadobj.Company;
        ag.Mailing_Address__c =leadobj.Street;
        ag.City__c =leadobj.City;
        ag.State__c = leadobj.State;
        ag.County__c = leadobj.County__c;
        ag.Zip_Code__c = leadobj.PostalCode;  
        ag.Phone__c = leadobj.Phone;
        ag.Website__c = leadobj.Website;
    if( agencyinsert.size()>0)
        insert agencyinsert;
   for(integer i=0; i<leadIds.size(); i++){
      for(integer j=0; j<agencyinsert.size(); j++){
             Lead leadobj1 = leadMap.get(lId);
             Broker__c b = new Broker__c();
             b.Name = leadobj1.Name;
             b.Agency__c = agencyinsert[j].id;
             b.Title__c = leadobj1.Title;
             b.Address__c = leadobj1.Street;
             b.City__c  = leadobj1.City;
             b.State__c = leadobj1.State;
             b.County__c = leadobj1.County__c;
             b.Zip_Code__c = leadobj1.PostalCode;
             b.Phone__c = leadobj1.Phone;
             b.Email__c = leadobj1.Email;
             b.Website__c = leadobj1.Website;
             b.Broker_Appointment_Lead_Source__c  = leadobj1.LeadSource;
    if(brokers.size() > 0)
        insert brokers;
       delete aclist;


put this at the end



List<Lead> leadToUpdate = new List<Lead>();
for(Lead L :
    if(L.accountId != null)
        L.accountId = null;
    if(L.contactID != null)
        L.contactID = null;
if(leadToUpdate != null && leadToUpdate > 0)
    update leadToUpdate;