You need to sign in to do that
Don't have an account?
NecroGiggles
Need to update a Null look up field between 2 objects based on conditions.
I need to link "Campaign Members" to a custom object "KPITarget" based on values of both objects using a lookup field.
The conditions are based on vales from both objects 3 fields that need to match on both objects and a date fields that just needs the month and year to mach. The last thing is the "HasResponded" field must be true on Campaign member.
I think this needs to be a class called by a Trigger but I am not sure since there will be no link between the objects at the start.
Create a List of all Campaign Member Id's getting the values of the 3 fields, Date Range and, HasResponded That the trigger fires off.
Create a List of all KPITargets Id’s that match the values of the Campaign Member, There should only be one.
The update the look up field on Campaign member with the ID of the KPITarget that matches.
How does that sound?
The conditions are based on vales from both objects 3 fields that need to match on both objects and a date fields that just needs the month and year to mach. The last thing is the "HasResponded" field must be true on Campaign member.
I think this needs to be a class called by a Trigger but I am not sure since there will be no link between the objects at the start.
Create a List of all Campaign Member Id's getting the values of the 3 fields, Date Range and, HasResponded That the trigger fires off.
Create a List of all KPITargets Id’s that match the values of the Campaign Member, There should only be one.
The update the look up field on Campaign member with the ID of the KPITarget that matches.
How does that sound?
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm
All Answers
1. Query for all KPITargets with the fields you need to compare against the Campaign Members and put it in a list.
2. Loop through Trigger.New (which is the list of CampaignMember records that were triggered) and compare each CampaignMember record against each KPITarget record in the list. If there's a match, assign the KPITarget record to the lookup field of the CampaignMember record.
I only consider the 3 fields, but you can incorporate the date and HasReponded fields to it. You could also have do an SOQL query within the first for loop to find the KPITarget record without having to use a for loop to find it, but you will run into SOQL query limits if you do when processing the trigger in bulk.
Note that since this is a before trigger, you just need to assign member.Marketing_KPI_Target__c = kpiTarget.Id and Salesforce will save this once the trigger is done. You don't need to explicitly call an insert/update.
kpiTarget.KPI_Target_Date__c == member.Response_Tracking_Date__c &&
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm