+ Start a Discussion
Gita BorovskyGita Borovsky 

Can't figure out where to put my line of code

I have a bit of code that checks to see if an account already exists based on a domain and reassigns the incoming lead based on Account owner. I need to update the trigger to leave a particular Account Owner Role out of the process but I can't figure out where to put the code.  Any help would be appreciated, thanks!  Here's the owner role id: 00E50000000t0Bf and here's the trigger:

trigger UpdateLeadOwner on Lead (before insert) {
    for (Lead leadInLoop : Trigger.new) {
   
if (leadInLoop.LeadSource != 'Contact Us' && leadInLoop.LeadSource != 'Reprint') {
    // Retrieve owner from Account record based on email domain name
    list <Account> acct = [Select OwnerId from Account WHERE Domain_1__c = :leadInLoop.Domain__c Limit 2];
        if (acct.size() == 1) {

    //the next line is for debugging, to check that you are getting a result back from the query
    System.debug('account owner id is: ' + acct[0].OwnerId);
    leadInLoop.OwnerId=acct[0].OwnerId;
        }
    }
}
}
Best Answer chosen by Gita Borovsky
sbbsbb
I am not sure if I understood your question correctly, but I am assuming you don't want to reassign the lead if the account owner has that particular role. If that is the case, you can just change your query to:

Select OwnerId from Account WHERE Domain_1__c = :leadInLoop.Domain__c AND Owner.UserRoleId != '00E50000000t0Bf' Limit 2];


All Answers

sbbsbb
I am not sure if I understood your question correctly, but I am assuming you don't want to reassign the lead if the account owner has that particular role. If that is the case, you can just change your query to:

Select OwnerId from Account WHERE Domain_1__c = :leadInLoop.Domain__c AND Owner.UserRoleId != '00E50000000t0Bf' Limit 2];


This was selected as the best answer
Gita BorovskyGita Borovsky
That did it. Thanks so much!
sbbsbb
Great! Could you please mark the question resolved? Thanks.