+ Start a Discussion
Jyosi jyosiJyosi jyosi 

TaskRealtion object while Inserting the task

Hello Everyone,
we got into typical requirement
when the user add the task on contact he selects 10 contact and assign the same task.
when the task is created and  assigned to 10 contact we need to update the contact records. e
i am able update the 10 contact when task is assigned.
but on create it takes only one contact i.e the primary contact gets updated
Apex Trigger

If(Trigger.isinsert || Trigger.IsAfter)
     {
        Task1.XXX(Trigger.New);
     }

// Handler class 

 public void XXX(List<Task> LstTask) 
   {
      Set<id> TaskIds= new Set<id>();
      Set<id> WhoIds= new Set<id>();
      List<TaskRelation> TaskRelation = new List<TaskRelation>();
      Map<Id,Account> AcctListUpdate= new Map<Id,Account>();
      Map<Id,Id> MapWhoId= new Map<Id,Id>();
       Map<Id,Id> MapWhatId= new Map<Id,Id>();
      Map<Id,Contact> LstContactupdate= new Map<Id,Contact>();
       for(Task TaskValues: LstTask)
       {
            TaskIds.add(TaskValues.id);
            WhoIds.add(TaskValues.whoId);
       }
      system.debug('TaskIds>>>>'+TaskIds+'>>>>>'+WhoIds);we get only whoids for the task 
      TaskRelation=[SELECT AccountId,IsWhat,RelationId,TaskId FROM TaskRelation Where TaskId =: TaskIds];
      system.debug('TaskQuery '+TaskRelation.size()+'!!! ' +TaskRelation);
      for(TaskRelation TaskRelations:TaskRelation)
      {
         MapWhoId.put(TaskRelations.id,TaskRelations.RelationId);
         MapWhatId.put(TaskRelations.id, TaskRelations.AccountId);
        system.debug('MapWhatId>.>>'+MapWhatId);
        system.debug('MapWhoId>.>>'+MapWhoId);
      }
       for(TaskRelation TaskRelations:TaskRelation)
      {
         Contact ContactUpdate= new Contact();
         Account AccountUpdate= new Account();
         ContactUpdate.Id=MapWhoId.get(TaskRelations.id);
         ContactUpdate.Date_of_Last_Call__c=system.today();
         AccountUpdate.Id=MapWhatId.get(TaskRelations.id);
         AccountUpdate.Date_of_Last_Call__c=system.today();
         LstContactupdate.put(ContactUpdate.Id,ContactUpdate);
         AcctListUpdate.put(AccountUpdate.Id, AccountUpdate);
          
      }
        
       system.debug('LstContactupdate>>>>'+LstContactupdate+'>>>>>>'+AcctListUpdate);
      if(LstContactupdate.size()>0)
      {
          try{
              update LstContactupdate.values();
              update AcctListUpdate.values();
              system.debug('LstContactupdatetoUpddate>>>>'+LstContactupdate);
          }catch(DmlException e)
          {
              system.debug('DmlException>>'+e);
          }
      }
       
   }



 
Tarun Khatri (TK)Tarun Khatri (TK)
Hello Jyosi,

For Shared Activities, WhoId shows Primary Contact only. Also TaskRelation object always return only one record with primary contact. It is a known issue with saleforce. Kinldy refer below links for more details:

https://success.salesforce.com/issues_view?id=a1p30000000Su0FAAS

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_task.htm


-Thanks,
TK