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);

        }


    }

}
  • Can you post the code that you've already tried it with? – Nick Cook Jul 19 '16 at 10:55
  • I've posted the code till now what I've written – Shobhit Saxena Jul 19 '16 at 11:27
  • How many Opportunities can a Contact have? – Nick Cook Jul 19 '16 at 11:51
  • A contact can have multiple opportunities,ContactRole being the junction object between them. – Shobhit Saxena Jul 19 '16 at 11:56
  • 1
    So which opportunity is the task assigned to? – Nick Cook Jul 19 '16 at 12:47
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!!
  • Any tym bro!! :) :) :) :) :) :) :) :) :) – AkshAy Jul 19 '16 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.