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
AbAb 

populating child value from parent with help of trigger

Hello,

I have a currency field on my object with name Price__c i want to populate it from the field (ParentPrice__c ) of parent (Parent__c) 

how can i achieve it in trigger,
thanks for suggestion
Best Answer chosen by Ab
Balajee S 10Balajee S 10
trigger populate on Price__c (after insert)
{
List<Price__c > child = new List<Price__c >();
List<Parent__c> parent = [select id,ParentPrice__c  from Parent__c where id in: trigger.newmap.keyset()];
for(Parent__c con: parent)
{
con.currencyfield__c = con.ParentPrice__c ;
child.add(con);
}  
update child;
}

All Answers

Sid_CloudSid_Cloud
Instead of Trigger, try using Process Builder. I belive it should be possible using a new Process in Process Builder. 
Balajee S 10Balajee S 10
trigger populate on Price__c (after insert)
{
List<Price__c > child = new List<Price__c >();
List<Parent__c> parent = [select id,ParentPrice__c  from Parent__c where id in: trigger.newmap.keyset()];
for(Parent__c con: parent)
{
con.currencyfield__c = con.ParentPrice__c ;
child.add(con);
}  
update child;
}
This was selected as the best answer
mritzimritzi
Use the following code and let me know how it behaves
 
trigger customTrigger on Child__c(before Insert, after Insert,
								before Update, after Update,
								before Delete, after Delete){
	
	if(Trigger.isInsert && Trigger.isAfter){
		for(Object__c o : Trigger.new)
			o.Price__c = o.ParentLookup__r.ParentPrice__c;
		
	}
	//Note: It is advised to have only one Trigger per Object.
	//Replace "ParentLookup" with the name of parent lookup field on child object.
}

 
sandeep reddy 37sandeep reddy 37
balagi wrote the correct one
 
vinod bandanadamvinod bandanadam
balagi the child.add(con) i am getting error mehod or signature doesnt exist please check once