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
SFDC DummySFDC Dummy 

How to update master object field when child object field updated

Hi 

How to update master object field when child object field updated

FOR EXAMPLE 
++++++++++++++++++++++++++++++
Bank__c --> Child Object

Balance__c --> Parent Object

There is a lookup between these two object 'test__c'

When i am doing any transcation on Bank__c it automatically updated on Child object means Balance__c
(Txt_Date__c field from child will be updated on Date__c field of parent)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
trigger tarunopp on Bank__c (after update,after insert) {

list<id> list = new list<id>();

for(Bank__c opp:trigger.new)
    {
list.add(opp.test__c);

   }

list<Balance__c > conlist = new list<Balance__c >();

conlist = [select id,Date__c, (Select id,test__c,Txt_Date__c from Bank__c) from Balance__c where ID IN : list];

for(Balance__c con:conlist)


 {
     
con.Date__c=opp.Txt_Date__c;
      update con;     
 }

}
Abhishek BansalAbhishek Bansal
Hi,

Please try the below code :

trigger tarunopp on Bank__c (after update,after insert) {
    set<id> balanceIds = new set<id>();
    
    for(Bank__c opp:trigger.new) {
        balanceIds.add(opp.test__c);
    }
    
    Map<Id,Balance__c > balMap = new Map<id,Balance__c >();
    List<Balance__c> updateList = new List<Balance__c>();
    balMap = [select id,Date__c from Balance__c where ID IN : balanceIds];

    for(Bank__c opp:trigger.new){
        if(balMap.containsKey(opp.test__c)){
            balMap.get(opp.test__c).Date__c = opp.Txt_Date__c;
            updateList.add(balMap.get(opp.test__c));
        }
    }
    if(updateList.size() > 0){
        update updateList;
    }
}

Thanks,
Abhishek.
ghada fouraneghada fourane
First of all you should avoid the DML statements inside the loop:
Try this code :
 
trigger tarunopp on Bank__c (after update,after insert) {

list<id> list = new list<id>();

for(Bank__c opp:trigger.new)
    {
list.add(opp.test__c);

   }

list<Balance__c > conlist = new list<Balance__c >();

conlist = [select id,Date__c, bank__c,bank__r.id, bank__r.Txt_Date__c,bank__r.test__c from Balance__c where bank__r.test__c IN : list];

for(Balance__c con:conlist)


 {
     
con.Date__c=bank__r.Txt_Date__c;
         
 }
update conlist;


 
SFDC DummySFDC Dummy
Hi Abhishek. i had tried code what u modified but its throwing error like [image: Error]Error: Compile Error: Illegal assignment from List to Map at line 10 column 5
Abhishek BansalAbhishek Bansal
Hi,

I have done modification in code. please try the below code.

trigger tarunopp on Bank__c (after update,after insert) {
    set<id> balanceIds = new set<id>();
    
    for(Bank__c opp:trigger.new) {
        balanceIds.add(opp.test__c);
    }
    
    Map<Id,Balance__c > balMap = new Map<id,Balance__c >([select id,Date__c from Balance__c where ID IN : balanceIds]);
    List<Balance__c> updateList = new List<Balance__c>();

    for(Bank__c opp:trigger.new){
        if(balMap.containsKey(opp.test__c)){
            balMap.get(opp.test__c).Date__c = opp.Txt_Date__c;
            updateList.add(balMap.get(opp.test__c));
        }
    }
    if(updateList.size() > 0){
        update updateList;
    }
}

Hope this will help you.

Thanks,
Abhishek.
 
SFDC DummySFDC Dummy
Hi Abhishek Its saved without any error .but after creating any reocrd on bank__c there is no update on balance__c field
Abhishek BansalAbhishek Bansal
Hi,

I don't see any issue with the code here.

Please check the below points :
1. Your trigger is active.
2. Test__c is properly populated during the creation of record.
3. Txt_Date__c is not blank when record is created.

If all of the above points are right than please try the updated code below :

trigger tarunopp on Bank__c (after update,after insert) {
    set<id> balanceIds = new set<id>();
    
    for(Bank__c opp:trigger.new) {
        balanceIds.add(opp.test__c);
    }
    
    Map<Id,Balance__c > balMap = new Map<id,Balance__c >([select id,Date__c from Balance__c where ID IN : balanceIds]);

    for(Bank__c opp:trigger.new){
        if(balMap.containsKey(opp.test__c)){
            balMap.get(opp.test__c).Date__c = opp.Txt_Date__c;
        }
    }
    update balMap.values();
}

Thanks,
Abhishek