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
CprocessingCprocessing 

Update All Child Objects From Parent - What am i doing wrong?

trigger SigningLiveUpdate on Account (before update) {

	//Get accounts from Trigger
	Account account = trigger.new[0];
    
    //If criteria in Account are met then proceed
    if(account.CustomerStatus__c = 'Approved')
    {
    	//Create New Signing Array - I'm aware i could condense this step, but it's an attempt to make it work
    	Signing__c[] allSignings = new Signing__c[0];
    	
    	//Add all Signing__c objects that share that are related to the account
    	allSignings = [select Live__c from Signing__c where Id =: account.Id];       
    
    	//Go through each related Signing__c object and set them to live
    	if(allSignings.size()>0) {
    		for(Signing__c signing: allSignings){
    	    	signing.Live__c = true;
    	    	//update signing;
       		}
    	}
    }
   	
   	// Update Signings
    update allSignings;              
}

 

Signing__c is a custom object that has a Master-Detail Relationship to Account.

 

Essentially all i want to do is when a the status in Account gets set to Approved i want all the signing objects to then get set to "Live"

 

This must be doable? What am i doing wrong guys?

 

CProcessing.

Best Answer chosen by Admin (Salesforce Developers) 
CprocessingCprocessing

Hi Guys,

 

Sorry to clutter up your forum, i quite clearly forgot how to query a database when i wrote the above code.

 

I simple wasn't pulling out the signing objects based upon their relationship to the account, rather their own ID

 

trigger SigningLiveUpdate on Account (after update) {
	
	// Initiate List
	List<Account> accounts = new List<Account>();
	
	// Loop through all accounts effected by the trigger
	for(Account account: trigger.new)
	{
		// If the account meets the required condition add it to your list of Accounts that have children that need updating
		if(true)
		{
			accounts.add(account);
		}
	}
    
    for(Account account: accounts)
    {
    	// Restrict Signings to be updated by the field that needs updating and Account ID
    	List<Signing__c> signings = [select Id, Account__c, Live__c from Signing__c where Account__c =: account.Id AND Live__c =: false];       
    	
    	// Loop Through signings, update field, update object.
    	for(Signing__c signing: signings)
    	{
    		signing.Live__c = true;
    		update signing;
    	}
    }      
}

 

All Answers

CprocessingCprocessing

Hi Guys i've neatened my code a little, but still no joy.

 

For simplicity i've just set the conditional value to "true"

 

trigger SigningLiveUpdate on Account (after update) {

	List<Account> accounts = new List<Account>();
	
for(Account account: trigger.new)
{
	accounts.add(account);
}
    
    for(Account account: accounts)
    {
    	if(true)
    	{
    		List<Signing__c> signings = [select Id, Live__c from Signing__c where Id =: account.Id];       
    		
    		for(Signing__c signing: signings)
    		{
    			signing.Live__c = true;
    			update signing;
    		}
    	}
    }      
}

 

CprocessingCprocessing

Hi Guys,

 

Sorry to clutter up your forum, i quite clearly forgot how to query a database when i wrote the above code.

 

I simple wasn't pulling out the signing objects based upon their relationship to the account, rather their own ID

 

trigger SigningLiveUpdate on Account (after update) {
	
	// Initiate List
	List<Account> accounts = new List<Account>();
	
	// Loop through all accounts effected by the trigger
	for(Account account: trigger.new)
	{
		// If the account meets the required condition add it to your list of Accounts that have children that need updating
		if(true)
		{
			accounts.add(account);
		}
	}
    
    for(Account account: accounts)
    {
    	// Restrict Signings to be updated by the field that needs updating and Account ID
    	List<Signing__c> signings = [select Id, Account__c, Live__c from Signing__c where Account__c =: account.Id AND Live__c =: false];       
    	
    	// Loop Through signings, update field, update object.
    	for(Signing__c signing: signings)
    	{
    		signing.Live__c = true;
    		update signing;
    	}
    }      
}

 

This was selected as the best answer