I want to achieve a scenario where as soon as a lead is converted to an account I want to change the account owner, one thing here is that as soon as the lead gets converted to the account the round robin assignment starts and that's how the account owner is decided. My first problem is that I do not have any idea on how to check the round robin assignment so that I can try to change that.. second thing is: I want to change the account owner (rather than letting the round robin process do it.) to a user based on my condition.
So I am trying to write a before insert/update trigger on account to make sure as soon as an account is getting created I change the owner.
But somehow I am not able to achieve this. I am able to change the account owner for an after update trigger but the before trigger is not even running on this.
Here is my code for changing the account owner:
trigger UpdateLeadOnAccount on Account (before insert, before update) {
System.debug('reached in before insert account trigger!');
System.debug('run once value is: ' + UpdateLeadOnAccountHandler.runOnce());
if(UpdateLeadOnAccountHandler.runOnce()) {
if(trigger.isBefore) {
UpdateLeadOnAccountHandler.updateLead(Trigger.new);
}
}
}
public class UpdateLeadOnAccountHandler {
private static boolean run = true;
public static boolean runOnce(){
if(run) {
run = false;
return true;
} else {
return run;
}
}
public static void updateLead(List<Account> accounts) {
System.debug('reached in before insert account trigger method handler!');
Set<Id> accountId = new Set<Id>();
for(Account acc : accounts) {
accountId.add(acc.Id);
}
String centreName = '';
Set<String> typeOfSpace = new Set<String> {'Day Pass','Virtual Office'};
Map<String, Object> centreMap = new Map<String, Object>();
List<Lead> leadList = [SELECT Id,
Name,
Type_of_Space__c,
Centre_Name__c,
ConvertedAccountId,
isconverted
FROM Lead
WHERE ConvertedAccountId IN: accountId
AND isconverted = TRUE];
System.debug('lead list is: ' + leadList);
Map<String, Lead> accountToLeadMap = new Map<String, Lead>();
for(Lead lead : leadList) {
accountToLeadMap.put(lead.ConvertedAccountId, lead);
}
System.debug('account to lead map: ' + accountToLeadMap);
List<Centre__c> centreList = [SELECT Id,
Name,
Community_Manager__c,
City_Code__c
FROM Centre__c ];
for(Centre__c centre : centreList) {
centreMap.put(centre.Name, centre);
}
System.debug('centre map is: ' + centreMap);
Map<Id, User> userMap = new Map<Id, User> ([SELECT Id, Name FROM User]);
List<Account> accntListToUpdate = new List<Account>();
for(Account acc : accounts) {
if(accountToLeadMap.containsKey(acc.Id)) {
System.debug('came insidejdbsndanda::::');
Lead leadObj = accountToLeadMap.get(acc.Id);
System.debug('lead is: ' + leadObj);
if(typeOfSpace.contains(leadObj.Type_of_Space__c)) {
centreName = leadObj.Centre_Name__c;
System.debug('lead centre name: ' + leadObj.Centre_Name__c );
Centre__c centre = (Centre__c)centreMap.get(centreName);
String userId = centre.Community_Manager__c;
User u = userMap.get(userId);
System.debug('userId is: ' + u.Id + ' user name is: ' + u.name);
//acc(id = acc.id, Ownerid = userId);
accntListToUpdate.add(new Account(id = acc.id, Ownerid = userId));
}
}
}
if(accntListToUpdate.size() > 0 && accntListToUpdate != null)
update accntListToUpdate;
}
}
**Initially I did not update the accntListToUpdate list since we don't need to save in before trigger. but the changes were not reflecting. so I just tried doing this for after trigger..
But I want to achieve this(Changing account owner) as soon as the account gets created.
I am not getting why this before trigger is not running. I asked a question for a similar scenario a day or two before.. but I had to change the owner after the lead is converted to an account (or when the lead is being converted to an account and not on the existing lead).
The other question is: how do u find out how is round robin is assigning the lead.. where and how can I override it.
Doubt: I just read somewhere that --->
Lead Before Update Triggers (isConverted = false, convertedAccountId = null, convertContactId = null, convertedOpportunityId = null)
does that mean I can not fetch the lead for that account (which is being converted from lead to account). My main condition for changing the account owner depends upon some field values from lead.
NOTE: just found out this: Before insert triggers (at least on Account/Contact) do NOT currently work on Lead conversion. Use after insert triggers instead if you will be doing any Lead conversion.
https://developer.salesforce.com/forums/?id=906F00000008v1rIAA
Any help with this?