You need to sign in to do that
Don't have an account?
Thomas Reinman 16
Apex Trigger Not Updating Lookup Field
Hello,
I need a trigger that will update a lookup field on a Task of a particular record type based upon the users' picklist value selection on that task record. The lookup field should be populated with the ID of another custom object. Both the task and custom object have a lookup to Opportunites, and the picklist on the task record matches the values of a picklist on the custom object. The matching criteria should be a combination of: (1) Task.WhatId and CustomObject.OpportunityId, (2) Task.PicklistValue and CustomObject.PicklistValue. So far, I have the following solution written:
It is saved and active in my sandbox, but it is not updating the lookup field as expected.
Does anyone have a correct solution?
Thanks,
Tom
I need a trigger that will update a lookup field on a Task of a particular record type based upon the users' picklist value selection on that task record. The lookup field should be populated with the ID of another custom object. Both the task and custom object have a lookup to Opportunites, and the picklist on the task record matches the values of a picklist on the custom object. The matching criteria should be a combination of: (1) Task.WhatId and CustomObject.OpportunityId, (2) Task.PicklistValue and CustomObject.PicklistValue. So far, I have the following solution written:
It is saved and active in my sandbox, but it is not updating the lookup field as expected.
Does anyone have a correct solution?
Thanks,
Tom
trigger updateMCPLookup on Task (before insert, before update) {
Set<Id> mcpObjIDSet = new Set<Id>();
Set<String> mcpObjSubSet = new Set<String>();
// Iterate over our trigger context variable
for (Task t : Trigger.new){
// Make sure the record type is Mutual Closing Plan
if(t.RecordType.Name == 'Mutual_Closing_Plan'){
// Declare collections to hold values (IDs and Subjects) we will need in the WHERE clause of the query
mcpObjIDSet.add(t.WhatId);
mcpObjSubSet.add(t.Subject);
}
}
// Declare a map, this map will be used to hold the data we care about
Map<String, Id> mcpIdToTaskNameMap = new Map<String, Id>();
// Perform a query for the information we want to retrieve
for(Mutual_Closing_Plan_Objective__c mcpObj : [SELECT Id, Subject__c FROM Mutual_Closing_Plan_Objective__c WHERE Opportunity__c IN :mcpObjIDSet AND Subject__c IN :mcpObjSubSet LIMIT 1]){
// Inside this loop, we populate the map with the data we're interested in.
mcpIdToTaskNameMap.put(mcpObj.Subject__c, mcpObj.Id);
// Finally, iterate one more time over the records we initially had. Since this is a 'before' trigger, no further DML is required.
for(Task task : trigger.new){
task.MCP_Objective__c = mcpIdToTaskNameMap.get(mcpObj.Id);
}
}
}
Here is the updated code I have saved:
It will help you identify the right name for the required record type. Once you review the options listed, you can then replace Mutual_Closing_Plan with the right value in line 9 of the trigger.
I was thinking the same thing but this is the name of the Record Type. I commented that portion of the code out and there are no more errors, but the Lookup field on the Task record still isn't being updated after I update the custom picklist field. Here is the latest sample I'm working with:
Thanks again for all of your assistance.