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
Hemalatha  ParuchuriHemalatha Paruchuri 

after convert ,i need to delete my leads records automatically

{!REQUIRESCRIPT("/soap/ajax/37.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/37.0/apex.js")}

var accnt = new sforce.SObject("Account");
var recType=sforce.connection.query("SELECT name, id FROM RecordType" );
//alert(recType);
var records = recType.getArray("records");
//alert(records);
accnt.Name='{!My_Leads__c.Company__c}';
if(accnt.Name !="")
{
accnt.RecordTypeId = records[0].Id;
//alert(records[0].Id);

accnt.Id = '{!Account.Id}';
accnt.Name = prompt('','{!My_Leads__c.Company__c}');
accnt.OwnerId='{!My_Leads__c.OwnerId}';

var result = sforce.connection.create([accnt]);
if(result[0].getBoolean("success"))
{
alert('Account created successfully');

}
var cnt= new sforce.SObject("Contact");
cnt.Id = '{!Contact.Id}';
cnt.OwnerId='{!My_Leads__c.OwnerId}';
cnt.AccountId=result[0].id;
cnt.lastName = prompt('','{!My_Leads__c.Lead_Name__c}');

var result = sforce.connection.create([cnt]);
 if(result[0].getBoolean("success"))
{
alert('contact created successfully');
window.location.reload();

}

else{
  alert('Error : '+result);
}
}
else
{
accnt.RecordTypeId=records[1].Id;
//alert(records[1].Id);
accnt.Id = '{!Account.Id}';
accnt.LastName = prompt('','{!My_Leads__c.Lead_Name__c}');
accnt.OwnerId='{!My_Leads__c.OwnerId}';

var result = sforce.connection.create([accnt]);
if(result[0].getBoolean("success"))
{
alert('Account created successfully');

}
else
{
 alert('Error : '+result);
}
}
Best Answer chosen by Hemalatha Paruchuri
Hemalatha  ParuchuriHemalatha Paruchuri
{!REQUIRESCRIPT("/soap/ajax/37.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/37.0/apex.js")}

var accnt = new sforce.SObject("Account");

//caprturing Mylead id for deletion after insertion of account
var accs='{!My_Leads__c.Id}';

//Querying record types of account
var recType=sforce.connection.query("SELECT name, id FROM RecordType" );
var records = recType.getArray("records");
// checking for business account type
accnt.Name='{!My_Leads__c.Company__c}';
if(accnt.Name !="")
{
accnt.RecordTypeId = records[0].Id;

//creating account
accnt.Id = '{!Account.Id}';
accnt.Name = prompt('','{!My_Leads__c.Company__c}');
accnt.OwnerId='{!My_Leads__c.OwnerId}';
var result = sforce.connection.create([accnt]);

if(result[0].getBoolean("success")) 

alert('Account created successfully'); 
}
//creating contact
var cnt= new sforce.SObject("Contact"); 
cnt.Id = '{!Contact.Id}';
cnt.OwnerId='{!My_Leads__c.OwnerId}';
cnt.AccountId=result[0].id;
cnt.lastName = prompt('','{!My_Leads__c.Lead_Name__c}');

var result = sforce.connection.create([cnt]);
 if(result[0].getBoolean("success"))

alert('contact created successfully'); 

//Deleting the Mylead record
var leadlist=sforce.connection.query("Select id, Company__c, Lead_Name__c from My_Leads__c where id='"+accs+"'");

var records = leadlist.getArray("records");
alert(records[0].Id);
var data = Array();
for(var i=0;i< records.length; i++)
data.push (records[i].Id);
alert (data);
var deleteIds= sforce.connection.deleteIds(data);
alert (deleteIds);
window.location.reload();

}
else{
  alert('Error : '+result);
}
}

//Creating person account

else
{
accnt.RecordTypeId=records[1].Id;
//alert(records[1].Id);
accnt.Id = '{!Account.Id}';
accnt.LastName = prompt('','{!My_Leads__c.Lead_Name__c}');
accnt.OwnerId='{!My_Leads__c.OwnerId}';

var result = sforce.connection.create([accnt]); 
if(result[0].getBoolean("success")) 

alert('Account created successfully'); 
var leadlist=sforce.connection.query("Select id, Company__c, Lead_Name__c from My_Leads__c where id='"+accs+"'");
alert (leadlist);
var records = leadlist.getArray("records");
alert(records[0].Id);
var data = Array();
for(var i=0;i< records.length; i++)
data.push (records[i].Id);
alert (data);
var deleteIds= sforce.connection.deleteIds(data);
alert (deleteIds);
window.location.reload();

}
else
{
 alert('Error : '+result);
}
}



This my final answer

All Answers

Roshan 99Roshan 99
Hi Hemalatha, 

From your statement I understand that you want to delete the lead record once it is converted. 

Proposed Solution: 
Step 1: Create two Unique Fields one on Lead and one on Account. 
e.g. LeadUniqueSLNo__c   the field on Lead can be of type Auto Number (External ID) 
and the one on Account LeadUniqueSLNoAcct__c can be text/integer
Step 2: Use custom Lead mapping to map LeadUniqueSLNo__c  to  LeadUniqueSLNoAcct__c 
Step 3: Create a Trigger on Account that should execute upon creation of records. 
When an account record is created then check in the trigger if the LeadUniqueSLNoAcct__c is not null
if its not Null then fire SOQL to get the Lead record by querying for LeadUniqueSLNo__c 
Then delete the Lead record you get from the SOQL result

Let me know if you need any furter clarification. 

Thanks,
Rosh
 
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Write a trigger on Lead after update and delete those records where IsConverted = True
Hemalatha  ParuchuriHemalatha Paruchuri
Thsis not a standrd lead..this my custom lead
Hemalatha  ParuchuriHemalatha Paruchuri
{!REQUIRESCRIPT("/soap/ajax/37.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/37.0/apex.js")}

var accnt = new sforce.SObject("Account");

//caprturing Mylead id for deletion after insertion of account
var accs='{!My_Leads__c.Id}';

//Querying record types of account
var recType=sforce.connection.query("SELECT name, id FROM RecordType" );
var records = recType.getArray("records");
// checking for business account type
accnt.Name='{!My_Leads__c.Company__c}';
if(accnt.Name !="")
{
accnt.RecordTypeId = records[0].Id;

//creating account
accnt.Id = '{!Account.Id}';
accnt.Name = prompt('','{!My_Leads__c.Company__c}');
accnt.OwnerId='{!My_Leads__c.OwnerId}';
var result = sforce.connection.create([accnt]);

if(result[0].getBoolean("success")) 

alert('Account created successfully'); 
}
//creating contact
var cnt= new sforce.SObject("Contact"); 
cnt.Id = '{!Contact.Id}';
cnt.OwnerId='{!My_Leads__c.OwnerId}';
cnt.AccountId=result[0].id;
cnt.lastName = prompt('','{!My_Leads__c.Lead_Name__c}');

var result = sforce.connection.create([cnt]);
 if(result[0].getBoolean("success"))

alert('contact created successfully'); 

//Deleting the Mylead record
var leadlist=sforce.connection.query("Select id, Company__c, Lead_Name__c from My_Leads__c where id='"+accs+"'");

var records = leadlist.getArray("records");
alert(records[0].Id);
var data = Array();
for(var i=0;i< records.length; i++)
data.push (records[i].Id);
alert (data);
var deleteIds= sforce.connection.deleteIds(data);
alert (deleteIds);
window.location.reload();

}
else{
  alert('Error : '+result);
}
}

//Creating person account

else
{
accnt.RecordTypeId=records[1].Id;
//alert(records[1].Id);
accnt.Id = '{!Account.Id}';
accnt.LastName = prompt('','{!My_Leads__c.Lead_Name__c}');
accnt.OwnerId='{!My_Leads__c.OwnerId}';

var result = sforce.connection.create([accnt]); 
if(result[0].getBoolean("success")) 

alert('Account created successfully'); 
var leadlist=sforce.connection.query("Select id, Company__c, Lead_Name__c from My_Leads__c where id='"+accs+"'");
alert (leadlist);
var records = leadlist.getArray("records");
alert(records[0].Id);
var data = Array();
for(var i=0;i< records.length; i++)
data.push (records[i].Id);
alert (data);
var deleteIds= sforce.connection.deleteIds(data);
alert (deleteIds);
window.location.reload();

}
else
{
 alert('Error : '+result);
}
}



This my final answer
This was selected as the best answer