+ Start a Discussion
carmilyn.c martincarmilyn.c martin 

Apex trigger getting values from a Lookup Field


We have 2 Fields from Custom Object Letter Request
1. Product_esi__c  - picklist Field
2. Item_esi__c - picklist field

We need to get their values from Call Sample Object and the field that is involved is a lookup field to Product Object.
2. Product_vod__c  - lookup

We have a trigger that creates Letter Request Record where these fields "Product_esi__c" and "Item_esi__c" from Letter Request Object will be populated with value from "Product_vod__c" field of Call Sample Object

- Product_esi__c = this will get its value from "Product_vod__c"'s Parent Product Name Field (Need to know how I can get the Name of the Parent Product of this child record "Product_vod__c".)
- Item_esi__c = this will get its value from "Product_vod__c"'s Name (with the current trigger it is getting the Salesforce ID of the Product instead of the name of the Product)


Current TRIGGER: 

List<Letter_Request_esi__c> insertLetterRequestList = New List<Letter_Request_esi__c>();
List<Letter_Request_esi__c> deleteLetterRequestList = New List<Letter_Request_esi__c>();
Set<Id> delCallSampleIds = New Set<Id>();
if(Trigger.IsInsert)
{
    system.debug('***********Enter if IsInsert****************');
    for(Call2_Sample_vod__c cs : trigger.New)
    {
        system.debug('***********iterating all call Sample records****************');
        if(cs.Product_Type_esi__c != null && cs.Product_Type_esi__c == 'Letter')
        {
            system.debug('***********Enter if product is null and prodcut type is Letter****************');
            Letter_Request_esi__c letReq = New Letter_Request_esi__c(Call__c = cs.Call2_Vod__c, Call_Sample__c = cs.id , Account__c = cs.Account_vod__c, Item_esi__c = cs.Product_vod__c , Product_Type__c = 'Letter Request' );
            insertLetterRequestList.add(letReq);
        }
        else if(cs.Product_Type_esi__c != null && cs.Product_Type_esi__c == 'Promotional')
        {
            system.debug('***********Enter if product is null and prodcut type is Promotional Letter****************');
            Letter_Request_esi__c letReq = New Letter_Request_esi__c(Call__c = cs.Call2_Vod__c, Call_Sample__c = cs.id , Account__c = cs.Account_vod__c, Item_esi__c = cs.Product_vod__c , Product_Type__c = 'Promotional Item');
            insertLetterRequestList.add(letReq);



WHAT We have before was: This didn't work


List<Letter_Request_esi__c> insertLetterRequestList = New List<Letter_Request_esi__c>();
List<Letter_Request_esi__c> deleteLetterRequestList = New List<Letter_Request_esi__c>();
Set<Id> delCallSampleIds = New Set<Id>();
if(Trigger.IsInsert)
{
    system.debug('***********Enter if IsInsert****************');
    for(Call2_Sample_vod__c cs : trigger.New)
    {
        system.debug('***********iterating all call Sample records****************');
        if(cs.Product_Type_esi__c != null && cs.Product_Type_esi__c == 'Letter')
        {
            system.debug('***********Enter if product is null and prodcut type is Letter****************');
            Letter_Request_esi__c letReq = New Letter_Request_esi__c(Call__c = cs.Call2_Vod__c, Call_Sample__c = cs.id , Account__c = cs.Account_vod__c, Item_esi__c = cs.Product_vod__r.Name , Product_Type__c = 'Letter Request' , Product_esi__c = cs.Product_vod__r.Parent_Product_vod__r.Name);
            insertLetterRequestList.add(letReq);
        }
        else if(cs.Product_Type_esi__c != null && cs.Product_Type_esi__c == 'Promotional')
        {
            system.debug('***********Enter if product is null and prodcut type is Promotional Letter****************');
            Letter_Request_esi__c letReq = New Letter_Request_esi__c(Call__c = cs.Call2_Vod__c, Call_Sample__c = cs.id , Account__c = cs.Account_vod__c, Item_esi__c = cs.Product_vod__r.Name , Product_Type__c = 'Promotional Item' , Product_esi__c = cs.Product_vod__r.Parent_Product_vod__r.Name);
            insertLetterRequestList.add(letReq);
        }
 
ManojjenaManojjena
Hi carmilyn,

Still your post is not clear enough to give solution for this .

What I under stood you have two objects Letter_Request_esi__c and Call2_Sample_vod__c. You have written trigger in Call2_Sample_vod__c  and Letter_Request_esi__c is the parent of Call2_Sample_vod__c  .  Please explain your requirment bit clearly so that we can give solution to you .
Thanks
Mnaoj

 
carmilyn.c martincarmilyn.c martin
We have those 2 fields from letter request that we need to be auto populated via trigger above. However with the current trigger we have the field Item_esi__c is getting the salesforce id of the value in Product_vod__c  field which is a lookup field from Call Sample object, instead of the Name.

1. How can i insert the NAME of the Product_vod__c field value  (this is the lookup field from call sample object that looks up to Product object) instead of the salesforce id, using and modifying the current trigger above, in Item_esi__c field of Letter Request.
2. How can I insert the name of the Parent product of the Product_vod__c field value (this is the lookup field from call sample object that looks up to Product object) in the Product_esi__c field of Letter request object. What should i add on the above current trigger. 

I thought that the trigger we had before will work but it didn't. So i need assistance to modify the current trigger to get the right values just by simply using or referencing the product_vod__c field from call sample object.