SOLVED

How to lookup a value in one SFDC field (like Vlookup)

Go to solution
michaelstancil
Level 3

Re: How to lookup a value in one SFDC field (like Vlookup)

0th element? 

 

I have a few other tokens that come after that reference the created merged list, such as this:

${mergedOpptyAndAVMList.get(0).ITEMFROMAVMLIST__c}
SanfordWhiteman
Level 10 - Community Moderator

Re: How to lookup a value in one SFDC field (like Vlookup)

Well you can’t have those without checking if the list is empty. Trying to referencing the first/[0]th element of an empty list is always a fatal error. Just like referencing the second/[1]th element of a list with only one element.

michaelstancil
Level 3

Re: How to lookup a value in one SFDC field (like Vlookup)

Got it, I've updated the first token that calls this to as follows:

#if( ! $display.alt($mergedOpptyAndAVMList,"").isEmpty() )
  #foreach(ITEMFROMAVM in mergedOpptyAndAVMList)
    #set($AVMCurrentValue=${mergedOpptyAndAVMList.get(0).ITEMFROMAVM})

I get this error:

Screen Shot 2021-12-16 at 11.05.02 PM.png 

SanfordWhiteman
Level 10 - Community Moderator

Re: How to lookup a value in one SFDC field (like Vlookup)

Large number of errors there. Typos and logic as a whole.

 

This will at least compile (assuming it’s only the top of your token and you’re #ending that #if and #foreach later on):

#if( !$display.alt($mergedOpptyAndAVMList,[]).isEmpty() )
  #foreach( $avm in $mergedOpptyAndAVMList )
    ## $avm is set to the current item in $mergedOpptyAndAVMList

 

But I don’t get what you’re trying to do with the list. As it iterates, $avm is set to each object in the list in turn. You should never, ever be referencing object [0].

michaelstancil
Level 3

Re: How to lookup a value in one SFDC field (like Vlookup)

This might be where there is some coding teaching here, what is object[0]? Sorry, I'm trying based on what languages and logic structures I know, but I'm sure it's not the prettiest, apologies for that and thank you again for helping.

 

As far as what I'm doing, maybe it will help if I user story it. Now that we have the mergedOpptyAndAVMList, there are three different values based on what the list was merged on that I want to display in the email. I had created the three tokens to pull each, and I was doing each as mergedOpptyAndAVMList.Item1, mergedOpptyAndAVMList.Item2, and mergedOpptyAndAVMList.Item3, where the .item is what the item was called from the AVM list.

 

Does that make more sense?

Jo_Pitts1
Level 10 - Community Advisor

Re: How to lookup a value in one SFDC field (like Vlookup)

@michaelstancil ,

I think there is an understanding gap around how the foreach loop works:

 

#if( ! $display.alt($mergedOpptyAndAVMList,"").isEmpty() )
  #foreach(ITEMFROMAVM in mergedOpptyAndAVMList)
    #set($AVMCurrentValue=${mergedOpptyAndAVMList.get(0).ITEMFROMAVM})

 

  1. Your first line is the empty guard... that makes sure we don't enter the loop if the list is empty.. that's hunky dory!
  2. The second line is your foreach loop.  It works by iterating the entire list.  On each iteration, it assigns the current element in the list to the named variable.  In your example, each iteration assigns an item from mergedOpptyAndAVMList to ITEMFROMAVM.  If you have (for example) 6 items in your list, the foreach loop will be executed 6 times, and each time ITEMFROMAVM will be set to the appropriate value in the list.
  3. it feels like your third line is you attempting to do what the second line is doing as of right.  You don't need this line.

By object[0], @SanfordWhiteman is referring to the generic notion of the zero indexed item of a list (the zero indexed item is the FIRST item).  If you try to access the [0] item in an empty list, it errors as there is no first item (as it is empty).  Substitute 'object' for the name of the list you are using (i.e. mergedOpptyAndAVMList) to make it specific to your use case.

 

I have to profess, I don't understand your use case as you've explained it.  Sorry.

 

Cheers

Jo

 

 

 

 

michaelstancil
Level 3

Re: How to lookup a value in one SFDC field (like Vlookup)

@Jo_Pitts1  @SanfordWhiteman 

 

So I asked our engineering team for some assistance, and we did some debugging. We're able to go into the Opportunity Loop, but when we loop through the AVM, nothing happens. So in the code below, we get pineapple, but not oranges. Is there something basic I'm missing? I've checked ALL of the values within the AVM custom object in the script token.

 

 

#set( $HackermanValue1= "Apple")
#if( !$OpportunityList.isEmpty() && !$AVM_Values__cList.isEmpty() )
  #foreach( $oppty in $OpportunityList )
   #set( $HackermanValue1 = "pineapple")   
    #foreach( $avm in $AVM_Values__cList )
     #set( $HackermanValue1 = "oranges")
    #end      
  #end
#end
${HackermanValue1}

 

 

Am I missing something obvious? I can see values within AVM dataset, so I know they aren't empty.

SanfordWhiteman
Level 10 - Community Moderator

Re: How to lookup a value in one SFDC field (like Vlookup)


Am I missing something obvious? I can see values within AM dataset, so I know they aren't empty.

What do you see when you simply output

 ${AVM_Values__cList}

 ?

 

That needs to be part of your testing — dumping the raw data.

michaelstancil
Level 3

Re: How to lookup a value in one SFDC field (like Vlookup)

@SanfordWhiteman The result in the email is:

 

${AVM_Values__cList} pineapple

 

 

SanfordWhiteman
Level 10 - Community Moderator

Re: How to lookup a value in one SFDC field (like Vlookup)

In that case that isn't the right name for the list. How did you determine it was the right name?