i have one requirement regarding Create task Automatically based on the Picklist value and that Picklist value Have some users..

  • Opportunity.PICK__c = User1, User2, User3, User4 ..........

  • When this Opportunity saved then Automatically Task has to be created with the Assignee of this User (Opportunity.PICK__c)..

  • this is working with the Workflow rule but it's a Static and i want it be to working with Dynamically

  • just because of in the future if User's are more than 100 then.....

Please Help me out....

Thanks a LOT !!

  • 1
    Nihar -- How are you creating the picklist? hard coded as metadata? There is a max on # of entries in a picklist and when you enumerate users who come and go, it becomes a maintenance nightmare – cropredy Apr 23 '16 at 6:25
  • Yes. but i've not created myself that Picklist with the Specific user, it's created byt client and they only want to use this kind of picklist...Thanks – Nihar Apr 23 '16 at 17:23
  • so, it is a standard field picklist? How will the values in the picklist be matched against actual User records? By which field in User? It will have to be an exact match - perfect spelling fidelity. If a user's name changes (typical example - marriage/divorce), then the picklist becomes out of sync with the User record) – cropredy Apr 23 '16 at 18:20
  • Yes.i got your point and it's a custom picklist not an standard – Nihar Apr 24 '16 at 0:09

I coded and tested a apex trigger according to your use case. Hope its help.

    trigger TaskUpdate on Opportunity (After insert,After update) {

//loop through all new opportunities  saved
List<task> tl= new list<task>();
Task T = new Task();
List<User> ul=[Select Id,Name from User];

    for(Opportunity Op: Trigger.new)

        T.Type = 'Email';
        T.Description = 'DEMO String';
        T.Priority= 'Low';
        T.Subject='demo subject';
        T.Status='Not Started';
        String userN = Op.PICK__c;
        for(integer i=0;i<ul.size();i++)
            If(ul[i].Name == userN )
                T.OwnerId = ul[i].Id;
        //    T.OwnerId = [Select Id from User where Name =:userN Limit 1].Id;
        T.WhatId = Op.Id; 
 insert tl;    
    // Optional if you want to change assign task owner is picklist vlaue is change.

You can code what to do in update condition according to your use case.

  • 1
    this answer is not bulkified; you are doing soql inside of for loop – cropredy Apr 23 '16 at 6:23
  • Agree with Cropdy...Rachit, can you please bulkified this trigger ? – Nihar Apr 23 '16 at 17:24
  • OKAY Done. Hope it helps. Please do let me know @Nihar – Rachit Apr 25 '16 at 6:32

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.