0

I'm trying to override the standard log a call functionality but with some changes. When creating a log a call record it will be possible to select multiple users and multiple contacts. I've created the page with a possibility to select multiple contacts and multiple Users. But now i'm stuck with trigger.

After the user selected multiple users the id's of selected populates in a field UserListField__c (The values are devines with ,), the same is with ContactListField__c. I don't really know how to fire trigger to create multiple records based on UserListField__c.

This is what i've done for now:

trigger MultiTaskCreate on Task(before insert) {
    //Trying to Make the List of Users Id's instead of string
    List < String > UsrId = new List < String > ();
    for (Task task: trigger.new) {
        UsrId.add(task.UserListField__c);
    }
    //Creating new Tasks records
    for (Task ts: trigger.new) {
        If(UsrId != NULL) {
            Task task = new Task();
            task.Assigned_To__c = UsrId; //Using the Lists of Id's
            task.Status = 'Completed';
            task.Priority = ts.Priority;
            task.Communication_Type__c = ts.Communication_Type__c;
            task.Subject = ts.Subject;
            task.Name__c = ts.Name__c;
            insert task;
        }
    }
}
3
  • What deliminator are you using? Is it a ,(Comma) or something else? Could you paste one of the field values? Jun 27, 2016 at 9:37
  • @ShaileshPatil Yes it is a comma (,). The example: 0037E000007mabaQAA,0037E000007mabpQAA
    – Viktor
    Jun 27, 2016 at 9:39
  • See if the answer helps. Let me know if you need any details in the code. Jun 27, 2016 at 9:56

1 Answer 1

2

Here, you have to use the Split function on the string which will break the string in the set of words using the deliminator and return you a list. Try this code:

    trigger MultiTaskCreate on Task (before insert) {

Map<task, List<Id>> taskToUsersIds = new Map<task, List<Id>>();
for(Task ts: trigger.new){
    if(ts.UserListField__c != null){
            taskToUsersIds.put(ts, ts.UserListField__c.split(','));
        }
}
List<task> taskToInsert = new List<task>();
for(Task t : taskToUsersIds.keySet())
{
    for(String userId : taskToUsersIds.get(t))
    {
            Task task = new Task();
            task.Assigned_To__c = userId; //You will get the UserId here
            task.Status = 'Completed';
            task.Priority = ts.Priority;
            task.Communication_Type__c = ts.Communication_Type__c;
            task.Subject = ts.Subject;
            task.Name__c = ts.Name__c;
            taskToInsert.add(task);

    }

}
 if(taskToInsert.size() > 0)
    insert taskToInsert;
}
6
  • Thaks for the answer, but it still doesn' work. The task.UserListField__c.split(',')) gives me an error in console that method doesn't exists. I make some changes: Add String UsrIds = string.valueOf(task.UserListField__c); String[] USplit = UsrIds.split(','); and replace task.UserListField__c.split(',')) with USplit. When i save task record it throw me an error: MultiTaskCreate: maximum trigger depth exceeded Task trigger event BeforeInsert for [new] Task trigger event BeforeInsert for [new] Task trigger event BeforeInsert for [new]
    – Viktor
    Jun 27, 2016 at 10:26
  • What is the field type of "UserListField__c"? Jun 27, 2016 at 10:34
  • This is a text area.
    – Viktor
    Jun 27, 2016 at 10:41
  • Check updated answer. Let me know if it works. Jun 27, 2016 at 10:42
  • It show error System.NullPointerException when trying to save in this line taskToUsersIds.put(ts, ts.UserListField__c.split(','));
    – Viktor
    Jun 27, 2016 at 11:01

You must log in to answer this question.

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