0

I wrote an apex trigger, which is suppose to trigger on new lead creation and assign to HR/Recruiter field pick list value. In sandbox it works 100% correctly and it evenly distributes leads between HR/Recruiters. In production it always selects one person more often. HR/Recruiters = {Jose, Michael, Kate} (pick list values) Here is my code:

Trigger updateHrRecruiterOnLead on Lead(after insert) {

    for (Lead lead:Trigger.new) {

        if(checkRecursive.runOnce2()) {

            if (lead.HR_Recruiter__c == null && lead.Record_Type_ID__c != 'xyz') {

                Schema.SObjectType schemaOfLeadGlobal = Schema.getGlobalDescribe().get('Lead');
                Schema.DescribeSObjectResult schemaOfLeadDescribe = schemaOfLeadGlobal.getDescribe();

                Map<String, Schema.SObjectField> leadFields = schemaOfLeadDescribe.fields.getMap();

                Schema.DescribeFieldResult fieldResult = leadFields.get('HR_Recruiter__c').getDescribe();

                List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

                List<String> allActiveHrRecruiterNames = new List<String>();

                for (Schema.PicklistEntry singleValues:ple) {

                    if (singleValues.isActive()) {

                        allActiveHrRecruiterNames.add(singleValues.getLabel());

                    }

                }

                Integer numberOfActiveRecruiters = allActiveHrRecruiterNames.size();

                List<AggregateResult> searchIfAllRecruitersWereUsedInTodaysLeads = [SELECT COUNT(Id), HR_Recruiter__c FROM Lead WHERE CreatedDate >=: Date.today() AND Record_Type_ID__c != 'xyz' AND HR_Recruiter__c != null GROUP BY HR_Recruiter__c ORDER BY HR_Recruiter__c ASC];

                Integer numberOfAllRecruitersWereUsedInTodaysLeads = searchIfAllRecruitersWereUsedInTodaysLeads.size();

                List<Lead> updateLead = new List<Lead>();
                Boolean createLeadForRecruiter = true;
                String nameOfHrPerson = null;

                if (numberOfActiveRecruiters == numberOfAllRecruitersWereUsedInTodaysLeads) {

                    List<AggregateResult> searchTodaysAllAssignedLeads = [SELECT COUNT(Id), HR_Recruiter__c FROM Lead WHERE CreatedDate >=: Date.today() AND Record_Type_ID__c != 'xyz' AND HR_Recruiter__c != null GROUP BY HR_Recruiter__c ORDER BY COUNT(Id) ASC LIMIT 1];

                    nameOfHrPerson = String.valueof(searchTodaysAllAssignedLeads[0].get('HR_Recruiter__c'));

                    Lead thisLead = new Lead(Id=lead.Id,HR_Recruiter__c=nameOfHrPerson);
                    updateLead.add(thisLead);

                } else {

                    for (String singleActiveRecruiterName:allActiveHrRecruiterNames) {

                        List<AggregateResult> searchTodaysAllAssignedLeads = [SELECT COUNT(Id), HR_Recruiter__c FROM Lead WHERE CreatedDate >=: Date.today() AND Record_Type_ID__c != 'xyz' AND HR_Recruiter__c =: singleActiveRecruiterName GROUP BY HR_Recruiter__c ORDER BY COUNT(Id) ASC LIMIT 1];

                        if (searchTodaysAllAssignedLeads.size() == 0 && createLeadForRecruiter) {

                            Lead thisLead = new Lead(Id=lead.Id, HR_Recruiter__c=singleActiveRecruiterName);
                            updateLead.add(thisLead);
                            createLeadForRecruiter = false;

                        }
                    
                    }

                }

                update updateLead;

            }

        }

    }

}

This is making sure that code is run only once:

checkRecursive.runOnce2()
  1. I am getting names of active recruiters
  2. I am getting names of recruiters already used today
  3. I am counting how many names I have in 1 and 2
  4. If number of active recruiters is equal to recruiters used today. I am checking which recruiters has smallest number of leads and update lead with his/her name.
  5. If numbers do not match then I use for loop to see if recruiter has leads (from point 1) if no then I assign the name to lead. Yes is impossible because 1 and 2 are not equal. So I have to encounter name that does not have lead.

I can not understand why this is not working and "Jose" ends up with more leads. Any idea would be very appreciated.

Thanks

1 Answer 1

0

After changing the code multiple times..., I finally realized that there is no error and leads are distributed correctly. My issue was that reports in SF return data based on time zone of the SF user running the report. But APEX code and SOQL is based on the UTC time zone. That is why I was getting different numbers. I hope this code will help someone.

You must log in to answer this question.

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