1

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?

1
  • I dont even have 'Enforce Validation and Triggers for Lead Convert' checkbox under my lead settings in salesforce
    – user905
    Commented Jan 24, 2018 at 11:30

1 Answer 1

2

I would suggest use a After update lead trigger, then you'll have all the info from the lead in the trigger.new and you can query all the account id's since your leads will have the ConvertedAccountId

How to map lead custom field with Standard Account field 'Parent Account'

in this question you'll have some example I provided to another use on a "somewhat similar case" although not the same. I think After update Leads are the most secure way to do it since then you ensure the whole conversion has been done successfully and then you can update the account info Safely.

In your case is far easier, since you only have to overwrite the OwnerId's and that's it.

Here's how I would adapt your code into this principle with all the info you provided.

public class ChangeLeadOwnerHandler { 


    Map<Id,Lead> accountsToCheck = new Map<Id,Lead>(); 

    public static void updateLeadOwner(List<Lead> leads) { 
        Map<Id,Id> leadAccount = new Map<Id,Id>(); 
        Map<Id,Lead> accountsToCheck = new Map<Id,Lead>(); 
        try { 
            Map<Id,Lead> oldMap = (Map<Id,Lead>) trigger.oldMap; 
            for (Lead l : (List<Lead>) trigger.new){ 
                if(l.isConverted != oldMap.get(l.id).isConverted){ 
                    if(l.convertedAccountId != null){ 
                        accountsToCheck.put(l.ConvertedAccountId, new Lead(Type_of_Space__c = l.Type_of_Space__c, Centre__c = l.Centre__c)); 
                        leadAccount.put(l.Id, l.ConvertedAccountId); 
                    } 
                } 
            } 

            String centreName = ''; 
            Set<String> typeOfSpace = new Set<String> {'Day Pass','Virtual Office'}; 
            Map<String, Centre__c> centreMap = new Map<String, Centre__c>(); 
            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); 
            } 
            Map<Id, User> userMap = new Map<Id, User> ([SELECT Id, Name FROM User]); 
            List<Account> recordsToUpdate = new List<Account>(); 
            for(Lead lead : leads) { 
                if(typeOfSpace.contains(lead.Type_of_Space__c)) { 
                    centreName = lead.Centre_Name__c; 
                    System.debug('lead centre name: ' + lead.Centre_Name__c ); 
                    Centre__c centre = 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); 
                    if(leadAccount.containsKey(lead.Id)) { 
                        String accID = leadAccount.get(lead.Id); 
                        if(userId == accID) continue; 
                        recordsToUpdate.add(new Account( Id = lead.convertedAccountId, Ownerid = accID)); 
                    } 
                } 
            } 
            update recordsToUpdate; 
            }Catch(Exception e) { 
                System.debug('Exception occured: ' + e.getLineNumber() + ' ' + e.getStackTraceString() + ' ' + e.getMessage()); 
            } 
        } 
    }
7
  • I changed a few things in the after update lead trigger and ran that but was not able to see any logs for that trigger. One more thing I would like to tell here is that..between the lead conversion and account creation there is a visualforce page in my system which is being called on the lead convert button. (Somebody previously has overridden that button to call that page and provide some mandatory details. ) on that VF page I have the standard convert button.. and as I mentioned I am nt able to see any logs.
    – user905
    Commented Jan 24, 2018 at 12:00
  • the trigger runs when some updates happen on the lead but not on the convert button. Should I share my code?
    – user905
    Commented Jan 24, 2018 at 12:12
  • If you still have some troubles related to this specific problem (not the trigger not firing like we discussed on the chat) tell me about it here so we can try close this question. Commented Jan 25, 2018 at 9:13
  • hey @Alexander Aeons Torn: its Working.. thank you so much for your help.
    – user905
    Commented Feb 7, 2018 at 7:27
  • You're welcome. If it's working confirm the answer as correct so it can help other people as well :) . Commented Feb 7, 2018 at 9:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .