I need to somehow change owners of legacy and new leads to the owner of the account, if that related account already exists in the system. Is there a way to do this using workflows or process builder? Also i want to be able to assign the account owner as the lead owner and mark that lead converted.
1 Answer
As Tushar mentioned, this is possible using Process Builder.
You will have to write an Apex Class with "invokable" method and include that class in your process builder step.
Possible code snippet will look like
Global class LeadUtils
{
@InvocableMethod
public static void AccountValidated(List<Id> leadIds)
{
List<Lead> leads = new List<Lead>();
List<Database.LeadConvert> lcList = new List<Database.LeadConvert>();
LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
for(Lead leadR : [Select id,ownerID,Account__c,Account__r.ownerid From Lead where ID IN :leadIds Limit :leadIds.size() ]){
if(leadR.Account__c != Null)
leadR.ownerID = leadR.Account__r.ownerId;
leads.add(leadR);
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(leadR.id);
lc.setConvertedStatus(convertStatus.MasterLabel);
lcList.add(lc);
}
update leads;
List<Database.LeadConvertResult> lcr = Database.convertLead(lcList);
}
}