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
Venkatesh Battini 9Venkatesh Battini 9 

Want to update Parent Object(Account) Field through Chlid Object(Contact) by using Triggers. Please suggest me code

Best Answer chosen by Venkatesh Battini 9
James LoghryJames Loghry
You're almost correct :). It is Contacts, not Contact.

All Answers

James LoghryJames Loghry
There are several results of this online already.  Here's one from a quick search: http://salesforce.stackexchange.com/questions/23338/trigger-to-update-parent-object-value-with-child-value
SarfarajSarfaraj
Hi 

Here is one sample for you,
trigger UpdateAmount on Contact (after insert, after update) { 
  Map<ID, Account> parentAcc = new Map<ID, Account>();
  List<Id> listIds = new List<Id>();

  for (Contact childObj : Trigger.new {
    listIds.add(childObj.Account);
  }

  parentAcc = new Map<Id, Account>([SELECT Id, Name, Phone FROM Account WHERE ID IN :listIds]);

  for (Contact c: Trigger:new){
     Account myParentOpp = parentAcc.get(c.Account__c);
     myParentOpp.Phone = c.Phone; //Update parent record as necessary.
  }

  update parentAcc.values();
}

 
Venkatesh Battini 9Venkatesh Battini 9
HI!!!!
I am writing like this
 acc=[Select id,Total__c,(Select id,Amount__c From Contact) From Account Where ID IN :listIds];

But its raising an error as below

Didn't understand relationship 'Contact' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.

What to do please help me.
James LoghryJames Loghry
You're almost correct :). It is Contacts, not Contact.
This was selected as the best answer
Venkatesh Battini 9Venkatesh Battini 9
Thanks alot James Loghry :) 
But i hve another doubt why it is Account not Accounts