1

I am stuck on this trigger.. i am trying to match Leads with existing accounts using a custom "domain" field. If an account exists with that same domain then i want to assign the incoming lead to the existing Account owner.

Here is my code so far, i am stuck on the last line, how do i get the account owner id?

trigger addAccount on Lead (before insert){

List<string> domain = new list<string>();

For (lead l:trigger.new){
  domain.add(l.domain__c);
}

List<Account> leadAccountIds = [Select Id, OwnerId, domain__c FROM Account WHERE domain__c IN: domain];

Map<String, Id> acctNameId=new Map<String, Id>();
Map<String, Id> acctNameOwner=new Map<String, Id>();

For (Account a:leadAccountIds){
  acctNameId.put(a.domain__c,a.Id);
  acctNameOwner.put(a.domain__c,a.ownerId);
}

For (Lead l2:trigger.new){
  if(acctNameId.containsKey(l2.domain__c)){
    l2.ownerId=acctNameOwner.get(a.ownerID);
  }
}
}

2 Answers 2

1

I think your problem is in acctNameOwner.get(a.ownerID) it should be acctNameOwner.get(l2.domain__c), but here is a more concise writeup of your trigger which should work.

trigger addAccount on Lead (before insert){

  Set<string> domain = new Set<string>();

  For (lead l:trigger.new){
    domain.add(l.domain__c);
  }

  List<Account> leadAccountIds = [Select Id, OwnerId, domain__c FROM Account WHERE domain__c IN: domain];

  Map<String, Account> accountsByDomain=new Map<String, Account>()

  For (Account a:leadAccountIds){
      accountsByDomain.put(a.domain__c,a);
  }

  for (Lead l2:trigger.new){
    if(accountsByDomain.containsKey(l2.domain__c)){
      l2.ownerId = accountsByDomain.get(l2.domain__c).ownerid;
    }
  }
}
2
  • Just incorporated Todd's great recommendation to use a Set instead of a list Commented Feb 10, 2016 at 21:55
  • 1
    One other thing to note is that this will throw an exception if the account owner is an inactive user, so be sure to handle that possibility with your code as well. Commented Feb 12, 2016 at 19:34
2

Looks like Sebastian beat me to it, but I'll still post my answer here.

You're quite close actually. I'd recommend a few tweaks and a couple points of caution.

First, gather your lead domains into a set rather than a list. This won't REALLY matter all that much, but generally if you want all of the different unique values, a set is the way to go.

Second, I'm not sure why you're creating two maps, but presumably you want to do some additional processing with the AccountId. If that's the case, I'd recommend doing what the other answer does and use a Map<String,Account> instead. That way you can get all the fields you need and do any additional processing without refactoring your code.

Also, best practice would be to use a handler class and move the logic out of the trigger. Either way, this could cause unexpected behavior if you have multiple Accounts with the same domain__c.

That said, your code should look something like this:

trigger addAccount on Lead (before insert){

    //Use a set instead of a list to gather all of the unique domains
    Set<String> leadDomains = new Set<String>();

    //Loop through leads to populate the set
    for(Lead l : trigger.new){
        leadDomains.add(l.domain__c);
    }

    //Query for accounts
    List<Account> leadAccounts = [Select Id, OwnerId, domain__c From Account Where domain__c = :leadDomains];

    //Create Map and loop through list to populate the map
    Map<String,Account> accMap = new Map<String,Account>();
    for(Account a : leadAccounts){
        accMap.put(a.domain__c,a);
    }

    //Loop through leads and assign correct owner from Map
    for(Lead l2 : trigger.new){
        if(accMap.containsKey(l2.domain__c)){
            l2.OwnerId = accMap.get(l2.domain__c).OwnerId;
        }
        else{
            //handle else condition if necessary
        }
    }

    //any additional processing here

}
0

You must log in to answer this question.

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