Skip to main content
added 97 characters in body
Source Link
user2655970
  • 361
  • 1
  • 3
  • 9

I have a child object called "Opportunity_Team__c" whose parent is Opportunity.

This Opportunity_Team__c object has 3 fields

Opportunity__c (lookup to Opportunity)
Role__c (which is a picklist) 
and 
User__c(which is a lookup to User)

Also in Task object we have a custom field called "Task Group Owner" which is a picklist and has the same picklist entries as in Opportunity_Team__c.Role__c

This image can be helpful

enter image description here

Whenever there is an update happening in Opportunity_Team__c record I need to query all the records in Task object with the below join and update the ownerId of the task with the latest value in Opportunity_Team__c.User__c

Task.WhatId = Opportunity_Team__c.Opportunity__c
&&
Task.Task_Group_Owner__c = Opportunity_Team.Role__c

I have developed the following trigger to achieve this.

Is there any way to optimize or improve this code ?

trigger OppTeamReAssignTaskTrigger on Opportunity_Team__c (after update) {

  List<Task> mastertasklist = new List<Task>();
  Set<Id> oids = new Set<Id>();
   
    for(Opportunity_Team__c ot : trigger.new)
    {
        oids.add(ot.Opportunity__c);
    }
    
    mastertasklist = [SELECT OwnerId, WhatId, Task_Group_Owner__c FROM Task WHERE WhatId IN :oids];
    
    List<Task> reassignTaskList = new List<Task>();
    
    for(Opportunity_Team__c ot : trigger.new)
    {
        for(Task t : mastertasklist)
        {
            if(ot.Opportunity__c == t.WhatId && ot.Role__c == t.task_group_owner__c)
            {
                t.OwnerId = ot.User__c;
            }
        }
    }
    
    update mastertasklist;    

}

EDIT:

Is there any way to modify the code so that I do not have to use two for-loops ?

I have a child object called "Opportunity_Team__c" whose parent is Opportunity.

This Opportunity_Team__c object has 3 fields

Opportunity__c (lookup to Opportunity)
Role__c (which is a picklist) 
and 
User__c(which is a lookup to User)

Also in Task object we have a custom field called "Task Group Owner" which is a picklist and has the same picklist entries as in Opportunity_Team__c.Role__c

This image can be helpful

enter image description here

Whenever there is an update happening in Opportunity_Team__c record I need to query all the records in Task object with the below join and update the ownerId of the task with the latest value in Opportunity_Team__c.User__c

Task.WhatId = Opportunity_Team__c.Opportunity__c
&&
Task.Task_Group_Owner__c = Opportunity_Team.Role__c

I have developed the following trigger to achieve this.

Is there any way to optimize or improve this code ?

trigger OppTeamReAssignTaskTrigger on Opportunity_Team__c (after update) {

  List<Task> mastertasklist = new List<Task>();
  Set<Id> oids = new Set<Id>();
   
    for(Opportunity_Team__c ot : trigger.new)
    {
        oids.add(ot.Opportunity__c);
    }
    
    mastertasklist = [SELECT OwnerId, WhatId, Task_Group_Owner__c FROM Task WHERE WhatId IN :oids];
    
    List<Task> reassignTaskList = new List<Task>();
    
    for(Opportunity_Team__c ot : trigger.new)
    {
        for(Task t : mastertasklist)
        {
            if(ot.Opportunity__c == t.WhatId && ot.Role__c == t.task_group_owner__c)
            {
                t.OwnerId = ot.User__c;
            }
        }
    }
    
    update mastertasklist;    

}

I have a child object called "Opportunity_Team__c" whose parent is Opportunity.

This Opportunity_Team__c object has 3 fields

Opportunity__c (lookup to Opportunity)
Role__c (which is a picklist) 
and 
User__c(which is a lookup to User)

Also in Task object we have a custom field called "Task Group Owner" which is a picklist and has the same picklist entries as in Opportunity_Team__c.Role__c

This image can be helpful

enter image description here

Whenever there is an update happening in Opportunity_Team__c record I need to query all the records in Task object with the below join and update the ownerId of the task with the latest value in Opportunity_Team__c.User__c

Task.WhatId = Opportunity_Team__c.Opportunity__c
&&
Task.Task_Group_Owner__c = Opportunity_Team.Role__c

I have developed the following trigger to achieve this.

Is there any way to optimize or improve this code ?

trigger OppTeamReAssignTaskTrigger on Opportunity_Team__c (after update) {

  List<Task> mastertasklist = new List<Task>();
  Set<Id> oids = new Set<Id>();
   
    for(Opportunity_Team__c ot : trigger.new)
    {
        oids.add(ot.Opportunity__c);
    }
    
    mastertasklist = [SELECT OwnerId, WhatId, Task_Group_Owner__c FROM Task WHERE WhatId IN :oids];
    
    List<Task> reassignTaskList = new List<Task>();
    
    for(Opportunity_Team__c ot : trigger.new)
    {
        for(Task t : mastertasklist)
        {
            if(ot.Opportunity__c == t.WhatId && ot.Role__c == t.task_group_owner__c)
            {
                t.OwnerId = ot.User__c;
            }
        }
    }
    
    update mastertasklist;    

}

EDIT:

Is there any way to modify the code so that I do not have to use two for-loops ?

Source Link
user2655970
  • 361
  • 1
  • 3
  • 9

Apex Trigger Optimization

I have a child object called "Opportunity_Team__c" whose parent is Opportunity.

This Opportunity_Team__c object has 3 fields

Opportunity__c (lookup to Opportunity)
Role__c (which is a picklist) 
and 
User__c(which is a lookup to User)

Also in Task object we have a custom field called "Task Group Owner" which is a picklist and has the same picklist entries as in Opportunity_Team__c.Role__c

This image can be helpful

enter image description here

Whenever there is an update happening in Opportunity_Team__c record I need to query all the records in Task object with the below join and update the ownerId of the task with the latest value in Opportunity_Team__c.User__c

Task.WhatId = Opportunity_Team__c.Opportunity__c
&&
Task.Task_Group_Owner__c = Opportunity_Team.Role__c

I have developed the following trigger to achieve this.

Is there any way to optimize or improve this code ?

trigger OppTeamReAssignTaskTrigger on Opportunity_Team__c (after update) {

  List<Task> mastertasklist = new List<Task>();
  Set<Id> oids = new Set<Id>();
   
    for(Opportunity_Team__c ot : trigger.new)
    {
        oids.add(ot.Opportunity__c);
    }
    
    mastertasklist = [SELECT OwnerId, WhatId, Task_Group_Owner__c FROM Task WHERE WhatId IN :oids];
    
    List<Task> reassignTaskList = new List<Task>();
    
    for(Opportunity_Team__c ot : trigger.new)
    {
        for(Task t : mastertasklist)
        {
            if(ot.Opportunity__c == t.WhatId && ot.Role__c == t.task_group_owner__c)
            {
                t.OwnerId = ot.User__c;
            }
        }
    }
    
    update mastertasklist;    

}