0

Below is the code that I use for task creation

        Task cv = new Task();
        cv.Subject = 'Email:' + EmailSubject;
        cv.WhoId = toContactId;
        cv.WhatId = OppId;
        cv.Status = 'Completed';
        cv.Priority = 'Normal';
        cvTask.add(cv);
        insert cvTask;

This was working fine, but my problem is the task is related to

WhatId - Opportunity, WhoId - Contact

But it is inserting two task for contact. one related to opportunity and another one without related to. Any ideas?

3
  • 1
    can you share ur complete code ? Oct 22, 2015 at 11:18
  • To followup on @VamsiKrishna, where is the cvTask variable being instantiated? I suspect there is already 1 task in that list, then you added 1 more. That's where you get 2 tasks.
    – James
    Oct 22, 2015 at 13:29
  • CVTask is instantiated outside the loop.. I checked with debug logs too. .. the task inserted onlt once.. But looks like the problem is with the who and whatId.. what ID - opportuntity and who Id - contact.. for opportunity it is inserted once.. for who id inserted twice one with related to and another one without related too.. Oct 24, 2015 at 4:09

1 Answer 1

0
 for(String toContactId : toAddressesList) {
        List<Messaging.SingleEmailMessage> msgList = new List<Messaging.SingleEmailMessage>();
        Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
        msg.setTargetObjectId(toContactId);
        msg.setFileAttachments(fileList);
        msg.setHTMLBody(htmlEmailBody);
        msg.setSubject(EmailSubject);
        msgList.add(msg);

        cv = new Task();
        cv.Subject = 'Email:' + EmailSubject;
        cv.WhoId = toContactId;
        cv.WhatId = OppId;
        cv.Status = 'Completed';
        cv.Priority = 'Normal';
        cvTask.add(cv); 

        Messaging.sendEmail(msgList); 
    }  
     if(!cvTask.IsEmpty()){
         insert cvTask;

        List<Attachment> attach = new List<Attachment>(); 
        for(Task iTask : cvTask)
        {
            Attachment attachment = new Attachment();
            attachment.Body = pdf.getContent();
            attachment.Name = EmailSubject;
            attachment.Description = htmlEmailBody;
            attachment.ParentId = iTask.Id;
            attach.add(attachment);
        } 
        if(!attach.IsEmpty())
        {
            insert attach;
        }

}

You must log in to answer this question.

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