1

Current Setup: Workflow rules are creating task assigned to a specific opportunity role. A trigger (written by a developer who has left the organization) associates value in business_associate__C picklist to the opportunity team with the specific role. It also limit 1 user to this role on the opportunity team.

New Business need: Once the task is created the subject needs to updated with the name in the Business_Associate__C field. Additional the value of the Business_Associate__C picklist on the opportunity needs to be pushed to a matching picklist on the task.

Since a workflow can not trigger another workflow, a task trigger needs to be created to update the task.

This is what I have put together so far... Not sure how associate values from related opportunity to the tasksToUpd map for update.

Trigger TaskTrigger On Task (after insert)

//Establish variable for whatIDs 
Set <String> whatIDs = new Set<String>();

//Loop New Tasks
For (Task t: Trigger.new) {
whatIDs.put(t.WhatID, t);
}

//List opportunities gathered from Task loop
List <Opportunity> oppty = [Select ID, Business__Associate__C from Opportunity WHERE ID =: whatIDs];

//Create Map for tasks to update 
Map <String, Task> tasksToUpd = new Map <String, Task> ();
For (Task t : Trigger.new) {
tasksToUpd.put(t.whatID, t);
}

//Loop through opportunities, gather 
For (Opportunity o: oppty) {
if (tasksToUpd.containsKey(o.ID)){
***[Get Business_Associate__C Value and Store in TasksToUpd Maps - TasksToUpd.Business_Associate__C]***
***[Get Business_Associate__C Value and Replace last two characters of subject 'BA' and store in TasksToUpd Maps - TasksToUpd.Subject]***
 }
Update TasksToUpd;

Any help will be appreciated. Thanks in advance.

1

2 Answers 2

2

Your code has the right patterns in it but is missing some of the details. I've stripped it down and listed below the code I would write.

Triggers should do as little as possible, so I've added a test to make sure that the trigger only queries when there is one or more Opportunity Id. (Tasks can be added to many objects in a typical org.) Relating the Opportunity to the Task is a matter of putting the right id and object together in a map (oppsToTasks in this case). And by moving to a "before" trigger the update of Task can be avoided because the changes made by the trigger will be included when the tasks are inserted.

trigger TaskTrigger on Task (before insert) {
    Map<Id, Task> oppsToTasks = new Map<Id, Task>();
    For (Task t: Trigger.new) {
        if (t.WhatId != null && t.WhatId.getSObjectType() == Opportunity.SObjectType) {
            oppsToTasks.put(t.WhatId, t);
        }
    }
    if (oppsToTasks.size() > 0) {
        for (Opportunity opp : [
                select Id, Business__Associate__C
                from Opportunity
                where Id in :oppsToTasks.keySet()
                ]) {
            Task task = oppsToTasks.get(opp.Id);
            task.Subject = ...
            task.Business__Associate__C = opp.Business__Associate__C;
        }
    }
}
0

The best approach is to create a map of opportunity then when you loop on your new tasks you can get the detail of your records from your map. Also you have to call your trigger before insert instead of after insert, you don't need to update your task again.

Trigger TaskTrigger On Task (before insert)
{
    //Establish variable for whatIDs 
    Map <ID,Opportunity> whatIDs = new Map<ID,Opportunity>();

    //Loop New Tasks
    For (Task t: Trigger.new) {
        whatIDs.put(t.WhatID, null);
    }

    //get opportunities
    whatIDs = new Map<ID,Opportunity>([Select ID, Business__Associate__c from Opportunity WHERE ID =: whatIDs]);

    //update task subject from oppty
    Map <String, Task> tasksToUpd = new Map <String, Task> ();
    For (Task t : Trigger.new) {
            if (whatIDs.get(t.WhatID) != null){
                    t.Subject = whatIDs.get(t.WhatID).Business__Associate__c;
            } 
     }
 }

You must log in to answer this question.

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