You need to sign in to do that
Don't have an account?
pre populate opportunity line item
Hi,
Trying to create a trigger that will populate a field in the opportunity line items before save, however my trigger only populates the opportunity line item once saved. Both fields in the trigger are in the opportunity line item object, is that the issue?
The reason to why I want to have the field populated before save is to be able to have a field dependency based on product information. If I could use "Product/PricebookEntry" as controlling field for my custom field Services__c that would solve my issue. But as that's not possible, I want to clone the product value to a picklist before save, and use that picklist for the field dependency.
The oli.Product_Family__c field is a formula field referencing a field in the related product (formula field: TEXT (PricebookEntry.Product2.Family) ).
trigger OLIAUTOFILL on OpportunityLineItem (before insert, before update) {
for(OpportunityLineItem oli: Trigger.new)
{
// If you want this for update operation, then check update as well in the below if statement
if(trigger.isBefore && trigger.isInsert)
oli.Test_Type__c = oli.Product_Family__c;
}
}
Before record creation, formula field will always be blank, instead of forumla field you may probably have this logic on the trigger itself.
Step#1 - Get list of all Product2Ids Step#2 - Query for Family from Proudct2 object
Step#3 - Prepopulate Family
Please ignore syntax error in the code, if any, but the logic works very well.
Regards
Lokesh
Hi Emilie,
@Lokeswara write fabulous code , but pls replace productMap.get(oli.Product2Id) to productMap.get(oli.Product2Id).Family;
Thanks
Hi,
Thanks for your reply! I'm getting an error message: unexpected token: 'Map', what can I do to resolve?
Hi Emelie,
please use this to resolve the error
Map<id,Product2> productMap = Map<id,Product2>([select id,Family from Product2 where Id IN : Product2IdList]);
Here I am using colon after IN, just for your reference, okie
Thanks
I still receive the error message, unexpected token: 'Map'..
Hi Emelie,
Please follow this code and then let me know the same - :
Map<id,Product2> productMap = new Map<id,Product2>([select id,Family from Product2 where Id INProduct2IdList]);
Thanks
Thanks for helping, really appreciate it :-)
The trigger is now working, however not updating the opportunity line item before insert - still only updating after save.
trigger OLIAUTOFILL2 on OpportunityLineItem (before insert, before update) {
// Step#1 Get list of all product2 Ids
Set<id> Product2IdList = new Set<id>();
for(OpportunityLineItem oli: Trigger.new){
Product2IdList.add(oli.Product2Id);
}
//Step#2 - Query for Family from Proudct2 object
Map<id,Product2> productMap = new Map<id,Product2>([select id,Family from Product2 where Id IN : Product2IdList]);
//Step#3 - Prepopulate Family
for(OpportunityLineItem oli: Trigger.new){
if(trigger.isBefore && trigger.isInsert)
if(productMap.containsKey(oli.Product2Id)){
oli.Test_Type__c = productMap.get(oli.Product2Id).Family;
}
}
}
I wonder how this is being validated before insert?
Hi Emelie,
Trigger needs an action to fire, Either you use before trigger or after trigger, You need to click save button as a action to fire trigger .
I wondered, To prepopulate the value on opportunity line Item from product -> family, you can give a try with URL Hacking approach .
Make new custom button and utilize url hacking over there to fetch product family field at time when u create opportunity line Item.
please follow the below link and let me know the same.
http://raydehler.com/cloud/clod/salesforce-url-hacking-to-prepopulate-fields-on-a-standard-page-layout.html
Thanks
I tried the URL hacking, but it seems hard to apply on the opportunity line item as it's a two step process to add line items.
I have the same request.
Rajat, you say that trigger needs an action to fire. When I select a product on the "Add Products" page (before the "Edit Selected Products" page) and I push the next button that can't be considered as an action to populate a field in the "Edit Selected Products" page?
Thank you.