0

i have crate a multiple assignment task for same opportunity with different users. i have write this code for task creation

public PageReference saveTask(){
    if(IdTask.size()>0){
        List<Task> taskList = new List<Task>();            
        for(String str : IdTask){
            newTask.OwnerId=str;
            taskList.add(newTask);
        }
        try{
            insert taskList;
        }catch(DMLException e){

        }
        PageReference parentPage = new PageReference('/' + WhatId);
        parentPage.setRedirect(true);
        return parentPage;
    }
    return null;        
}

but getting a error. but owner id different.

System.ListException: Before Insert or Upsert list must not have two identically equal elements

Visualforce:

<apex:PageBlockSection title="Task Information">
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Assigned To</apex:outputLabel>
                <apex:outputLabel style="border-left-color: #c00;">
                    <apex:outputLabel >{!assignedToLable}<br/></apex:outputLabel>
                    <apex:outputLabel style="font-weight:bold;">{!assignedToTaskSize}</apex:outputLabel>
                </apex:outputLabel>                    
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Related To</apex:outputLabel>
                <apex:inputField value="{!newTask.WhatId}"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Subject</apex:outputLabel>
                <apex:inputField value="{!newTask.Subject}"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Name</apex:outputLabel>
                <apex:inputField value="{!newTask.WhoId}"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Type</apex:outputLabel>
                <apex:inputField value="{!newTask.Type}"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Due Date</apex:outputLabel>
                <apex:inputField value="{!newTask.ActivityDate}"/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                <apex:outputLabel >Comments</apex:outputLabel>
                <apex:inputField value="{!newTask.Description}" />
            </apex:pageBlockSectionItem>
        </apex:PageBlockSection>

pls help me out.

1 Answer 1

1

You are adding a single Task object multiple times to your list. Instead you need to create a new Task object each time:

for (Id id : IdTask) {
     Task t = new Task(OwnerId = id, ...);
     taskList.add(t);
}

You will also need to set other Task fields i.e. replace the ... with other assignments. Or you can clone a base Task.

4
  • Hi @Keith Thanks for reply. i have create task object which are reference to Vf page. similar to the Standard task page. i want to change only Owner id rest of the information are same. Oct 19, 2016 at 13:22
  • @GmDeepakChounan21 Still unclear to me exactly what you are trying to do. Suggest you edit your question to describe that in more detail.
    – Keith C
    Oct 19, 2016 at 13:31
  • @GmDeepakChounan21 You still need to create new Task instances. Right now, you're adding the same Task instance to the list over and over. Because it's the same instance, when you go to change the owner id, you actually end up modifying the owner id of the records already in the list. Thus, you get your error about inserting identical items in the list. You can verify this by putting your list in a debug statement (and then inspecting the log).
    – Derek F
    Oct 19, 2016 at 13:32
  • Hi @Ketih It's work fine ... :) Oct 20, 2016 at 6:55

You must log in to answer this question.

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