+ Start a Discussion
AntonyasLenAntonyasLen 

pre-populating a lookup-up on Opportunity Product from Product

Hi,

 

***********************************************************************************************************************

 

Background:

 

Every product has a delivery man (look up) to this product.

When the salesman will add an Opportunity Product he will have to add a delivery man to the Opportunity Product.

But i wanted to pre-populate the Develiry man with the Product2 value.

 

***********************************************************************************************************************

i was thinking that using trigger "Before insert" would help me but i was wrong.


Dear community do you have any idea?

SFDC-SDSFDC-SD

I guess if it there on Product already, you should be able to get it.

 

Can you please post  your before trigger that didn't work... it might give enough details to  resolve.

AntonyasLenAntonyasLen
trigger PrepopulateOpportunityProduct on OpportunityLineItem (before insert) {
    for(OpportunityLineItem opl : Trigger.new){
    	opl.Carrier__c = opl.PricebookEntry.Product2.Carrier_del__c;
    }
}

 

AntonyasLenAntonyasLen

Nobody can help me?

SFDC-SDSFDC-SD

Here you go... this should work for you.

 

trigger PrepopulateOpportunityProduct on OpportunityLineItem (before insert, before update) {

    Set<Id> pbeIds = new Set<Id>();

    for(OpportunityLineItem oli: trigger.new)
    {       
       if(!pbeIds.contains(oli.PricebookEntryId)) pbeIds.add(oli.PricebookEntryId);      
    }      

    List<PricebookEntry> pbe = new list<PricebookEntry>();
    pbe= [SELECT Id, PricebookEntry.Product2.Carrier_del__c FROM PricebookEntry where Id IN :pbeIds];

    for(OpportunityLineItem opl : Trigger.new){
        for (integer i=0; i<pbe.size(); i++)
        {
        if(opl.PricebookEntryId == pbe[i].Id)
           opl.Carrier__c = pbe[i].Product2.Carrier_del__c;
        }
    }
}

 

 

AntonyasLenAntonyasLen

The trigger set the carrier to the Opportunity Product but it doesn't suggest it (it's not pre-populated).

 

Also you put the "before update" but this override the carrier value if a user want to change the carrier.


I'm keeping working on it, thanks for trying to help me.

SFDC-SDSFDC-SD

I used before update to test the stuff I wrote, I did something similar on a different object.

 

Also, I don't understand where you want to pre-populate "carrier" field when adding Opportunity Product. 

 

 

AntonyasLenAntonyasLen

i want to pre-populate the field on the creation screen.