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
CodeBeeCodeBee 

Can we set id to lookup field via trigger ?

Good day, 

 

Could we set the lookup field with id ? for instance, i  have a Opportunity product, which have lookup relationship to product, when i save oppty product, it getting value of product's myStoreID field and save it to Opportunity product lookup field.

 

Thanks in advance !

Shashikant SharmaShashikant Sharma

Yes definitly you can do this with trigger. You need to write a before inser trigger on opportunity product. If you have any problem I can help you in wrtting it, just provide me a bit more information about your requirement. You can check my blog also for writting trigger.

CodeBeeCodeBee

Thanks !

 

When i try following for before insert trigger, i hit error, what wrong with trigger.oldmap.keyset() ?

 

List<OpportunityLineItem> tempOpportunityLineItemList =[	SELECT id, pricebookentry.product2id 
																FROM OpportunityLineItem 
																WHERE id IN :trigger.oldmap.keyset()

 

Shashikant SharmaShashikant Sharma

In before triggers trigger.oldmap and trigger.old are not available as it is before insert and how would be there a old value.

And another thing is that in before triggers you will not get ID as Ids are generated after insert. 

let me ask if any issue in above explaination.

CodeBeeCodeBee

Hi Shashikant Sharma, 

 

What i need to accomplish is , after i inserted a record to OpportunityLineItem , i also need to find out a field that store an id - storeId from other objectA, by getting that value from the store id field , i will need to query it based on the value i get from another OBjectB, so how can i accomplish this ?

Shashikant SharmaShashikant Sharma

What is the raltionship between OpportunityLineItem , ObjectA and ObjectB let me know, you can see this post

 

http://boards.developerforce.com/t5/Apex-Code-Development/Trigger-not-updating-lookup-field/td-p/306915 please see is it similar to your requirement. If it is not just provide me some more information , relationship , field APIName , Object APIName . I can try to write trigger for you.

CodeBeeCodeBee

Ignore above mentioned object relationship , i get myself confuse and what i need to do is to get productid value from pricebookentry.

 

Here is what i try to do in "After Insert " trigger, apparently OpportunityLineItem not allow to save it own object. not sure how can i do this with before Insert trigger.

 

trigger myTrigger on (after insert){

 

for(OpportunityLineItem opptyLineItem : trigger.new){

opptyLineItem.myProduct__c = opptyLineItem.pricebookentry.product2id;

}

 

}

 

after i get the product2Id value, i'll need to update the lookup field call "MyProduct__c" in OpportunityLineItem itself.

Shashikant SharmaShashikant Sharma

try this I hope it will work for you

trigger myTrigger on OpportunityLineItem  (after insert)
{

Set<id> setPriceBookEntryid = new Set<ID>();
for(OpportunityLineItem opptyLineItem : trigger.new)
    {
    setPriceBookEntryid.add(opptyLineItem.pricebookentryId);
    
    } 

MAP<ID , pricebookentry> mapPriceBookEntry = new MAP<ID , pricebookentry>([Select product2id from pricebookentry where id in: setPriceBookEntryid]);
for(OpportunityLineItem opptyLineItem : trigger.new)
    {
    
        if(opptyLineItem.pricebookentryId != null && mapPriceBookEntry.containsKey(opptyLineItem.pricebookentryId))
        {
            opptyLineItem.myProduct__c = mapPriceBookEntry.get(opptyLineItem.pricebookentryId).product2id;
        }
    
    }
 
}

 you were trying to access related object field from trigger.new list's object which will always give null value. Let me know if any issue in above trigger.