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
RAJNagRAJNag 

Update field value

Hi
i wrote the trigger my task is whenever Asset price field changes then update the same value in product object custom field New_Price_Value__c 
trigger updateContactDesriptionWithAccount on Product2(after insert,after update){

    set<id> conids=new set<id>();
    for(Product2 con:trigger.new){
    
        
        
            conids.add(con.id);
    
    }
    list<Product2> conlst=[select id,New_Price_Value__c,(select Price from Assets) from Product2 where id in:conids];
           
    for(Product2 c:conlst){
    
        New_Price_Value__c = c.Asset.Price ;
       
    }
    update conlst;
    
}

trigger updateContactDesriptionWithAccount on Product2(after insert,after update){

    set<id> conids=new set<id>();
    for(Product2 con:trigger.new){
    
        
        
            conids.add(con.id);
    
    }
    list<Product2> conlst=[select id,nagaraju__New_Price_Value__c,(select Price from Assets) from Product2 where id in:conids];
           
    for(Product2 c:conlst){
    
        c.trigger updateContactDesriptionWithAccount on Product2(after insert,after update){

    set<id> conids=new set<id>();
    for(Product2 con:trigger.new){
    
        
        
            conids.add(con.id);
    
    }
    list<Product2> conlst=[select id,nagaraju__New_Price_Value__c,(select Price from Assets) from Product2 where id in:conids];
           
    for(Product2 c:conlst){
    
        c.nagaraju__New_Price_Value__c = c.Asset.Price ;
       
    }
    update conlst;
    
}= c.Asset.Price ;
       
    }
    update conlst;
    
}

but i got the error like this *Error: Compile Error: Invalid foreign key relationship: Product2.Asset at line 15 column 40
Plz Can any one help me 
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi ,

Please try below code and make sure which asset price you want to populate over product field as there can be many assets for one product.
 
trigger updateContactDesriptionWithAccount on Product2(after insert,after update){

    set<id> conids=new set<id>();
    for(Product2 con:trigger.new){
    
        
        
            conids.add(con.id);
    
    }
    list<Product2> conlst=[select id,New_Price_Value__c,(select Price from Assets) from Product2 where id in:conids];
           
    for(Product2 c:conlst){
		
		for(Asset a:c.Assets){
				c.New_Price_Value__c = a.Price ;
		}
        
       
    }
    update conlst;
    
}

trigger updateContactDesriptionWithAccount on Product2(after insert,after update){

    set<id> conids=new set<id>();
    for(Product2 con:trigger.new){
    
        
        
            conids.add(con.id);
    
    }
    list<Product2> conlst=[select id,nagaraju__New_Price_Value__c,(select Price from Assets) from Product2 where id in:conids];
           
    for(Product2 c:conlst){
    
        c.trigger updateContactDesriptionWithAccount on Product2(after insert,after update){

    set<id> conids=new set<id>();
    for(Product2 con:trigger.new){
    
        
        
            conids.add(con.id);
    
    }
    list<Product2> conlst=[select id,nagaraju__New_Price_Value__c,(select Price from Assets) from Product2 where id in:conids];
           
    for(Product2 c:conlst){
    
        c.nagaraju__New_Price_Value__c = c.Asset.Price ;
       
    }
    update conlst;
    
}= c.Asset.Price ;
       
    }
    update conlst;
    
}

Let us know if it helps you.
RAJNagRAJNag
Hi code will be saved without error,But when i am save to product record i got this error.

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger nagaraju.updateContactDesriptionWithAccount1 caused an unexpected exception, contact your administrator: nagaraju.updateContactDesriptionWithAccount1: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 01t280000006BJHAA2; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, nagaraju.updateContactDesriptionWithAccount1: maximum trigger depth exceeded Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH]: []: Trigger.nagaraju.updateContactDesriptionWithAccount1: line 21, column 1
Amit Chaudhary 8Amit Chaudhary 8
Please try below code :-
trigger updateContactDesriptionWithAccount on Product2(after insert,after update){

    set<id> conids=new set<id>();
    for(Product2 con:trigger.new){
    
        
        
            conids.add(con.id);
    
    }
    list<Product2> conlst=[select id,New_Price_Value__c,(select Price from Assets) from Product2 where id in:conids];
        
    List<Product2> 	conlstToUpdate = new List<Product2> ();		
    for(Product2 c:conlst)
	{
		for(Asset a:c.Assets)
		{
				c.New_Price_Value__c = a.Price ;
		}
		conlstToUpdate.add(c);
    }
	
    //update conlst;
    update conlstToUpdate;
	
}

trigger updateContactDesriptionWithAccount on Product2(after insert,after update){

    set<id> conids=new set<id>();
    for(Product2 con:trigger.new){
    
        
        
            conids.add(con.id);
    
    }
    list<Product2> conlst=[select id,nagaraju__New_Price_Value__c,(select Price from Assets) from Product2 where id in:conids];
           
    for(Product2 c:conlst){
    
        c.trigger updateContactDesriptionWithAccount on Product2(after insert,after update){

    set<id> conids=new set<id>();
    for(Product2 con:trigger.new)
	{
            conids.add(con.id);
    }
	
    //list<Product2> conlst=[select id,nagaraju__New_Price_Value__c,(select Price from Assets) from Product2 where id in:conids];
    list<Product2> conlst=[select id,nagaraju__New_Price_Value__c from Product2 where id in:conids];
           
    for(Product2 c:conlst)
	{
        c.nagaraju__New_Price_Value__c = c.Asset.Price ;
    }
	
    update conlst;
    
}= c.Asset.Price ;
       
    }
    update conlst;
    
}
Please let us know if this will help you


 
RAJNagRAJNag
Hi i am trying this code but i got same error

trigger updateContactDesriptionWithAccount1 on Product2(after insert,after update){

    set<id> conids=new set<id>();
    for(Product2 con:trigger.new){
    
        
        
            conids.add(con.id);
    
    }
    list<Product2> conlst=[select id,nagaraju__New_Price_Value__c,(select Price from Assets) from Product2 where id in:conids];
        
    List<Product2>  conlstToUpdate = new List<Product2> ();     
    for(Product2 c:conlst)
    {
        for(Asset a:c.Assets)
        {
                c.nagaraju__New_Price_Value__c = a.Price ;
        }
        conlstToUpdate.add(c);
    }
    
    //update conlst;
    update conlstToUpdate;
    
}

Error is

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger nagaraju.updateContactDesriptionWithAccount1 caused an unexpected exception, contact your administrator: nagaraju.updateContactDesriptionWithAccount1: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 01t280000006BJHAA2; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, nagaraju.updateContactDesriptionWithAccount1: maximum trigger depth exceeded Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH] Product2 trigger event AfterUpdate for [01t280000006BJH]: []: Trigger.nagaraju.updateContactDesriptionWithAccount1: line 24, column 1
Amit Chaudhary 8Amit Chaudhary 8
try to use before update Trigger