0

I wanna create a trigger on Task created on a Contact that automatically creates a new task and assign it to related opportunity.

How can I query the contact for the related opportunity?

Trigger code :

trigger CreateNewTaskOnOpportunity on Task (after insert) {

    CreateNewTaskOnOpportunityHandler obj1 = new CreateNewTaskOnOpportunityHandler();
    obj1.onAfterInsert(Trigger.newMap);

}

Class :

 public with sharing class CreateNewTaskOnOpportunityHandler {

    public void onAfterInsert(List<Task> lstTask)
    {
        Set<Id> contactId = new Set<Id>();
        for(Task t:lstTask)
        {
            if(String.valueOf(t.whatId).startsWith('003')==true)
            {
                contactId.add(t.whatId);
            }
        }

        List<Contact> lstContact = [Select Id,LastName,(Select OpportunityId From OpportunityContactRoles) From Contact where Id in : contactId];
        List<OpportunityContactRole> oppContactRole = new List<OpportunityContactRole>();
        for(Contact con: lstContact)
        {
            for(OpportunityContactRole opp: con.OpportunityContactRoles)
            {
                oppContactRole.add(opp);
            }
            //opportunityId.add(lstContact.OpportunityContactRoles.OpportunityId);

        }


    }

}
5
  • Can you post the code that you've already tried it with?
    – Nick C
    Commented Jul 19, 2016 at 10:55
  • I've posted the code till now what I've written Commented Jul 19, 2016 at 11:27
  • How many Opportunities can a Contact have?
    – Nick C
    Commented Jul 19, 2016 at 11:51
  • A contact can have multiple opportunities,ContactRole being the junction object between them. Commented Jul 19, 2016 at 11:56
  • 1
    So which opportunity is the task assigned to?
    – Nick C
    Commented Jul 19, 2016 at 12:47

1 Answer 1

1

//Handler

public with sharing class CreateNewTaskOnOpportunityHandler {

 public void onAfterInsert(List<Task> lstTask){
  Set<Id> contactId = new Set<Id>();
  for(Task t : lstTask){
   contactId.add(t.WhoId);
   System.debug('>>>>>>Ids: '+t.WhoId);
  }

  List<Contact> lstContact = [Select Id,LastName,(Select OpportunityId From OpportunityContactRoles) From Contact where Id IN: contactId];
  System.debug('>>>>>>>>>>>>>lstcontact: '+lstContact);
  List<OpportunityContactRole> oppContactRole = new List<OpportunityContactRole>();
  for(Contact con: lstContact){
   for(OpportunityContactRole opp: con.OpportunityContactRoles){
    oppContactRole.add(opp);
   }
   //opportunityId.add(lstContact.OpportunityContactRoles.OpportunityId);

  }

  List<Task> lstNewTasks = new List<Task>(); 
  for(Task t1 :lstTask )
  {
   for(OpportunityContactRole objOpportunityContactRole :oppContactRole)
   {
    Task newTask= new Task();
    newTask.WhatId = objOpportunityContactRole.OpportunityId;
    //System.debug();
    newTask.Subject = t1.Subject;
    newTask.Priority = t1.Priority;
    newTask.Status = t1.Status;
    newTask.Type = t1.Type;
    lstNewTasks.add(newTask);
   }
  }
  insert lstNewTasks;

 }

}


//Thanks!!
1
  • Any tym bro!! :) :) :) :) :) :) :) :) :)
    – AkshAy
    Commented Jul 19, 2016 at 14:32

You must log in to answer this question.

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