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
Mo SyedMo Syed 

Basic trigger help for non Dev

HI All 
Can anyone help with with a basic trigger please? 
Im an admin so not a dev. I am experienced in using Visual Flows and can fulfil most requirements using that, its incredibly powerfull. However it has a limitation, which is you cannot trigger a VISUAL FLOW AFTER DELETE of a record. 

Hence I need an example trigger. This is a good oppty for me to head over into the Dev side and learn since im limited by native automation. 
I have a object called
Bookings  (custom)- Associated to Account via Lookup 
On Delete of  a Bookiing record all I need is to Update the Account object a custom field called Recalculate

I have a visual flow when the Recalculate is TRUE it loops through all child records, aggregates and SUMS on the parent. Hence I need this Recalculate checked to TRUE on delete of Booking record. 

Can anyone kindly help with this basic trigger please? 

 
LBKLBK
Hi,

Try this Trigger.
 
trigger updateAccount on Booking__c (after delete) {
    List<Id> lstAccountIds = new List<Id>();
	
	//Fetch the account ids from deleted Bookings
    for (Booking__c booking : trigger.old){ 
        if(booking.Account__c != null){
            lstAccountIds.add(booking.Account__r.Id);
        }
    }
	
	//Retrieve all the accounts to be recalculated
    Map<Id, Account> mapAccounts = new Map<Id, Account>([SELECT Id, Recalculate__c FROM Account WHERE Id =: lstAccountIds]);
	List<Account> lstAccounts = new List<Account>();
	
	//Set Recalculate__c to true;
    for (Booking__c booking : trigger.old){  
        if(booking.Account__c != null){
            Account account = mapAccounts.get(booking.Account__r.Id);
			account.Recalculate__c = true;
			lstAccounts.add(account);
        }
    }
	
	//update all the accounts to be recalculated.
	update lstAccounts;
}
Look out for syntactical errors related to your Custom Object and Custom field names.

Let me know how it goes.