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
Shawn NguyenShawn 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!
Hargobind_SinghHargobind_Singh
Hi, 

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. 

//this trigger empties the Contact_Roles_c field on Contact
//assuming that Account_Contact_Roles_c has a lookup to Contact 
//and field name is Contact__c 

trigger emptyField on Account_Contact_Roles_c(after delete){
	
	List<Contact> updateList = new List<Contact>(); 
	for(Account_Contact_Roles_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.Contact_Roles_c = '';
			updateList.add(c); 
		}
	}
	if(updateList.size()>0){
		update updateList; 
	}
}




Shawn NguyenShawn Nguyen
Roles_c is a multi-picklist on Account_Contact_Roles_c object.
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. 
Hargobind_SinghHargobind_Singh
Ok, in that case, just use the correct field on Contact.

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 ? 





Shawn NguyenShawn Nguyen
PERFECT!! Thanks for your help. Just had to copy and paste the exact API name to be working correctly. Otherwise, works like a charm.

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;
    }
}
Hargobind_SinghHargobind_Singh
Great, good to know that it works. Please mark this post as "Solved". 

Shawn NguyenShawn Nguyen
What would be the Test code for this trigger?
Shawn NguyenShawn Nguyen
This is as far as I got and received the following error: Variable does not exist: c.Roles__c at line 11 column 30

@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)
     ;
    }
}
Shawn NguyenShawn Nguyen
@Hargobind_Singh,
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
Jonye KeeJonye Kee

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/)