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
mng0827mng0827 

Trigger to Update Detail Object

I have written a trigger to update the "Email Opt Out" and "Do Not Call" Contact fields when the "Lead Status" in Accounts is equal to "Dead". Here is the code:

 

trigger updateContactfields on Account (before update) {
for (Account accts: Trigger.new)
{
if (accts.Lead_Status__c == 'Dead'){
List<Contact> con = new List<Contact>();
con = [SELECT Id FROM Contact WHERE AccountId = :accts.ID];
for(Contact c: con)
{c.DoNotCall = True;
c.HasOptedOutOfEmail = True;}
update con;
}
}
}

 

In addition, I would also like to update a custom object, which is a detail object of the Account. I've tried adding this on my trigger but I get an error saying that "No such column 'AccountID' on entity 'Master_c'. Can you help me on this? Here's the code I'm trying to add:

 

 List<Master_c> con = new List<Master_c>();
        mt = [SELECT Id FROM Master_c WHERE AccountId = :accts.ID];
        for(Master_c m: mt)
        m.Shut_Off_c = True;
        update mt;
digamber.prasaddigamber.prasad

Hi,

 

I assume name of account lookup should be Account__c. According to this could you please try below:-

 

List<Master_c> con = new List<Master_c>();
        mt = [SELECT Id FROM Master_c WHERE Account__c = :accts.ID];
        for(Master_c m: mt)
        m.Shut_Off_c = True;
        update mt;

 

If this is not the right relationship name, you can always go to this custom object and check for correct API Name of object.

 

One more thing, you should make your trigger bulk safe.

 

Let me know if you have any question.

mng0827mng0827

Thanks Digamber! I did what you just said and it now works perfectly!

digamber.prasaddigamber.prasad

Happy that it worked for you! If you think I was able to help you out, could you please give me KUDO for this.