You need to sign in to do that
Don't have an account?
Sarma Duvvuri
Conversion of Lead Without Opportunity
Hi All,
How to do lead conversion without creating an opportunity. (Only account and contact has to be created)
If the account is already exists contact detailes has to updated to that exisiting account and if the details are already there in salesforce and different from the requirement, we need to update those details in account and contacts.
Lead Conversion Code:
Trigger web2LeadConvert on Lead (after Update) {
LeadStatus convertStatus = [
select MasterLabel
from LeadStatus
where IsConverted = true
limit 1
];
List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
for (Lead lead: Trigger.new) {
if (!lead.isConverted && lead.Status == 'Qualified') {
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(lead.Id);
lc.setConvertedStatus(convertStatus.MasterLabel);
leadConverts.add(lc);
}
}
if (!leadConverts.isEmpty()) {
List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
}
Please help to slove above problems.
Regards,
Sarma
How to do lead conversion without creating an opportunity. (Only account and contact has to be created)
If the account is already exists contact detailes has to updated to that exisiting account and if the details are already there in salesforce and different from the requirement, we need to update those details in account and contacts.
Lead Conversion Code:
Trigger web2LeadConvert on Lead (after Update) {
LeadStatus convertStatus = [
select MasterLabel
from LeadStatus
where IsConverted = true
limit 1
];
List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
for (Lead lead: Trigger.new) {
if (!lead.isConverted && lead.Status == 'Qualified') {
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(lead.Id);
lc.setConvertedStatus(convertStatus.MasterLabel);
leadConverts.add(lc);
}
}
if (!leadConverts.isEmpty()) {
List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
}
Please help to slove above problems.
Regards,
Sarma
Add the below line of code in your existing code:
lc.setDoNotCreateOpportunity(TRUE); after lc.setConvertedStatus(convertStatus.MasterLabel);
This will not create an opportunity upon lead conversion.
Let me know if it helps or you need more assistance.
Thanks,
Jainam Contractor,
Salesforce Consultant,
Varasi LLC,
www.varasi.com
All Answers
Add the below line of code in your existing code:
lc.setDoNotCreateOpportunity(TRUE); after lc.setConvertedStatus(convertStatus.MasterLabel);
This will not create an opportunity upon lead conversion.
Let me know if it helps or you need more assistance.
Thanks,
Jainam Contractor,
Salesforce Consultant,
Varasi LLC,
www.varasi.com
Your welcome... Glad that my solution was able to help you.
Please select it as the best answer/ solution to help other community members and mark your question as solved.
Thanks.
Kindly help me in this please.
If the account is already exists contact detailes has to updated to that exisiting account and if the details are already there in salesforce and different from the requirement, we need to update those details in account and contacts.
Regards,
Sarma
This is not a good practice to combine two different requirement under one question. It may confuse other community members searching for solutions.
Can you please elaborate some more regarding this requirement. Is it the extension of the above requirement. You want to check whether the Lead's account already exist or not...???
Please elaborate more so that i can help you.
Thanks
Thanks,
sarma
I get the first part of the requirement i.e if account exists, that lead should be associated with that account and new account need not be created. But you are talking about updating the Contact, that lead might be a new lead from the same account, in that you would want to create a new contact rather than updating the existing contact.
Please clarify if my understanding is correct.
For checking if account already exists: fetch all the company names from the lead and write an SOQL to fetch for those accounts like:
Add the below snippet after initializing list of LeadConverts.
List<String> LeadAcc = new list<String>();
for(Lead L : Trigger.New){
LeadAcc.add(L.Company)
}
List<Account> AccList = [SELECT Id, Name FROM Account WHERE Name IN :LeadAcc];
Now before setting the lead id in the for Loop, add the following snippet:
for(Account A : AccList){
if(Lead.Company == A.Name){
Leadconvert.setAccountId(A.Id);
}
}
This will not create the new Account but convert the lead and associate with the existing account and create a new Contact record for the lead.
Please let me know if this helps you.
Thanks & Regards,
Jainam Contractor,
Salesforce Consultant,
Varasi LLC,
www.varasi.com
Thank yo ufor the quick response.
I am getting the below error. Please help
Error: Compile Error: Method does not exist or incorrect signature: void setAccountId(Id) from the type System.LeadConvert at line 20 column 25
Code: Trigger web2LeadConvert on Lead (after Update) {
LeadStatus convertStatus = [
select MasterLabel
from LeadStatus
where IsConverted = true
limit 1
];
List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
List<String> LeadAcc = new list<String>();
for(Lead L : Trigger.New){
LeadAcc.add(L.Company);
}
List<Account> AccList = [SELECT Id, Name FROM Account WHERE Name IN :LeadAcc];
for (Lead lead: Trigger.new) {
if (!lead.isConverted && lead.Status == 'Qualified') {
Database.LeadConvert lc = new Database.LeadConvert();
for(Account A : AccList){
if(Lead.Company == A.Name){
Leadconvert.setAccountId(A.Id);
}
}
lc.setLeadId(lead.Id);
lc.setConvertedStatus(convertStatus.MasterLabel);
lc.setDoNotCreateOpportunity(TRUE);
leadConverts.add(lc);
}
}
if (!leadConverts.isEmpty()) {
List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
}
}
Change Leadconvert.setAccountId(A.Id); to lc.setAccountId(A.Id); as we have used lc as the object name for Database.LeadConvert
Please try the above workaround and let me know if your error is gone.
Thanks & Regards,
Jainam Contractor.
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger web2LeadConvert caused an unexpected exception, contact your administrator: web2LeadConvert: execution of AfterUpdate caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: DUPLICATES_DETECTED, You're creating a duplicate account. Please use the existing record instead.: []: Trigger.web2LeadConvert: line 36, column 1
Can you send over your whole code... So that i can check what is causing error and rectify the same.
Thanks
Please refer the whole code below.
Trigger web2LeadConvert on Lead (after Update) {
LeadStatus convertStatus = [
select MasterLabel
from LeadStatus
where IsConverted = true
limit 1
];
List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
List<String> LeadAcc = new list<String>();
for(Lead L : Trigger.New){
LeadAcc.add(L.Company);
}
List<Account> AccList = [SELECT Id, Name FROM Account WHERE Name IN :LeadAcc];
for (Lead lead: Trigger.new) {
if (!lead.isConverted && lead.Status == 'Qualified') {
Database.LeadConvert lc = new Database.LeadConvert();
for(Account A : AccList){
if(Lead.Company == A.Name){
lc.setAccountId(A.Id);
}
}
lc.setLeadId(lead.Id);
lc.setConvertedStatus(convertStatus.MasterLabel);
lc.setDoNotCreateOpportunity(TRUE);
leadConverts.add(lc);
}
}
if (!leadConverts.isEmpty()) {
List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
}
Sarma
Please find the below code... It works exactly fine for me for all possible scenarios i.e
Lead from New Account : Creates an Account and Contact Record
Lead from Existing Account : Creates only New Contact and associates the Contact with the Existing Account
Trigger web2LeadConvert on Lead (after Update) {
LeadStatus convertStatus = [
select MasterLabel
from LeadStatus
where IsConverted = true
limit 1
];
List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
List<String> LeadAcc = new list<String>();
for(Lead L : Trigger.New){
LeadAcc.add(L.Company);
}
List<Account> AccList = [SELECT Id, Name FROM Account WHERE Name IN :LeadAcc];
for (Lead lead: Trigger.new) {
if (!lead.isConverted && lead.Status == 'Qualified') {
Database.LeadConvert lc = new Database.LeadConvert();
for(Account A : AccList){
if(lead.Company == A.Name){
lc.setAccountId(A.Id);
system.debug('Account Name-----'+A.Name+'---------'+lead.Company);
}
}
lc.setLeadId(lead.Id);
lc.setConvertedStatus(convertStatus.MasterLabel);
lc.setDoNotCreateOpportunity(TRUE);
leadConverts.add(lc);
system.debug('LeadConverts-----'+leadConverts);
}
}
if (!leadConverts.isEmpty()) {
List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
}
}
Please check if it helps you...
Thanks,
Jainam Contractor
Kindly help
Apex trigger web2LeadConvert caused an unexpected exception, contact your administrator: web2LeadConvert: execution of AfterUpdate caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: DUPLICATES_DETECTED, You're creating a duplicate account. Please use the existing record instead.: []: Trigger.web2LeadConvert: line 37, column 1
List<String> LeadAcc = new list<String>();
for(Lead L : Trigger.New){
LeadAcc.add(L.Company); //This is taking the ‘company’ field from the lead
}
List<Account> AccList = [SELECT Id, Name FROM Account WHERE Name IN :LeadAcc]; //Now it is looking for that company field as an account name. There are no exact matches since we used Company + City + State Code to form our account names. This list will be empty.
for (Lead lead: Trigger.new) {
if (!lead.isConverted && lead.Status == 'Qualified') {
Database.LeadConvert lc = new Database.LeadConvert();
for(Account A : AccList){
if(lead.Company == A.Name){ //Now it compares the company with the empty list to see if there are matches before adding the new account. There are no matches since the list is empty, it will create a new account each time.
lc.setAccountId(A.Id);
system.debug('Account Name-----'+A.Name+'---------'+lead.Company);
}
}
In that scenario, you have to create the String according to the Account Name in the first for Loop. Add the below snippet in place of the First For Loop.
String AccName = "";
for(Lead L : Trigger.New){
AccName = L.Company + L.City + L.State; //Add the spacing as per your requirement
LeadAcc.add(AccName);
}
In the other for loop also, you will need to create a similar String as per the Account Name and use the same in the IF condition instead of just the Company.
This should now work then. Please let me know if it works and solves your problem.
Thanks,
Jainam Contractor
Please confirm the below code is correct or not. Thank you for the help.
Trigger web2LeadConvert on Lead (after Update) {
LeadStatus convertStatus = [
select MasterLabel
from LeadStatus
where IsConverted = true
limit 1
];
List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
String AccName = "";
for(Lead L : Trigger.New){
AccName = L.Company + L.BillingCity + L.BillingState; //Add the spacing as per your requirement
LeadAcc.add(AccName);
}
for (Lead lead: Trigger.new) {
if (!lead.isConverted && lead.Status == 'Qualified') {
Database.LeadConvert lc = new Database.LeadConvert();
for(Account A : AccList){
if(lead.AccName == A.Name){
lc.setAccountId(A.Id);
system.debug('Account Name-----'+A.Name+'---------'+lead.Company);
}
}
lc.setLeadId(lead.Id);
lc.setConvertedStatus(convertStatus.MasterLabel);
lc.setDoNotCreateOpportunity(TRUE);
leadConverts.add(lc);
system.debug('LeadConverts-----'+leadConverts);
}
}
if (!leadConverts.isEmpty()) {
List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
}
}
Regards,
Sarma
You removed the SOQL query and many you messed a lot with the Code. Please use the below code and let me know if it helps:
Trigger web2LeadConvert on Lead (after Update) {
LeadStatus convertStatus = [
select MasterLabel
from LeadStatus
where IsConverted = true
limit 1
];
List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
List<String> LeadAcc = new list<String>();
String AccName = "";
for(Lead L : Trigger.New){
AccName = L.Company + L.City + L.State; //Add the spacing as per your requirement
LeadAcc.add(AccName);
AccName = '';
}
List<Account> AccList = [SELECT Id, Name FROM Account WHERE Name IN :LeadAcc];
for (Lead lead: Trigger.new) {
if (!lead.isConverted && lead.Status == 'Qualified') {
AccName = lead.Company + lead.City + lead.State;
Database.LeadConvert lc = new Database.LeadConvert();
for(Account A : AccList){
if(AccName == A.Name){
lc.setAccountId(A.Id);
system.debug('Account Name-----'+A.Name+'---------'+lead.Company);
}
}
lc.setLeadId(lead.Id);
lc.setConvertedStatus(convertStatus.MasterLabel);
lc.setDoNotCreateOpportunity(TRUE);
leadConverts.add(lc);
AccName = '';
system.debug('LeadConverts-----'+leadConverts);
}
}
if (!leadConverts.isEmpty()) {
List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
}
}
Text highlightd in BOLD are new changes that you messed while changing.
Please use the above code and check whether it works or not.
Thanks,
Jainam Contractor.
How are you creating the Account Name from the Lead. Please give those exact details. The format might differ from the one we are using here.
Something Like: if the Account Name is: Varasi Pune Maharashtra then string would be something like this : AccName = lead.Company + ' ' + lead.City + ' ' + lead.State; where Company=Varasi, City=Pune, State=Maharashtra
So send over the Account Name format so that i can help else this is the Correct method.
Thank,
Jainam Contractor
Contact: Harsha Rajesh
Account: HSBC California CA
if i create contact as Sarma Duvvuri. getting the new account not updating the exisitng account.
Hope this is the solution for your problem now.
Instead of the AccName = L.Company + L.City + L.State; use AccName = L.Company + ' ' + L.City + ' ' + L.State; and instead of
AccName = lead.Company + lead.City + lead.State; use AccName = lead.Company + ' ' + lead.City + ' ' + lead.State;
Please make those changes and let me know if it helps.
Thanks,
Jainam Contractor.
Error: Compile Error: Invalid identifier ''. Apex identifiers must start with an ASCII letter (a-z or A-Z) followed by any number of ASCII letters (a-z or A-Z), digits (0 - 9), '$', '_'. at line 19 column 65
Trigger web2LeadConvert on Lead (after Update) {
LeadStatus convertStatus = [
select MasterLabel
from LeadStatus
where IsConverted = true
limit 1
];
List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
List<String> LeadAcc = new list<String>();
String AccName = '';
for(Lead L : Trigger.New){
AccName = L.Company + ' ' + L.BillingCity + ' ' + L.BillingState;
LeadAcc.add(AccName);
AccName = '';
}
List<Account> AccList = [SELECT Id, Name FROM Account WHERE Name IN :LeadAcc];
for (Lead lead: Trigger.new) {
if (!lead.isConverted && lead.Status == 'Qualified') {
AccName = lead.Company + ' ' + lead.BillingCity + ' ' + lead.BillingState;
Database.LeadConvert lc = new Database.LeadConvert();
for(Account A : AccList){
if(AccName == A.Name){
lc.setAccountId(A.Id);
system.debug('Account Name-----'+A.Name+'---------'+lead.Company);
}
}
lc.setLeadId(lead.Id);
lc.setConvertedStatus(convertStatus.MasterLabel);
lc.setDoNotCreateOpportunity(TRUE);
leadConverts.add(lc);
AccName = '';
system.debug('LeadConverts-----'+leadConverts);
}
}
if (!leadConverts.isEmpty()) {
List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
}
}
Kindly help.
Sarma
Sure... but for that i need the logic/ requirement which you are using to create an Account Name.
In my answer, I have considered lead.Company + ' ' + lead.BillingCity + ' ' + lead.BillingState logic.
So for eg:
Lead Company = Varasi
Billing City = Lake Forest
Billing State = California
Then it will search for Account Name = Varasi Lake Forest California, if it finds one then it will associate lead to the Account else it will create new Account with that name and create a Contact to that Account.
Can you please let me know what is your requirement in detail so that i can help you.
Thanks,
Jainam Contractor
I have created the lead conversion.. while converting the lead both contact and acount details are deleting. i want only existing account details to be deleted.
Please check the below code.
Trigger web2LeadConvert on Lead (after Update) {
LeadStatus convertStatus = [
select MasterLabel
from LeadStatus
where IsConverted = true
limit 1
];
List<String> Aname = New List<String>();
List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
map<string,string> statemap = new map<string,string>();
for (State_Code__mdt StateNames : [SELECT State_Name__c, State_Code_Name__c FROM State_Code__mdt]) {
statemap.put(StateNames.State_Name__c,StateNames.State_Code_Name__c);
system.debug('%%%%%%%%>' +Statenames);
}
for (Lead lead: Trigger.new) {
if (!lead.isConverted && lead.Status == 'Closed - Converted') {
if (statemap.containsKey(lead.State)) {
Aname.add(lead.Company+' '+lead.City+' '+statemap.get(lead.State));
system.debug('#####' + Aname);
}
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(lead.Id);
lc.setConvertedStatus(convertStatus.MasterLabel);
lc.setDoNotCreateOpportunity(TRUE);
leadConverts.add(lc);
system.debug('LeadConverts @@@@@'+leadConverts);
}
}
if (!leadConverts.isEmpty()) {
List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
System.debug('--------->' +lcr);
List<ID> ConvertedAccount = New List<ID>();
For(Database.LeadConvertResult lr : lcr){
ConvertedAccount.add(lr.AccountId);
}
List<Contact> contacs = [Select ID,Accountid,Account.Name,LastName From Contact Where Accountid IN : ConvertedAccount];
system.debug('******' + contacs);
/*Map<String , Contact> AllContacts = New Map<String , Contact>();
For(Contact c : contacs){
AllContacts.put(c.Account.Name, c);
system.debug('&&&&&&&&' + Allcontacts);
} */
List<Contact> cc = New List<Contact>();
List<Account> acc = [Select id, Name, (Select ID, Lastname From Contacts) From Account Where Name IN : Aname Order by CreatedDate DESC];
system.debug('List of acc$$$$$' + acc);
/*For(Account a : acc){
If(AllContacts.get(a.Name) != Null && AllContacts.get(a.Name).Name!= a.Name){
Contact c = AllContacts.get(a.Name);
c.Accountid = a.id;
cc.add(c);
}
}
update cc;*/
If(acc.size() != 0){
for(contact con : contacs){
con.accountId = acc[0].Id;
cc.add(con);
}
update cc;
List<Account> DeleteAccount = [Select Id From Account Where Id IN : ConvertedAccount];
Delete DeleteAccount;
}
}
}
Regards,
Sarma
I am using this code but not working. convert the lead automatic opportunity created
Please Help.
my requirement convert of lead without Opportunity.
Trigger :
trigger ptpLeadconversionnoOpp on Lead (after update) {
LeadStatus convertStatus = [ select MasterLabel from LeadStatus where IsConverted = true limit 1];
List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
for (Lead lead: Trigger.new) {
if (!lead.isConverted && lead.Source_Description__c == 'PTP Registration') {
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(lead.Id);
lc.setConvertedStatus(convertStatus.MasterLabel);
lc.setDoNotCreateOpportunity(TRUE);
leadConverts.add(lc);
}
}
if (!leadConverts.isEmpty()) {
List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
}
}
Test Class :
@isTest
public class ptpLeadconversionnoOpp_Test {
@isTest
static void testPTPLeadConversion() {
// Create a Lead
Lead led = new Lead();
led.LastName = 'Test Lead247';
led.Company = 'Test Company247';
led.Email = 'test247@gmail.com';
led.Status = 'Open';
led.Source_Description__c = 'PTP Registration';
insert led;
// Create a LeadStatus for conversion
LeadStatus convertStatus = [SELECT MasterLabel FROM LeadStatus WHERE IsConverted = true LIMIT 1];
// Trigger Lead Conversion
test.startTest();
led.Status = convertStatus.MasterLabel;
update led;
test.stopTest();
// Verify that the Lead was converted
led = [SELECT IsConverted FROM Lead WHERE Id = :led.Id];
System.assertEquals(true, led.IsConverted, 'Lead should be converted');
// Additional assertions can be added as needed
}
}
Could you let me know the errors on test class pls.