0

I have written a Trigger on Lead which updates the Owner of Lead to Contact Owner or Account Owner or a Queue.

  1. First checks for Contact (if criteria is true) then assigns to Contact Owner.
  2. If first criteria is not met checks for Account (if criteria is true) then assigns to Acc Owner.
  3. If both the criterias are not met then assigns to a Queue.

As I tested this it is perfectly working fine and don't see any case that the Owner field updating to NULL. However, just would like to know this would not end up in issues. Please let me know.

trigger UpdateLeadOwner on Lead (before insert) {
String LeadEmail, LeadDomain, Owner;
List<Contact> ConList = new List<Contact>();
List<Account> AccList = new List<Account>();
List<Lead> LeadList = new List<Lead>();
for(Lead L : Trigger.new)
{
  LeadEmail = L.Email;
  LeadDomain = L.Lead_Domain__c;
  Owner = L.OwnerID;
}

ConList = [SELECT OwnerId FROM Contact where Email =: LeadEmail];

if(ConList != NULL && ConList.size() > 0)
{
  Owner = ConList[0].OwnerId ;
  system.debug(Owner);
}
else
{
  AccList = [SELECT OwnerId  FROM Account where Domain__c =: LeadDomain];
  system.debug(AccList.size());
  if(AccList.size() > 0)
  {
    Owner = AccList[0].OwnerId ;
    system.debug(Owner);
  }
  else
  {
    // Below is the Unprocessed Queue ID 
    Owner = '00GN0000001Mscd';
        system.debug(Owner + 'I am executing');
  }
}

for(Lead L : Trigger.new)
{
//try{
  L.OwnerId = Owner;
  LeadList.add(L);
  //}
  //catch(Exception e)
  //{
    //L.adderror('The following error occured'+ e);
  //}
}
}

Thanks Sethu

  • 1
    this trigger has problems - it is not bulkified; trigger.new could contain many Leads – cropredy Feb 9 '16 at 4:39
  • 1
    Also you are hardcoding user ids; Owner = '00GN0000001Mscd – Salesforcesmarty Feb 9 '16 at 5:31
0

Logic has several issues:

//LeadEmail, LeadDomain and Owner will have value from the Last list in Trigger.New Index. 
//What about other lead records.

for(Lead L : Trigger.new)
{
  LeadEmail = L.Email;
  LeadDomain = L.Lead_Domain__c;
  Owner = L.OwnerID;
}

// Quering contacts having the Last lead email address in Trigger.New. //There will not be all contacts matching to Lead in Trigger.New
ConList = [SELECT OwnerId FROM Contact where Email =: LeadEmail];

// Similar to above here is only one LeadDomain
AccList = [SELECT OwnerId  FROM Account where Domain__c =: LeadDomain];

// This is non-reliable.
Owner = '00GN0000001Mscd';

In last, trigger must be able to handle multiple records so, it should be written using bulkified approach.

Code must be moved to apex class and call it from Trigger. Read about best practices when writing trigger.

  • Thank you for the detailed findings and the sources. I have bulkified my code completely now and is working perfectly for mass imports. – Kanikala Mar 2 '16 at 23:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.