You need to sign in to do that
Don't have an account?
trigger to update field value in parent object with child object's value
Hi,
I was writting a trigger to update field value in parent object with child object's value:
Product--- Custom Parent Object
Product Period Data---Custom Child Object
field to update in parent object ---Days_Delinquent__c
trigger UpdateProduct on ENT_NMTC_Product_Period_Data__c (after insert)
{
for(ENT_NMTC_Product_Period_Data__c ppd : System.Trigger.New)
{
ENT_NMTC_Product__c prod = new ENT_NMTC_Product__c();
ppd.NMTC_Product__r.Days_Delinquent__c = ppd.Days_Delinquent__c;
update(prod);
}
I am not getting any error message in trigger but it does not satisfy the purpose too
You'll have to change it to something like such...
Hi ,
I have modified the trigger a little but as before it does not update field value of child record into parent record
rigger UpdateProduct on ENT_NMTC_Product_Period_Data__c (after insert)
{
Set<Id> parentIds = new Set<Id>();
//Set<Id> productPeriodDataIds = new Set<Id>();
for(ENT_NMTC_Product_Period_Data__c ppd :trigger.new)
{
parentIds.add(ppd.NMTC_Product__c);
}
Map<Id, ENT_NMTC_Product__c> parentMap = new Map<Id, ENT_NMTC_Product__c>([SELECT Id ,Days_Delinquent__c FROM ENT_NMTC_Product__c WHERE Id IN :parentIds]);
for(ENT_NMTC_Product_Period_Data__c ppd : System.Trigger.New)
{
ENT_NMTC_Product__c prod = parentMap.get(ppd.NMTC_Product__c);
prod.Days_Delinquent__c = ppd.NMTC_Product__r.Days_Delinquent__c;
update prod;
}
if(null != parentMap && parentMap.values().size() > 0)
{
update parentMap.values();
}
}
You can suggest what could be the reason
Thanks
This line of code...
Because you are getting information from another Sobject you need to query for it prior... I would do this then