You need to sign in to do that
Don't have an account?
S_Batman
Create a Checkbox field based on two fields on Related list
I want to create a check box field (let's call it TestABC), which will be on true based on the below criteria;
I have two checkbox fields on the Contact object - Primary Contact & Do Not email (these fields also show up on the Contact Related List on the Account object)
TestABC = True if (Primary Contact is true and Do Not Email is false)
I want this TestABC field on the Account level, and look through all the contacts associated with the account to validate the formula.
Is this possible with a trigger? Could someone help me write such Trigger?
I have two checkbox fields on the Contact object - Primary Contact & Do Not email (these fields also show up on the Contact Related List on the Account object)
TestABC = True if (Primary Contact is true and Do Not Email is false)
I want this TestABC field on the Account level, and look through all the contacts associated with the account to validate the formula.
Is this possible with a trigger? Could someone help me write such Trigger?
Please find the latest code based on your new logic:
Please do let me know if it helps you.
Regards,
Mahesh
All Answers
Yes we can do it.
If you can clarify onething here
Account --> TestABC field should be true only if
Cont 1 --> PC is True and DNE is False
Cont 2 --> PC is True and DNE is False
Cont 2 --> PC is True and DNE is False
Account --> TestABC field should be False only if
Cont 1 --> PC is True and DNE is False
Cont 2 --> PC is False and DNE is False
Cont 2 --> PC is True and DNE is True
By Considering the scenario one, below is the code:
Please do let me know if it helps you.
Regards,
Mahesh
Typo mistake.
Regards,
Mahesh
Compile Error: expecting a right parentheses, found ',' at line 26 column 45
I checked to see all the parentheses line up
Regards,
Mahesh
Variable does not exist: Do_not_email__c at line 26 column 59
I copied the API name into the code you wrote above. My API name is the same as the one you used in the code
Pleae take the final code:
Regards,
Mahesh
I created this trigger in the Contact Object;
I have 3 contacts where Primary Contact = true for all 3, and Do not email = is false for 3; in this case TestABC = True, but it's not checked off.
PS: where can I learn how to write code like this? I know there's salesforce Trailhead, any other resources you would recommend?
You have to touch the record then only the trigger will perform calculation.
Please follow the below links to understand Apex:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_intro_what_is_apex.htm
https://developer.salesforce.com/page/Apex
https://developer.salesforce.com/page/An_Introduction_to_Apex
https://resources.docs.salesforce.com/sfdc/pdf/salesforce_apex_language_reference.pdf
https://developer.salesforce.com/page/Apex_Code:_The_World's_First_On-Demand_Programming_Language
Please do let me know if it helps you.
Regards,
Mahesh
The checkbox field did not update.
Regards,
Mahesh
This is created in Sandbox, I have not deployed it yet in production yet.
Just click on update and save and see the results.
Also make sure that the fields you used correct.
Regards,
Mahesh
One more change
Replace below line in your code:
if(accIdSet.isEmpty()) {
with
if(!accIdSet.isEmpty()) {
Thats it everything is good to go.
I tested it and it is working the functionality also.
Please do let me know if it works.
Regards,
Mahesh
and now whenever I update contact and try to save I get this:
Error:Apex trigger ContactTrigger caused an unexpected exception, contact your administrator: ContactTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00156000005ZGoxAAG; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, updatemytest: maximum trigger depth exceeded Contact trigger event AfterUpdate for [00356000006luQD] Account trigger event AfterUpdate for [00156000005ZGox] Contact trigger event AfterUpdate for [00356000006luLD, 00356000006luLI, 00356000006luQD] Account trigger event AfterUpdate for [00156000005ZGox] Contact trigger event AfterUpdate for [00356000006luLD, 00356000006luLI, 00356000006luQD] Account trigger event AfterUpdate for [00156000005ZGox] Contact trigger event AfterUpdate for [00356000006luLD, 00356000006luLI, 00356000006luQD] Account trigger event AfterUpdate for [00156000005ZGox] Contact trigger event AfterUpdate for [00356000006luLD, 00356000006luLI, 00356000006luQD] Account trigger event AfterUpdate for [00156000005ZGox] Contact trigger event AfterUpdate for [00356000006luLD, 00356000006luLI, 00356000006luQD] Account trigger event AfterUpdate for [00156000005ZGox] Contact trigger event AfterUpdate for [00356000006luLD, 00356000006luLI, 00356000006luQD] Account trigger event AfterUpdate for [00156000005ZGox] Contact trigger event AfterUpdate for [00356000006luLD, 00356000006luLI, 00356000006luQD] Account trigger event AfterUpdate for [00156000005ZGox]: []: ()
You have one trigger on the Account which is updating the contact and this contact trigger is updating the contact.
Please paste you Account trigger here.
trigger updatemytest on Account (after update)
{
list<contact>cm1=[select id,accountid from contact where accountid IN : trigger.newMap.KeySet()];
List<contact> cm2= new List<contact>();
for(contact cm4 : cm1)
{ Account acct = Trigger.NewMap.get(cm4.accountid);
if(acct.Account_Type__c=='Top X' )
{
cm4.TOP_X__c= true;
cm4.Customer__c=false;
cm4.KHA__c=false;
cm2.add(cm4);
}
else if((acct.Account_Type__c=='Customer - GB') || (acct.Account_Type__c=='Customer' ))
{
cm4.Customer__c= true;
cm4.TOP_X__c= false;
cm2.add(cm4);
}
else if (acct.Partner__c=='Kaufman Hall')
{
cm4.KHA__c= true;
cm2.add(cm4);
}
else {
cm4.KHA__c=false;
cm2.add(cm4);
}
}
List<Database.SaveResult> srList = Database.update(cm2);
for (Database.SaveResult sr : srList)
{
if (sr.isSuccess()) {
// Operation was successful, so get the ID of the record that was processed in Debug Logs
System.debug('Successfully inserted account. Account ID: ' + sr.getId());
}
else {
// Operation failed, so get all errors
for(Database.Error err : sr.getErrors())
{
Trigger.newMap.get(sr.getId()).addError(err.getMessage());
}
}
}
}
While testing the functionality,
Make sure that, you are modifying any of the below fields
Account
Primary Contact
Do Not Email
Any of these field changes then only it will perform the calculation.
Regards,
Mahesh
Cont 1 - PC = True DNE = False
Cont 2 - PC = False DNE = False
Cont 3 - PC = False DNE = False
TestABC = True
so pretty much if an account has at least Contact who is Primary Contact and DNE = False I want the TestABC = true
if the account only has contacts that are primary contact but all the primary contact are DNE = true then TestABC = False.
Also, this trigger will work on any number of contacts within the account, correct?
Please find the latest code based on your new logic:
Please do let me know if it helps you.
Regards,
Mahesh
Please follow my last reply which I tested it in my DE environment and everything is working properly.
Waiting for Sutha to confirm on it.
Regards,
Mahesh
In the meantime, you think you can help me answer my question?
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000DBvSIAW
Thanks again Mahesh :)