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
Steven Wellman 28Steven Wellman 28 

Trigger After Delete not updating

I'm trying to update a field on the account when a child record is deleted but the field isn't updating. Here's the trigger:
trigger RatePlanDeletion on Zuora__SubscriptionRatePlan__c (after delete) {

	// Make a list of the deleted subscription rate plans
	List<Zuora__SubscriptionRatePlan__c> deletedRatePlan = [SELECT Id,
Name,
Zuora__Account__r.Local_Listings__c,
Zuora__Account__r.Engage__c,
Zuora__Account__r.SEO__c,
Zuora__Account__r.Social_Ads__c,
Zuora__Account__r.Social_Posting__c,
Zuora__Account__r.Website_Product__c
Zuora__SubscriptionRatePlan__c
IN :Trigger.old];

	List<Zuora__SubscriptionRatePlan__c> newRatePlan = new List<Zuora__SubscriptionRatePlan__c>();

	for (Zuora__SubscriptionRatePlan__c sub : deletedRatePlan) {

		// Get Trigger.new version of the rate plan
		Zuora__SubscriptionRatePlan__c subInTriggerNew = Trigger.newMap.get(sub.Id);		
		
		// Local Listings
		if ((sub.Name.contains('Marketing Essentials') || sub.Name.contains('Local Listings'))) {
			subInTriggerNew.Zuora__Account__r.Local_Listings__c = false;
		}

		// Engage 
		if (sub.Name.contains('Engage')) {
			subInTriggerNew.Zuora__Account__r.Engage__c = false;
		}

		// Reviews
		if (sub.Name.contains('Review')) {
			subInTriggerNew.Zuora__Account__r.Reviews__c = false;
		}

		// SEO
		if (sub.Name.contains('SEO')) {
			subInTriggerNew.Zuora__Account__r.SEO__c = false;
		}

		// Social Ads
		if (sub.Name.contains('Social Ads')) {
			subInTriggerNew.Zuora__Account__r.Social_Ads__c = false;
		}

		// Social Posting
		if (sub.Name.contains('Social Posting')) {
			subInTriggerNew.Zuora__Account__r.Social_Posting__c = false;
		}

		// Website
		if (sub.Name.contains('Website')) {
			subInTriggerNew.Zuora__Account__r.Website_Product__c = false;
		}
		
		newRatePlan.add(subInTriggerNew);
	}
	update newRatePlan;
}
I appreciate any help I can get.
Jay Wang 2Jay Wang 2
Hi Steven,

I believe that Trigger.newMap does not work in delete triggers. Try replacing that with Trigger.oldMap and let me know how that goes.

Kind Regards,

Jay
Steven NsubugaSteven Nsubuga
Hi Steven, try this
trigger RatePlanDeletion on Zuora__SubscriptionRatePlan__c (after delete) {

	// Make a list of the deleted subscription rate plans
	List<Zuora__SubscriptionRatePlan__c> deletedRatePlan = [SELECT Id,
		Name,
		Zuora__Account__c,
		Zuora__Account__r.Local_Listings__c,
		Zuora__Account__r.Engage__c,
		Zuora__Account__r.SEO__c,
		Zuora__Account__r.Social_Ads__c,
		Zuora__Account__r.Social_Posting__c,
		Zuora__Account__r.Website_Product__c FROM
		Zuora__SubscriptionRatePlan__c WHERE ID
		IN :Trigger.oldMap.keyset()];

	Set<Id> accountIds = new Set<Id>();
	for (Zuora__SubscriptionRatePlan__c sub : Trigger.old) {
		accountIds.add(sub.Zuora__Account__c);
	}

	Map<Id, Account> accountsMap = new Map<Id, Account>([SELECT Local_Listings__c, Engage__c, Reviews__c, SEO__c, Social_Ads__c, Social_Posting__c, Website_Product__c
		FROM Account WHERE Id IN :accountIds]);
		
	for (Zuora__SubscriptionRatePlan__c sub : deletedRatePlan) {

		if (accountsMap.keyset().contains(sub.Zuora__Account__c)){
			
			// Local Listings
			if ((sub.Name.contains('Marketing Essentials') || sub.Name.contains('Local Listings'))) {
				accountsMap.get(sub.Zuora__Account__c).Local_Listings__c = false;
			}
			
			// Engage 
			if (sub.Name.contains('Engage')) {
				accountsMap.get(sub.Zuora__Account__c).Engage__c = false;
			}
			
			// Reviews
			if (sub.Name.contains('Review')) {
				accountsMap.get(sub.Zuora__Account__c).Reviews__c = false;
			}
			
			// SEO
			if (sub.Name.contains('SEO')) {
				accountsMap.get(sub.Zuora__Account__c).SEO__c = false;
			}

			// Social Ads
			if (sub.Name.contains('Social Ads')) {
				accountsMap.get(sub.Zuora__Account__c).Social_Ads__c = false;
			}

			// Social Posting
			if (sub.Name.contains('Social Posting')) {
				accountsMap.get(sub.Zuora__Account__c).Social_Posting__c = false;
			}

			// Website
			if (sub.Name.contains('Website')) {
				accountsMap.get(sub.Zuora__Account__c).Website_Product__c = false;
			}
		}
	}
	update accountsMap.values(); 
}

 
Steven Wellman 28Steven Wellman 28
It's still not working. Here's the test class (maybe there's something wrong with this):
@isTest
private class RatePlanDeletionTest {
	
	@isTest static void test_method_one() {
		// Create Account and Contact 
		Account acc = New Account();
		acc.Name = '1234567890987654321';
		acc.Local_Listings__c = true;

		insert acc;

		Contact myCon = New Contact();
		myCon.FirstName = 'Steve';
		myCon.LastName  = 'Welderbeast';
		myCon.Email     = 'apextest@gmail.com';
		myCon.AccountId = acc.Id;

		insert myCon;
	
		// Create Subscription
		Zuora__Subscription__c mySub = New Zuora__Subscription__c();
		mySub.Name = 'Apex Test Sub';
		mySub.Zuora__Zuora_Id__c = '1';
	
		insert mySub;

		// Local Listings
		Zuora__SubscriptionRatePlan__c myRateLL = New Zuora__SubscriptionRatePlan__c();
		myRateLL.Zuora__Subscription__c = mySub.Id;
		myRateLL.Name = 'Local Listings';

		insert myRateLL;
		delete myRateLL;

		Account accLL = [SELECT Local_Listings__c
						   FROM Account
						  WHERE Id = :acc.Id];

		system.assertEquals(false,accLL.Local_Listings__c);

		// Engage 
		Zuora__SubscriptionRatePlan__c myRateE = New Zuora__SubscriptionRatePlan__c();
		myRateE.Zuora__Subscription__c = mySub.Id;
		myRateE.Name = 'Engage';

		insert myRateE;
		delete myRateE;

		Account accE = [SELECT Engage__c
						   FROM Account
						  WHERE Id = :acc.Id];

		system.assertEquals(false,accE.Engage__c);

		// Reviews
		Zuora__SubscriptionRatePlan__c myRateR = New Zuora__SubscriptionRatePlan__c();
		myRateR.Zuora__Subscription__c = mySub.Id;
		myRateR.Name = 'Review';

		insert myRateR;
		delete myRateR;

		Account accR = [SELECT Reviews__c
						   FROM Account
						  WHERE Id = :acc.Id];

		system.assertEquals(false,accR.Reviews__c);

		// SEO
		Zuora__SubscriptionRatePlan__c myRateSEO = New Zuora__SubscriptionRatePlan__c();
		myRateSEO.Zuora__Subscription__c = mySub.Id;
		myRateSEO.Name = 'SEO';

		insert myRateSEO;
		delete myRateSEO;

		Account accSEO = [SELECT SEO__c
						    FROM Account
						   WHERE Id = :acc.Id];

		system.assertEquals(false,accSEO.SEO__c);

		// Social Ads
		Zuora__SubscriptionRatePlan__c myRateSA = New Zuora__SubscriptionRatePlan__c();
		myRateSA.Zuora__Subscription__c = mySub.Id;
		myRateSA.Name = 'Social Ads';

		insert myRateSA;
		delete myRateSA;

		Account accSA = [SELECT Social_Ads__c
						   FROM Account
						  WHERE Id = :acc.Id];

		system.assertEquals(false,accSA.Social_Ads__c);

		// Social Posting
		Zuora__SubscriptionRatePlan__c myRateSP = New Zuora__SubscriptionRatePlan__c();
		myRateSP.Zuora__Subscription__c = mySub.Id;
		myRateSP.Name = 'Social Posting';

		insert myRateSP;
		delete myRateSP;

		Account accSP = [SELECT Social_Posting__c
						   FROM Account
						  WHERE Id = :acc.Id];

		system.assertEquals(false,accSP.Social_Posting__c);

		// Website
		Zuora__SubscriptionRatePlan__c myRateW = New Zuora__SubscriptionRatePlan__c();
		myRateW.Zuora__Subscription__c = mySub.Id;
		myRateW.Name = 'Social Posting';

		insert myRateW;
		delete myRateW;

		Account accW = [SELECT Website_Product__c
						   FROM Account
						  WHERE Id = :acc.Id];

		system.assertEquals(false,accW.Website_Product__c);

	}

		
}

 
Steven NsubugaSteven Nsubuga
Hi Steven, the issue seems to be your test class.
I expected to see a Lookup or Master Detail field called Zuora__Account__c linking Zuora__SubscriptionRatePlan__c  to the Account.
See updated test class below
@isTest
private class RatePlanDeletionTest {
	
	@isTest static void test_method_one() {
		// Create Account and Contact 
		Account acc = New Account();
		acc.Name = '1234567890987654321';
		acc.Local_Listings__c = true;

		insert acc;

		Contact myCon = New Contact();
		myCon.FirstName = 'Steve';
		myCon.LastName  = 'Welderbeast';
		myCon.Email     = 'apextest@gmail.com';
		myCon.AccountId = acc.Id;

		insert myCon;
	
		// Create Subscription
		Zuora__Subscription__c mySub = New Zuora__Subscription__c();
		mySub.Name = 'Apex Test Sub';
		mySub.Zuora__Zuora_Id__c = '1';
	
		insert mySub;

		// Local Listings
		Zuora__SubscriptionRatePlan__c myRateLL = New Zuora__SubscriptionRatePlan__c();
		myRateLL.Zuora__Subscription__c = mySub.Id;
		myRateLL.Name = 'Local Listings';
        myRateLL.Zuora__Account__c  = acc.Id;

		insert myRateLL;
		delete myRateLL;

		Account accLL = [SELECT Local_Listings__c
						   FROM Account
						  WHERE Id = :acc.Id];

		system.assertEquals(false,accLL.Local_Listings__c);

		// Engage 
		Zuora__SubscriptionRatePlan__c myRateE = New Zuora__SubscriptionRatePlan__c();
		myRateE.Zuora__Subscription__c = mySub.Id;
		myRateE.Name = 'Engage';
        myRateE.Zuora__Account__c = acc.Id;

		insert myRateE;
		delete myRateE;

		Account accE = [SELECT Engage__c
						   FROM Account
						  WHERE Id = :acc.Id];

		system.assertEquals(false,accE.Engage__c);

		// Reviews
		Zuora__SubscriptionRatePlan__c myRateR = New Zuora__SubscriptionRatePlan__c();
		myRateR.Zuora__Subscription__c = mySub.Id;
		myRateR.Name = 'Review';
        myRateR.Zuora__Account__c = acc.Id;

		insert myRateR;
		delete myRateR;

		Account accR = [SELECT Reviews__c
						   FROM Account
						  WHERE Id = :acc.Id];

		system.assertEquals(false,accR.Reviews__c);

		// SEO
		Zuora__SubscriptionRatePlan__c myRateSEO = New Zuora__SubscriptionRatePlan__c();
		myRateSEO.Zuora__Subscription__c = mySub.Id;
		myRateSEO.Name = 'SEO';
        myRateSEO.Zuora__Account__c = acc.Id;

		insert myRateSEO;
		delete myRateSEO;

		Account accSEO = [SELECT SEO__c
						    FROM Account
						   WHERE Id = :acc.Id];

		system.assertEquals(false,accSEO.SEO__c);

		// Social Ads
		Zuora__SubscriptionRatePlan__c myRateSA = New Zuora__SubscriptionRatePlan__c();
		myRateSA.Zuora__Subscription__c = mySub.Id;
		myRateSA.Name = 'Social Ads';
        myRateSA.Zuora__Account__c = acc.Id;

		insert myRateSA;
		delete myRateSA;

		Account accSA = [SELECT Social_Ads__c
						   FROM Account
						  WHERE Id = :acc.Id];

		system.assertEquals(false,accSA.Social_Ads__c);

		// Social Posting
		Zuora__SubscriptionRatePlan__c myRateSP = New Zuora__SubscriptionRatePlan__c();
		myRateSP.Zuora__Subscription__c = mySub.Id;
		myRateSP.Name = 'Social Posting';
        myRateSP.Zuora__Account__c = acc.Id;

		insert myRateSP;
		delete myRateSP;

		Account accSP = [SELECT Social_Posting__c
						   FROM Account
						  WHERE Id = :acc.Id];

		system.assertEquals(false,accSP.Social_Posting__c);

		// Website
		Zuora__SubscriptionRatePlan__c myRateW = New Zuora__SubscriptionRatePlan__c();
		myRateW.Zuora__Subscription__c = mySub.Id;
		myRateW.Name = 'Social Posting';
        myRateW.Zuora__Account__c = acc.Id;

		insert myRateW;
		delete myRateW;

		Account accW = [SELECT Website_Product__c
						   FROM Account
						  WHERE Id = :acc.Id];

		system.assertEquals(false,accW.Website_Product__c);

	}

		
}