function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kromerokromero 

Field Update Trigger for Two Objects

When we send out emails to customers they have the option to opt-out of recieving future emails. If they click on this link then a custom field "unsubscribe date" within our custom object "message statistics" is automatically updated.

 

Does anyone have suggestions for writing a trigger that once the "unsubscribe date" field within the "message statistics" custom object is filled out then the "opt-out" checkbox within the "contact" object will become checked?

 

Any suggestions are appreciated as I'm just beginning to learn about Apex code.

k_bentsenk_bentsen

Do you have a lookup relationship between the "Message statistics" custom object and Contact object? If so, you can run a query to obtain the Contact record you are looking for, then update the opt-out field to true, or whatever mechanism you use. Are you familiar with SOQL queries at all?

vbsvbs
@kromero - A better idea on the relationship between these 2 objects will tell us if there is a better way than code in implementing this use case.
kromerokromero
No, there is not a lookup relationship between the "Message Statistics" and "Contact" objects. Any other suggestions?
kromerokromero
The "Contact" object is just the standard SFDC object, but the "Message Statistics" object actually came from a managed packaged that was downloaded for the AppExchange. I don't believe they are currently related. The "Message Statistics" object is related to the "Campaign" object. Does that give enough information?
k_bentsenk_bentsen

Can you provide some more detail on what the "Message statistics" custom object is, what data it captures, how is it created, etc.?

kromerokromero
The "message statistics" custom object contains the statistics for an individual message recipient, such as if they click on any links within the email, when they open it, if the email bounces, the unsubscribe date, etc. It was one of the custom objects that was downloaded as part of the iContact app.
k_bentsenk_bentsen

In that case, you should be able to write your trigger to query the contact record that is the same person related to the "message statistic" record. It would look something like:

 

Contact c = [Select Id, Email, HasOptedOutOfEmail from Contact where Email = :msgStat.Email_address__c];
c.HasOptedOutOfEmail = TRUE;
update c;

 

where msgStat would be the variable name of a given "message statistic" record that the trigger is executing upon. I am also assuming the API label of the email address field on "message statistic" is Email_address__c