You need to sign in to do that
Don't have an account?
Mleary
Help with Trigger - update field on Contact with Change on Account
Hey Everyone - I am new ot the developer community and just getting my feet wet with learning Apex Code. Could someone steer me in the right direction with the below requested automation?
Here is the scenario ---
When checkbox field Custom_Feild_A__c is marked true on Account
Two checkbox fields are updated on the Contact:
Custom_Feild_B__c is marked true
Custom_Feild_C__c is marked true
I have tested this several times but after checking the box on the account the two checkbox fields on the Contact remained unchecked?
Hi Mleary
Is this the standard relationship between account and contact?
if so, which contact do you want to update under the account?
lets assume that you want all contacts related to the account, your code should be:
Hope this helps - if it does, kindly mark it as resolved.
All Answers
Hi Mleary
Is this the standard relationship between account and contact?
if so, which contact do you want to update under the account?
lets assume that you want all contacts related to the account, your code should be:
Hope this helps - if it does, kindly mark it as resolved.
Yes you were correct in assuming that I needed all associated Contacts and that they have a standard relationship. I will work on this now and will let you know the outcome! Thank you so much!
so just change the fields in the trigger i wrote to your own fields - and your set.
you don't need code for this i don't think.
just use a formula field and workflow.
create your checkbox field a on account, and checkbox fields b and c on contact
then create hidden formula field (type text) on contact as well with a formula like this (change field names if you like)
IF(
Account.Custom_Field_A__c = true,
"1","0"
)
Then createe workflow rule on contact with 2 field updates on checkboxes b and c on create or edited and did not previously meet criteria, setting them to true.
always use config over code if possible (unless I misunderstand the reqs here)
yes they do. I just did it in a dev org
I put in the trigger as you wrote it, inserted my custom fields, and then recieved this error message:
Error: Compile Error: unexpected token: Suppression at line 1 column 12
Try it. They don't fire for me...
It's hard to tell what is the problem without seeing it.
Sure thing!
trigger ROI Suppression on ACCOUNT (after update) {
set<Id> accIds = new set<Id>();
for(Account acc: trigger.new)
{
if(acc.ROI_Suppression__c && !trigger.oldMap.get(acc.Id).ROI_Suppression__c)
{
accIds.add(acc.Id);
}
}
if(!accIds.isEmpty())
{
List<Contact> update_contacts = new List<Contact>();
for(Contact con : [select Id,ROI_Automation_Suppression__c,Custom_Feild_C__c from Contact where AccountId IN: accIds])
{
if(!con.ROI_Automation_Suppression__c && !con.ROI_Suppression__c)
{
con.ROI_Automation_Suppression__c = con.ROI_Suppression__c = true;
update_contacts.add(con);
}
}
if(!update_contacts.isEmpty())
{
update update_contacts;
}
}
}
Get rid of the space between 'ROI' and 'Suppression' in your trigger name.
i just double checked my work and you can definitely do this with 1 workflow rule w. 2 workflow field updates and a formula field.
Step 1. create your fields:
on account:
Custom Field A (checkbox)
on contact:
Custom Field B (checkbox)
Custom Field C (checkbox)
Hidden_Field__c (Formula of type text with this formula:
IF(
Account.Custom_Field_A__c = true,
"1","0"
)
Step 2. Create your workflow and 2 field updates
Create Workflow Rule:
Worfkflow Rule should be on Contact object and should fire every time a record is created or edited and did not previously meet the rule criteria
Workflow Rule crtieria:
Contact: Hidden Field EQUALS 1
Create Workflow Rule Field updates (2):
field update#1: Set Contact.Custom Field B (checkbox) to "True"
field update#2: Set Contact.Custom Field C (checkbox) to "True"
Activate workflow rule.
How does that not work?
Same reason why you can't perform history on them.
But, maybe it changed since I last checked.
Are you sure you don't have something else that updates the contacts wen the account is updated and the workflows just "hitch a ride" on that update?
yeah this definitely works with the formula and workflow. no code should be needed at all; you are right that it didn't used to work, though. i know you definitely could not use a formula field in wfr crtieria before, but you definitely can now. i'm pretty sure sf changed this so you can do this now.
i've got no other logic that could possibly be doing this. it works
That is awesome News SFAdmin5.
i can probably get rid of a few triggers now.
thanks for the tip.
got me interested - so I tried it.
it doesn't work for me.
here is what I did:
what do you think is wrong SFAdmin5?
good call. the workflow and formula solution only works on account record insert having Custom Field A being set to true.
if the requirement is that accounts will always get that checkbox on record create and not in a later update this still works.
but this get me thinking...wouldn't the simplest solution here be to just have the custom field a checkbox on the account, and then have custom fields b and c on the contacts be 2 formula fields that output a text yes or no, like this
IF(
Account.rossg10242012__Custom_Field_A__c = true,
"Yes","No"
)
Unless someone has some reason that checkbox fields b and c must be checkboxes (not sure what that could be but there might be a reason I can't think of), then that would work I believe.
but if all 3 fields need to be checkboxes, and if the account can get updated later after creation with checkbox a = true, then you are definitely correct that the trigger is the only way to go here.
just trying to find a way around this without code since there is so much in config between contacts and accounts that you can do already.
Hi Ross!
There could be situations where checkboxes B and C on the Contact could be checked, but the Account Checkbox A is unchecked. But never will the Account Checkbox A be checked without all related Contact checkboxes B and C also being checked. So that is why the checkboxes on the Contact need to remain checkbox fields as opposed to formula fields.
The trigger provided by Noam.dgani does work for me! So I will close the post.
But I wasn't quite following why the formula field + workflow rule won't work. Just for my understanding, are you saying that it will only work upon record creation?
Oh brother, I don't know how to write a test script. Any direction or help you could give me here?
this trigger and test class ought to work