You need to sign in to do that
Don't have an account?
Shawn Nguyen
Delete text on Master object when Child object is deleted
Hi everyone,
First attempt at writing a trigger and need some guidance.
I have a custom Child object calls Account_Contact_Roles_c (not using the standard Salesforce b/c we want to see the related list on both the account and contact record) that tracks the account main contacts. We have a custom field on the contact record calls Contact Roles_c, which is a formula text field that pulls in the value from the Account_Contact_Roles_c's Role_c field. Using a WF rule with the following formula:
IF ( INCLUDES ( Roles__c , "Primary Contact" ), "Primary Contact: ",null ) &
IF ( INCLUDES ( Roles__c , "Key Contact" ), "Key Contact: ",null ) &
IF ( INCLUDES ( Roles__c , "Billing Contact" ), "Billing Contact: ",null ) &
IF ( INCLUDES ( Roles__c , "Sponsor Contact" ), "Sponsor Contact: ",null )
Whenever the contact role is updated in the custom object, the field on the contact record will update to reflect the new value. Thus if Primary & Sponsor are seleted in Role_C, Contact_Roles_c will have the value Primar Contact: Sponsor Contact.
However, we want the Contact_Roles_c field to clear out when the Account Contact Role record is deleted. I know WF can't accomplish this and only trigger can. Hope someone can point me in the right direction.
Thanks!
First attempt at writing a trigger and need some guidance.
I have a custom Child object calls Account_Contact_Roles_c (not using the standard Salesforce b/c we want to see the related list on both the account and contact record) that tracks the account main contacts. We have a custom field on the contact record calls Contact Roles_c, which is a formula text field that pulls in the value from the Account_Contact_Roles_c's Role_c field. Using a WF rule with the following formula:
IF ( INCLUDES ( Roles__c , "Primary Contact" ), "Primary Contact: ",null ) &
IF ( INCLUDES ( Roles__c , "Key Contact" ), "Key Contact: ",null ) &
IF ( INCLUDES ( Roles__c , "Billing Contact" ), "Billing Contact: ",null ) &
IF ( INCLUDES ( Roles__c , "Sponsor Contact" ), "Sponsor Contact: ",null )
Whenever the contact role is updated in the custom object, the field on the contact record will update to reflect the new value. Thus if Primary & Sponsor are seleted in Role_C, Contact_Roles_c will have the value Primar Contact: Sponsor Contact.
However, we want the Contact_Roles_c field to clear out when the Account Contact Role record is deleted. I know WF can't accomplish this and only trigger can. Hope someone can point me in the right direction.
Thanks!
I am assuming that you have a lookup to Contact, on your Account_Contact_Roles_c object.
Am also assuming that Contact_Roles_c is a text field
Here is a code example. Haven't compiled, please check if there are any errors, then correct them. but you can use this logic.
Roles__c is a text field on the Contact object.
The code given looks like it's going the opposite direction. We want to clear the text on Roles_c in the Contact Object when the Account_Contact_Roles_c object is deleted. The WF rule takes care of new or updated role.
Line 12 should read as : c.Role__c = '';
The trigger runs on Account_Contact_Roles_c Delete , and clears the contact role__c field.
Have you tried it ?
Below is the final code:
trigger emptyField on Account_Contact_Role__c(after delete){
List<Contact> updateList = new List<Contact>();
for(Account_Contact_Role__c ac: trigger.old){
if(ac.Contact__c !=null ){
//create a new contact object with empty Contact_Roles_c field
Contact c = new Contact(id = ac.Contact__c);
c.Roles__c = '';
updateList.add(c);
}
}
if(updateList.size()>0){
update updateList;
}
}
@isTest
private class EmptyContactRole
{
static testMethod void validateEmptyContactRole()
{
Account_Contact_Role__c R = new Account_Contact_Role__c (Account__c= 'Bay Area Council', Contact__c = 'Shawn Nguyen', Roles__c = 'Primary Contact')
//Insert Account Contact Role insert R
;
//Delete Account Contact Role delete R
delete(R);
System.assertEquals ('',c.Roles__c)
;
}
}
I updated my test code to this, but the assertion failed.
@isTest
private class EmptyContactRole
{
static testMethod void validateEmptyContactRole()
{
//insert records
Contact c1 = new Contact(lastname = 'test', Nickname__c='test');
insert c1;
Account a1 = new Account(name = 'test1', Account_Type__c='non-Member', Industry='Banking');
insert a1;
Account_Contact_Role__c R = new Account_Contact_Role__c (Account__c= a1.id, Contact__c = c1.id, Roles__c = 'Primary Contact')
//Insert insert R
;
System.assertEquals (c1.Roles__c,'Primary Contact');
//Delete Account Contact Role delete R
delete R;
System.assertEquals (c1.Roles__c,'');
}
}
Error Message System.AssertException: Assertion Failed: Expected: null, Actual: Primary Contact
Stack Trace Class.EmptyContactRole.validateEmptyContactRole: line 14, column 1
The upside of sapphire glass that is utilized in our Rolex Imitation architect Watches over customary glass.
You can check that replica watches (https://identicalwatchess.to/)