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
Emelie HammarbergEmelie Hammarberg 

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;
    }
}

Lokeswara ReddyLokeswara Reddy
Dear Emelie,

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
// 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 = 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);
		}
}

Please ignore syntax error in the code, if any, but the logic works very well.

Regards
Lokesh
rajat Maheshwari 6rajat Maheshwari 6

Hi Emilie,

@Lokeswara write fabulous code , but pls replace productMap.get(oli.Product2Id) to productMap.get(oli.Product2Id)​.Family;

 

Thanks

Emelie HammarbergEmelie Hammarberg

Hi, 
Thanks for your reply! I'm getting an error message: unexpected token: 'Map', what can I do to resolve? 

rajat Maheshwari 6rajat Maheshwari 6

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

 

Emelie HammarbergEmelie Hammarberg
Hi,
I still receive the error message, unexpected token: 'Map'..
 
Lokeswara ReddyLokeswara Reddy
There was typo, missing NEW in the query
 
Map<id,Product2> productMap = new Map<id,Product2>([select id,Family from Product2 where Id IN : Product2IdList]);

 
Lokeswara ReddyLokeswara Reddy
if you still see any errors, please paste your trigger code and point at what line it fails
rajat Maheshwari 6rajat Maheshwari 6

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

                   
 

Emelie HammarbergEmelie Hammarberg

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;
        }
}

}

Lokeswara ReddyLokeswara Reddy
Record should be saved inorder to see the data.
​I wonder how this is being validated before insert?
rajat Maheshwari 6rajat Maheshwari 6

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

 

 

Emelie HammarbergEmelie Hammarberg
Hi, 
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. 
Stéphane C.Stéphane C.
Hi,

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.