You need to sign in to do that
Don't have an account?
SFDC 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)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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; } }
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.
Try this code :
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.
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