+ Start a Discussion
AmitabhleoAmitabhleo 

Update a child field from the parent formula field

Hi ,

 

I am trying to update a contact field from the parent account formula field which returns a percent.

 

Below is my code

public with sharing class HelloUpdateAccount { public static void updateAcc(Account[]accs){ // Create a list of account records from a SOQL query List<Account> accs1 = [SELECT Id,BillingCity, Name FROM Account WHERE Name = 'Acme' limit 1]; // Loop through the list and update the Name field for(Account a : accs1){ //Retrieve the contact Contact cont = new Contact(); cont = [select Name,Id from Contact where AccountId =:a.id]; cont.MailingCity = a.BillingCity; //check_tax_update__c is a test formula field which returns a percent below is the code //IF( SLAExpirationDate__c >today(), 50, 90) cont.SLA_check__c=46; //a.check_tax_update__c; update cont; } } }

 

Thanks in advance.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
snugglessnuggles

hi amitabhleo,

 

i would try doing this with a formula field instead.  build a formula on contact and reference the account info you need using the advanced formula tab.

 

also, i noticed you have a lot of dml/queries in your loop which will cause you to hit some governor limits very quickly if you aren't careful.

All Answers

snugglessnuggles

hi amitabhleo,

 

i would try doing this with a formula field instead.  build a formula on contact and reference the account info you need using the advanced formula tab.

 

also, i noticed you have a lot of dml/queries in your loop which will cause you to hit some governor limits very quickly if you aren't careful.

This was selected as the best answer
AmitabhleoAmitabhleo

Thanks Snuggles,

 

it worked.