0

I am getting all the contact Ids in SetOfId and I want to relate one task to multiple contact.

public Task_Controller() {
    if (ApexPages.currentPage().getParameters().get('SetOfId') != null) {
        for (String s: ApexPages.currentPage().getParameters().get('SetOfId').split(',')) {
            SetOfId.add(s);
            System.debug('SetOfId is####' + SetOfId);
        }
    }
    create_task();
}
public void create_task() {
    // Instanciate Object
    tskNew = new Task();

    // Pre-populate Fields
        tskNew.WhoId =
        tskNew.WhatId =
        tskNew.Subject =
        tskNew.OwnerId = userInfo.getUserId();
}    
public pagereference save() {
    insert tsk;
    return stdController.save();
}

Something similar to this:

  List < id > listOfCon = new List < id > ();
     for (Contact c: [select id from Contact where Id in: SetOfId]) {
         listOfCon.add(c.id);
         System.debug('listOfCon is####' + listOfCon);
     }

then

for (Contact c : listOfCon){
 tsk = new Task();
}
1
  • have you looked into TaskRelation object ?
    – Ratan Paul
    Commented Feb 27, 2017 at 14:13

5 Answers 5

5

Bit late but for other people having the same question. This is possible. First you need to create the task. After that you can loop through the contact Id's in your list and for every Id make the following insert.

TaskRelation tskr = new TaskRelation();
tskr.TaskId = 'task_id';
tskr.IsWhat = False;
tskr.RelationId = 'contact_id';
insert tskr;
1
  • Perfect! That's what I was looking for. Commented Oct 4, 2021 at 10:47
0

If I understand you right, you are trying to relate one task to many contact? If that's the case, that's simply not possible. A task is the child in a one to many relationship and can only relate to one contact/lead etc.

1
  • 2
    I dunno why we got downvoted
    – EricSSH
    Commented Feb 15, 2017 at 17:53
0

That isn't possible, But what you can do is create a new object and call it something like Task Contacts from your Contacts create a Lookup to Task Contacts now you will have a related List on your Task Contacts, associate your Task Contacts to the Task WhatID..

0

I don't know if this is possible in APEX but it's definitely possible via the interface - it's a feature which is enabled by speaking to Salesforce to turn this on. I'd love to know how the data is stored though so I can reference it, given that the Who Id is just that - an ID and not a list.

0
0

I don't know if you can insert data as I haven't tried that yet, but you can reference the data as it's in a table called TaskRelation. e.g. :

SELECT Id, RelationId, TaskId, IsWhat, AccountId FROM TaskRelation where TaskID = ''

(just insert your required task id in the obvious place :) )

Thanks to a quick response to my query in the salesforce success community :)

https://success.salesforce.com/answers?id=9063A000000DgFi

You must log in to answer this question.

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