Skip to main content
added 378 characters in body
Source Link
sfdcfox
  • 501.8k
  • 21
  • 473
  • 828

Starting from where you get the masterTaskList, you should create a map so you can easily find the records you're looking for.

trigger OppTeamReAssignTaskTrigger on Opportunity_Team__c (after update) {

    Set<Id> oids = new Set<id>();
    Map<Opportunity_Team__c, Id> oppRoleMap = new Map<Opportunity_Team__c, Id>();
    // Create a map of Opportunity/Role to User Id
    for(Opportunity_Team__c record: Trigger.new) {
      oids.add(record.Opportunity__c);
      oppRoleMap.put(
        new Opportunity_Team__c(
          Opportunity__c=record.Opportunity__c, 
          Role__c=record.Role__c),
      record.User__c);
    }
    Task[] masterTaskList = [SELECT OwnerId, WhatId, Task_Group_Owner__c FROM Task WHERE WhatId IN :oids],
      recordUpdateList = new Task[0];
    // Use opportunity/role combination to find new owner
    for(Task record: masterTaskList) {
      Opportunity_Team__c key = new Opportunity_Team__c(
          Opportunity__c=record.WhatId, 
          Role__c=record.Task_Group_Owner__c);
      Id value = oppRoleMap.get(key);
      // Update only if the user differs from the task
      if(value != null && value != record.OwnerId) {
        record.OwnerId = value;
        recordUpdateList.add(record);
      }
    }
    update recordUpdateList;
}

Starting from where you get the masterTaskList, you should create a map so you can easily find the records you're looking for.

Map<Opportunity_Team__c, Id> oppRoleMap = new Map<Opportunity_Team__c, Id>();
// Create a map of Opportunity/Role to User Id
for(Opportunity_Team__c record: Trigger.new) {
  oppRoleMap.put(
    new Opportunity_Team__c(
      Opportunity__c=record.Opportunity__c, 
      Role__c=record.Role__c),
  record.User__c);
}
Task[] recordUpdateList = new Task[0];
// Use opportunity/role combination to find new owner
for(Task record: masterTaskList) {
  Opportunity_Team__c key = new Opportunity_Team__c(
      Opportunity__c=record.WhatId, 
      Role__c=record.Task_Group_Owner__c);
  Id value = oppRoleMap.get(key);
  // Update only if the user differs from the task
  if(value != null && value != record.OwnerId) {
    record.OwnerId = value;
    recordUpdateList.add(record);
  }
}
update recordUpdateList;

Starting from where you get the masterTaskList, you should create a map so you can easily find the records you're looking for.

trigger OppTeamReAssignTaskTrigger on Opportunity_Team__c (after update) {

    Set<Id> oids = new Set<id>();
    Map<Opportunity_Team__c, Id> oppRoleMap = new Map<Opportunity_Team__c, Id>();
    // Create a map of Opportunity/Role to User Id
    for(Opportunity_Team__c record: Trigger.new) {
      oids.add(record.Opportunity__c);
      oppRoleMap.put(
        new Opportunity_Team__c(
          Opportunity__c=record.Opportunity__c, 
          Role__c=record.Role__c),
      record.User__c);
    }
    Task[] masterTaskList = [SELECT OwnerId, WhatId, Task_Group_Owner__c FROM Task WHERE WhatId IN :oids],
      recordUpdateList = new Task[0];
    // Use opportunity/role combination to find new owner
    for(Task record: masterTaskList) {
      Opportunity_Team__c key = new Opportunity_Team__c(
          Opportunity__c=record.WhatId, 
          Role__c=record.Task_Group_Owner__c);
      Id value = oppRoleMap.get(key);
      // Update only if the user differs from the task
      if(value != null && value != record.OwnerId) {
        record.OwnerId = value;
        recordUpdateList.add(record);
      }
    }
    update recordUpdateList;
}
Source Link
sfdcfox
  • 501.8k
  • 21
  • 473
  • 828

Starting from where you get the masterTaskList, you should create a map so you can easily find the records you're looking for.

Map<Opportunity_Team__c, Id> oppRoleMap = new Map<Opportunity_Team__c, Id>();
// Create a map of Opportunity/Role to User Id
for(Opportunity_Team__c record: Trigger.new) {
  oppRoleMap.put(
    new Opportunity_Team__c(
      Opportunity__c=record.Opportunity__c, 
      Role__c=record.Role__c),
  record.User__c);
}
Task[] recordUpdateList = new Task[0];
// Use opportunity/role combination to find new owner
for(Task record: masterTaskList) {
  Opportunity_Team__c key = new Opportunity_Team__c(
      Opportunity__c=record.WhatId, 
      Role__c=record.Task_Group_Owner__c);
  Id value = oppRoleMap.get(key);
  // Update only if the user differs from the task
  if(value != null && value != record.OwnerId) {
    record.OwnerId = value;
    recordUpdateList.add(record);
  }
}
update recordUpdateList;