0

Write a trigger on Lead to perform the following changes during insert and update.

  1. Assign the Lead as per Country field of User. Example : If Country of new Lead is India then assign this lead to User which country is India. If Country of new Lead is US then assign this lead to User which country is US.
  • tell me how can i solve this query – Ashish Jain Jun 24 '16 at 10:11
  • Hello Ashish, What have you tried so far? Have you got some code that you have tried? – developer__c Jun 24 '16 at 10:22
  • trigger userLead on Lead (after update) { list<lead> lstlead = new list<lead>(); list<user> lstUser = new list<user>(); set<id> leadId = new set<id>(); for(lead objlead : Trigger.New){ if((objlead.Country =='india') || (objlead.Country == 'us' )){ leadId.add(objlead.Id ); } list<user> lst1User = [select id, Country from user where id =: leadId ]; system.debug('+++++++++++'+lst1User ); for(user objUser : lst1User ){ if((objUser.Country =='india') || (objUser.Country == 'us' )){ } } } } – Ashish Jain Jun 24 '16 at 10:28
0

I would suggest you don't use a trigger and setup Lead Processes. Create a Queue for each country, assign users to that country queue. Then create a Lead Process for each country and set the process to assign it to the corresponding queue.

The problem you have with what you are proposing is if you assign based on the user country you might end up with one user getting all the leads assigned to them as they are the first SOQL result; unless you use a queue or write some pretty advanced apex to even distribute leads between users.

  • but my requirement is trigger. – Ashish Jain Jun 24 '16 at 10:43
  • 2
    Your requirement never is a trigger. Your requirement is handling a business process in sfdc. – Robin De Bondt Jun 24 '16 at 12:26
  • A good developer should be able to interpret requirements from non-technical writers; as Robin has highlighted the requirement is not for a trigger, but the writer wants an event to happen in a similar context to a trigger. We also know in SalesForce you should use every powerful declarative feature prior to writing code. – developer__c Jun 24 '16 at 12:33
0

If your requirement is Trigger then use it :

trigger AssignLeadsToUser on Lead (before insert){
List<User> userList = new List<User>();
set<String> Countryset = new set<String>();
for(Lead ld: trigger.new){
Countryset.add(ld.Country);
}
userList =[SELECT ID FROM User Where Country =:Countryset];
    for(Lead l:trigger.new){
        for(user u:userList){
            if(l.Country == u.Country && l.OwnerId ==null){
              l.OwnerId = u.Id;
            }
        }
    }
}

Regards,

Ajay

  • You have not mentioned what to do when more than one user belongs to same country ? – Ajay Dubedi Jun 24 '16 at 10:52
  • i agree, in that case this code will not work. but now i am using only single user. – Ashish Jain Jun 24 '16 at 11:02
  • It works on that case also but it assign the first user as Lead owner. – Ajay Dubedi Jun 24 '16 at 11:29
  • bdw thanks for your kind suggestation and i have noted your point . – Ashish Jain Jun 24 '16 at 11:51
  • Ajay, looks like you need to add Country to the Select, no? For performance reasons, I'd a a break after the assignment of OwnerId. – Jochen Jun 25 '16 at 19:20

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.