You need to sign in to do that
Don't have an account?
Sunny S
Auto Lead and Opportunity Conversion
Hi community friends, need some help please !
Overview:
We are getting some Person Accounts to flow into SalesCloud from one of the external systems (Dynamics 365).
We are than using Process Builder to auto create “Leads” for these accounts (for reporting and marketing perspective) – PB seems to be working correctly fine here;
Requirement:
1: We want to automate these Leads (just these ones) to close as ‘Converted’ upon creation; plus if possible
2: Have the opportunities to get auto created and close as ‘Closed – Won’ upon creation – without creating an account (as the account already exists)
I am been trying to use a combination of the following Apex class and Trigger, but seems is not working as desired…. Would anyone be able to help me with this issue please ?
Apex Trigger:
Trigger AutoConvertLead on Lead(After Insert)
{
For(Lead ld : trigger.new){
// calling apex class method to convert this lead
AutoConvertLead.leadconversion(ld.id)
}
}
Apex Class:
Public class AutoConvertLead(){
public static void leadConversion(string leadid){
Database.LeadConvert Leadconvert = new Database.LeadConvert();
// pass the leadId which need to be converted
Leadconvert.setLeadId(leadid);
LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
Leadconvert.setConvertedStatus(convertStatus.MasterLabel);
Leadconvert.setCreateOpportunity(True);
Database.LeadConvertResult Leadconverts = Database.convertLead(Leadconvert);
System.assert(Leadconverts.isSuccess());
}
}
Thanks and regards,
Sunny
Overview:
We are getting some Person Accounts to flow into SalesCloud from one of the external systems (Dynamics 365).
We are than using Process Builder to auto create “Leads” for these accounts (for reporting and marketing perspective) – PB seems to be working correctly fine here;
Requirement:
1: We want to automate these Leads (just these ones) to close as ‘Converted’ upon creation; plus if possible
2: Have the opportunities to get auto created and close as ‘Closed – Won’ upon creation – without creating an account (as the account already exists)
I am been trying to use a combination of the following Apex class and Trigger, but seems is not working as desired…. Would anyone be able to help me with this issue please ?
Apex Trigger:
Trigger AutoConvertLead on Lead(After Insert)
{
For(Lead ld : trigger.new){
// calling apex class method to convert this lead
AutoConvertLead.leadconversion(ld.id)
}
}
Apex Class:
Public class AutoConvertLead(){
public static void leadConversion(string leadid){
Database.LeadConvert Leadconvert = new Database.LeadConvert();
// pass the leadId which need to be converted
Leadconvert.setLeadId(leadid);
LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
Leadconvert.setConvertedStatus(convertStatus.MasterLabel);
Leadconvert.setCreateOpportunity(True);
Database.LeadConvertResult Leadconverts = Database.convertLead(Leadconvert);
System.assert(Leadconverts.isSuccess());
}
}
Thanks and regards,
Sunny
You can use setAccountId(accountId) method on the LeadConvert class. This way the lead will merge into the account and not create a new one.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dml_convertLead.htm
Thank you for your message mate. Unfortunately I am still a newbie with Apex programming stuff and learning out my way gradually.
Would you be able to guide me where do i need to use "setAccountId" method in my code? and does my code looks ok to you from our requirement perspective ?
Thanks in advance ! 👍
Are you linking the person Account to Lead?
If not, you will have to persist Account ID on the lead record which you created using Process Builder. You can create a custom field on Lead say(AccountID__c) and store the value of the Account which invoked the Process Builder.
Now in your Apex Class after Leadconvert.setCreateOpportunity(True); line you can write Leadconvert.setAccountId(AccountId value);
However, in order to this you should pass the lead object directly from trigger to apex class instead of just the ID.Also you should bulkify your code.
Instead of passing a single record in a loop, try passing a list or a set or lead records.
Thanks for your reply mate, and yes, I am trying to auto-convert and auto-close (as converted/won) Parent Account into Lead and Opportunity.
Can you please guide me with this please ?
Thanks so much!