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
Masie MaseliMasie Maseli 

trigger to update value using formula

Hi

I have a very simple trigger to update a few fields based on a formula field but its not updating. The trigger assist in showing the values of the formula field on the edit screen of my quote line items. Can someone please help.
trigger UpdateFieldsFormula on QuoteLineItem (before insert, before update) {
        
        for(QuoteLineItem q:trigger.new)
        {
         q.Quantity_on_hand__c = q.Product_Quantity_On_Hand__c;
         q.Cost_Price__c = q.Product_Cost_Price__c;
         q.In_Stock_Price2__c = q.In_Stock_Pricing__c;
 
}
}

 
ManojjenaManojjena
Hi Masie,

Please use below code .
trigger UpdateFieldsFormula on QuoteLineItem (after insert, after update) {
    List<QuoteLineItem> qliToUpdate=new  List<QuoteLineItem>();  
	for(QuoteLineItem qli:trigger.new){
	qli.Quantity_on_hand__c = qli.Product_Quantity_On_Hand__c;
	qli.Cost_Price__c = qli.Product_Cost_Price__c;
	qli.In_Stock_Price2__c = qli.In_Stock_Pricing__c;
    qliToUpdate.add(qli);
	}
	try{
	  update qliToUpdate;
	}catch(DmlException de){
	  System.debug(de);
	}
}

 
sandeep sankhlasandeep sankhla
HI Masie,

Above code should work on befor insert also..can you tell the type of the fields where you are assigning the values...because for me this same kind of scenario is working...I am abl eto update the feilds from formula firld on before insert..

Thanks
Masie MaseliMasie Maseli
Hi Manoj

Thanks for the suggestion but I still have the same problem- after selecting the products on the Add Quote Line Item screen the fields do not update please see the picture attached.User-added image
Masie MaseliMasie Maseli
Sandeep I am assigning the values to currency and number fields
sandeep sankhlasandeep sankhla
What is the return type of your formula fields ?