2

So I've written this trigger below. I am having issues with this trigger firing off when I import records. It's meant to reassign Lead owners from querying the system on certain factors, and it works fine inside Salesforce when you update or insert a record. The issue comes up when I try to insert via data loader.

To be clear, even if i insert one record via dataloader, the trigger does not fire.

Any insight into this issue would be great, thanks!

    trigger LeadReassignmentTrigger on Lead (before insert, before update) //after update before insert,
     {
    public Lead LeadRecordName {get;set;}
    public Contact contactInsert {get;set;}
    public QueueSobject Queue {get;set;}
    list<Id> LeadIdList = new list<Id>();
    public Account AccountRecordsName {get;set;}
    public Lead LeadInsert {get;set;}
    public integer Counter {get;set;}

      if(Lead_Trigger_Helper.runonce()){
    for(Lead leads : trigger.new)
    {
      LeadIdList.add(leads.id);

    } 


    try{
                LeadInsert = [SELECT id, 
                              email, 
                              Ownerid 
                              FROM Lead 
                              //where Id IN: Trigger.newMap.keySet()
                              where id in : LeadIdList 
                              //limit 1
                             ];
    }
    Catch(Exception e)

    {}
    try{
     contactInsert =        [SELECT email, 
                             Ownerid 
                             FROM Contact 
                             where email =: LeadInsert.email 
                             //limit 1
                             ];

    }
    catch(Exception e){

    }
//3.3

    //List<Contact> ContactRecords = [SELECT email, Ownerid FROM Contact where id in : LeadIdList];
    /*List<Lead> LeadRecords = [SELECT 
                              email, 
                              Lead.Lead_Lifecycle__c, 
                              id, 
                              ownerid 
                              FROM Lead 
                              where Id IN: Trigger.newMap.keySet()
                              //limit 1
                             ]; */


       // for (Lead LD: LeadRecords){
        for (Lead LD: Trigger.new){
            if(LD.Lead_Lifecycle__c == 'AQL' && LD.email == null){
                if(LD.Lead_Lifecycle__c == 'AQL' && LD.email == contactInsert.email){
                    if(LD.email != null){
     //List<Contact> ContactRecords = [SELECT email FROM Contact];
     //List<Lead> LeadRecords = [SELECT email FROM Lead where email =:ContactRecords.email];
             LD.OwnerId = contactInsert.ownerid;

                }

                 }

           // } 

            }
            //leadrecords.add()
        }

    //update LeadRecords;


//3.3, number 2 FSR 

   /* List<Lead> LeadRecordsName = [SELECT email,
                                  Lead.Lead_Lifecycle__c, 
                                  id, ownerid, 
                                  Company, 
                                  name 
                                  FROM Lead 
                                  where Id IN: Trigger.newMap.keySet()]; */

                Queue =           [SELECT Id,
                                  queue.Name, 
                                  QueueId 
                                  FROM QueueSobject 
                                  WHERE queue.Name = :'VP Queue'
                                  limit 1];
    try{
                 LeadRecordName = [SELECT email, 
                                  Lead.Lead_Lifecycle__c, 
                                  id, 
                                  ownerid, 
                                  Company, 
                                  name 
                                  FROM Lead 
                                  where Id IN: Trigger.newMap.keySet()
                                  //limit 1
                                  ];
    }
          Catch (Exception e)
            {}


     try{
                AccountRecordsName = 
                                  [SELECT id, 
                                  Name, 
                                  ownerid 
                                  FROM Account 
                                  where Name =: LeadRecordName.Company
                                  //limit 1
                                  ];
     }
            Catch (Exception e)
            {}
    try{
                 counter = 
                                 [SELECT 
                                  Count() 
                                  FROM Accountteammember 
                                  where accountid=:AccountRecordsName.id 
                                  ];
    }
    catch (Exception e){}

        //for (Lead LD2: LeadRecordsName){
    for (Lead LD2: Trigger.new){

     //List<Lead> LeadRecordsName = [SELECT email, Lead.Lead_Lifecycle__c, id, ownerid, Company, name FROM Lead where Id IN: Trigger.newMap.keySet()];
        if(LD2.Company != null && counter != null)
        {
        if(LD2.Company == AccountRecordsName.Name && counter == 1)
        {
            if(LD2.Lead_Lifecycle__c == 'AQL'){

            try{
              Accountteammember AccountTeamMemberInfo = [SELECT 
                                               accountid, 
                                               userid,
                                               TeamMemberRole
                                               from Accountteammember 
                                               where accountid=:AccountRecordsName.id
                                               limit 1];


        LD2.ownerid = AccountTeamMemberInfo.userid;
            }
            catch (Exception e){}
            }
        }

        if(LD2.Company == AccountRecordsName.Name && counter > 1)
            if(LD2.Lead_Lifecycle__c == 'AQL'){ 
                //&&
        {
                        //try{
             /*  Accountteammember AccountTeamMemberInfo = [SELECT 
                                               accountid, 
                                               userid,
                                               TeamMemberRole
                                               from Accountteammember 
                                               where accountid=:AccountRecordsName.id
                                               ]; */


           // if(AccountTeamMemberInfo.TeamMemberRole == 'FSR')
           // {            
            //Add FSR ISR Logic
            //Finish Query. Test?


                //}             
            //}

             //catch (Exception e){}

            LD2.ownerid = queue.QueueId;

                        }
                    }

                }

            }

        }


      }


    //3.4, ISR
  • 1
    A few things jump out at me: 1. Your trigger comment states after update and before insert but your trigger is set to only fire (after update) 2. You do not need to create a set of Ids as the trigger.newMap has the keytset available for you. 3. You are querying for a single lead record and then a single contact record but not sure why or where you are using that. 4. Then you are using the keyset to get the lead records but limiting it to 1... You might want to take a step back and read more about bulkification & triggers. – Jenny B Mar 11 '16 at 16:53
  • 1. Yes I took those out because I was receiving errors when I was attempting to fix the issue. 2. Thank you for that, I was not aware. 3. The Contact query is to check if there is a contact with the same email as the lead. 4. Thanks for spotting that! I'll go back and change that. I have read some about bulkification and have tried to replicate, however i am fairly new to Apex. Thank you for the feedback – Christian Mar 11 '16 at 17:06
  • Unfortunately that will only tell you if it's the same contact as the 1st lead but not the rest of them. I would suggest querying for all contacts then creating a map of email to contact then loop through the leads and use the email to see if the contact exists. What is the business requirement? Is it something that could be accomplished with Process Builder? – Jenny B Mar 11 '16 at 17:11
  • Hrmm..Not sure if it could be accomplished with process builder. Basically for 1 scenario they want a lead to be reassigned if the status is = AQL and if the lead matches an existing contact ID. That lead should be reassigned to the contact owner. There are more business requirements around account teams and reassigning them, and I'm not sure if process builder could handle that. If it could, that'd be great and I could just use that – Christian Mar 11 '16 at 17:42
  • 2
    @Christian -- by way of SFSE etiquette - formatting your code better would get you more assistance from the community. It is really hard to read; remove the commented out lines, the blank lines, simpler indenting – cropredy Mar 16 '16 at 1:46
1

A couple things I would suggest:

  • Break this down and get one task accomplished at a time even if in a developer org or a separate sandbox.
  • You do not need to use the getter and setter methods
  • Before insert the Id of the lead will not be available so the keyset will be null and your leadidlist will not have any ids.
  • Eliminate all of your try and catch blocks and let the system handle it since you are really just eliminating errors and not handling them.
  • You are still only grabbing the first email, assuming this is not desired to use the 1st email on a lead for all remaining leads being inserted.
  • Add a system.debug at the beginning of your trigger and you should see that the trigger is indeed running but you are not getting the desired results

I would do suggest doing something like:

  1. Create a map of emails to the corresponding lead record.
  2. Use the email keyset to query the matching contacts.
  3. Create a map of emails to contactOwnerId
  4. Reloop through the new leads and check the contact email map to see if it contains the lead email and update the ownerid.

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.