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 

Update field from product to quote line item entry screen

I have a field on the product that I would like visible as quote line items are entered. Can someone please tell me what is wrong with my code below?
trigger UpdateFormulaFields on QuoteLineItem (before insert,before update) {

 set<id> PIds = new set<id>();
   for(QuoteLineItem qli : Trigger.new){
       PIds.add(qli.Product2Id);
   }
     list<Product2> pList = [select id, name , Cost_Price__c, Quantity_On_Hand__c, In_Stock_Pricing__c  from Product2 where Id IN : PIds];  
     for(Product2 p : pList){
      
         for(QuoteLineItem ql : Trigger.new){
             if(p.id == ql.product2Id){
                
                 if(Trigger.isInsert){
                     
                 	ql.Cost_Price__c = p.Cost_Price__c;
                    ql.Quantity_On_Hand__c = p.Quantity_On_Hand__c;
                    ql.In_Stock_Price2__c = p.In_Stock_Pricing__c;
             	
                 }
                 if(Trigger.isUpdate){
                                     
                    ql.Cost_Price__c = p.Cost_Price__c;
                    ql.Quantity_On_Hand__c = p.Quantity_On_Hand__c;
                    ql.In_Stock_Price2__c = p.In_Stock_Pricing__c;
                     
                 }
             }
             
       }
    }
    update pList;
  
}
Bhanu MaheshBhanu Mahesh
Hi Masie Maseli;

I assume you want the field values from product sould be displayed on quote line items.
if yes, try below code
trigger UpdateFormulaFields on QuoteLineItem (before insert,before update) {
	set<id> PIds = new set<id>();
	Map<Id,Product2> mapIdWithProduct = new Map<Id,Product2>();
	for(QuoteLineItem qli : Trigger.new){
		if(trigger.isInsert){
			if(qli.Product2Id != null){
				PIds.add(qli.Product2Id);
			}
		}
		if(trigger.isUpdate){
			if(qli.Product2Id != trigger.oldMap.get(qli.Id).Product2Id){
				PIds.add(qli.Product2Id);
			}
		}
	}
	if(!PIds.isEmpty()){
		for(Product2 product :[select id, name , Cost_Price__c, Quantity_On_Hand__c, In_Stock_Pricing__c  from Product2 where Id IN : PIds]){
			mapIdWithProduct.put(product.Id,product);
		}
		for(QuoteLineItem qli : Trigger.new){
			if(qli.Product2Id != null && mapIdWithProduct != null && mapIdWithProduct.get(qli.Product2Id) != null){
				Product2 product = mapIdWithProduct.get(qli.Product2Id);
				ql.Cost_Price__c = product.Cost_Price__c;
				ql.Quantity_On_Hand__c = product.Quantity_On_Hand__c;
				ql.In_Stock_Price2__c = product.In_Stock_Pricing__c;
			}
		}
	}
}

Mark this as "SOLVED" if your issue is resolved