SOLVED

Re: Velocity Opportunity Sort Token

Go to solution
Barry_Moffat2
Level 2

We have some emails where we use script tokens to sort the opportunities on a person's record by a datetime field and output a data value from the opportunity with the newest datetime in that specific field. However, we're finding that the tokens periodically fail, resulting in the email soft bouncing (the sender name and email address are two of the fields output by these tokens). 

However, when I look at the records on which they fail, the required sort fields are all present on the opportunity at the point that the email sends, so I'm at a loss as to why they fail. 

 

Has anyone encountered this? Attached example of the token script: 

 

#if( !$OpportunityList.isEmpty() )



#set( $OpportunityListRecentFirst = $sorter.sort($OpportunityList,["vu_vurpurpose:desc","vu_vurattached:desc"]) )
${OpportunityListRecentFirst[0].rss_hbcemail}
#end

 

 

All fields are checked on the token sidebar. The first datapoint is a string with only two possible values, and the second is the datetime field used to sort by most recent value. 

 

We use a Microsoft Dynamics CRM in case that's pertinent, with the dynamics stakeholder grid deployed so that multiple people can be associated with an individual opportunity

 

Any insights or suggestions would be much appreciated.   

 

 

1 ACCEPTED SOLUTION
SanfordWhiteman
Level 10 - Community Moderator

Barry and I deciphered this offline, it was actually a question of unexpected null values (which had been thought impossible due to CRM-side rules when creating Opportunities).

View solution in original post

15 REPLIES 15
SanfordWhiteman
Level 10 - Community Moderator

Barry and I deciphered this offline, it was actually a question of unexpected null values (which had been thought impossible due to CRM-side rules when creating Opportunities).

SanfordWhiteman
Level 10 - Community Moderator

However, we're finding that the tokens periodically fail, resulting in the email soft bouncing (the sender name and email address are two of the fields output by these tokens). 

Are you sure none of the fields (neither the fields you're sorting on, nor the field used for output) are null-able?

 

How are you verifying that "required sort fields are all present on the opportunity at the point that the email sends"? You'd have to send a real email (not sample) with the full $OpportunityList in the body (of course hard-coding the From:/Reply-To: info in this case to avoid the failure). What's the output then?

Barry_Moffat2
Level 2

They are null-able. I was verifying before by looking at the individual's activity log and reviewing the Update Opportunity activity that triggered the campaign (which has a log of all fields as they were at that moment I believe). 

 

I did what you suggested and dropped the full Opportunity List into an email (nice trick by the way) and all the opportunities in the list have values in those fields. It also outputted some other fields that we use in the email using similar script tokens (presumably because they were still in the text version?) 

SanfordWhiteman
Level 10 - Community Moderator

I did what you suggested and dropped the full Opportunity List into an email (nice trick by the way) and all the opportunities in the list have values in those fields. It also outputted some other fields that we use in the email using similar script tokens (presumably because they were still in the text version?) 


Any fields checked off in the tree will be included when you dump the whole list object.

 

So what output do you get — in the body here, so it doesn't stop the send — when you drop the logic to get the property off the most recent Oppty?

Barry_Moffat2
Level 2

Ah sorry, Sanford, I misunderstood you before and simply made a script token with a single phrase- $OpportunityList, i.e. no logic to sort by most recent. This is what outputted everything I mentioned previously (in ascending order.) 

 

What I think I now understand you actually suggested before was to output the entire sorted opportunity list into the email. Forgive my lack of knowledge, but how do I do that?  I tried this and it output nothing so guess that is wrong.

#if( !$OpportunityList.isEmpty() )



#set( $OpportunityListRecentFirst = $sorter.sort($OpportunityList,["vu_vurpurpose:desc","vu_opportunitycreated","vu_vurattached:desc"]) )
${OpportunityListRecentFirst}
#end

 

SanfordWhiteman
Level 10 - Community Moderator

Ah sorry, Sanford, I misunderstood you before and simply made a script token with a single phrase- $OpportunityList, i.e. no logic to sort by most recent. This is what outputted everything I mentioned previously (in ascending order.) 

That was what I meant to start out, so you were following me!

 

Now you have a good comparison: the unsorted list, the sorting does not.  

 

But you reported something strange: if #set-in$OpportunityListRecentFirst fails (which would happen, for a primary example, if any of the values were null) then outputting $OpportunityListRecentFirst should print just the code (the literal String "OpportunityListRecentFirst"), not an empty String.

You're going to need to show me the values of these fields from the $OpportunityList dump, something is off here.

Barry_Moffat2
Level 2

Thanks Sanford. If you don't mind, I'll send you a DM  with the full dump as it contains customer information. Reviewing it again though, the specific opportunity that triggered the email (and should have had the most recent datetime field for the sort) is missing from the list. That doesn't really explain why it didn't just output the most recent one actually present on the list though. 

 

I've encountered opportunity to person  associations breaking very occasionally on one of our other instances, but that instance has a custom built API-driven data sync, rather than the native CRM bi-directional sync we use in this case. Up until now, have never encountered oppty associations failing in this one.   

 

Thanks for your help, 

SanfordWhiteman
Level 10 - Community Moderator

In your last snippet, you're trying to sort on the property vu_opportunitycreated, but that doesn't appear at all in the $OpportunityList dump you PM'd me.

 

However, a completely absent property would be ignored from the sort (as opposed to if it were sometimes null, which definitely is not ignored). So that wouldn't on its own explain the behavior, but it is confusing!

 

To round up the findings: you're still seeing no output when you try to output the entire sorted list, $OpportunityListRecentFirst, but in that same email, $OpportunityList dumps as expected (and is the same as what you PM'd)?

 

 

Barry_Moffat2
Level 2

So sorry Sanford, that last snippet was a red-herring. I had been messing with a test token to see if other sort fields made a difference and I added that vu_opportunitycreated field - forgot to remove it again before posting. That said, now that I've removed that extra sort field, it's back to the below and still outputs nothing for that specific person, while the simple $opportunitylist token does. The one below does output the sorted list for other people though, so is the issue likely to be related to the opportunity role/association breaking at least intermittently on some person records , rather than a failure of the velocity script itself?     

#if( !$OpportunityList.isEmpty() )



#set( $OpportunityListRecentFirst = $sorter.sort($OpportunityList,["vu_vurpurpose:desc","vu_vurattached:desc"]) )
$OpportunityListRecentFirst
#end

  

Thanks again, Barry

 

 

SanfordWhiteman
Level 10 - Community Moderator

likely to be related to the opportunity role/association breaking at least intermittently on some person records , rather than a failure of the velocity script itself?     

I don't think that's it. You can see the list exists, so it is otherwise accessible in Velocity. Something is up with the specific list for that lead.  Please PM me the output of this code for that lead:

 

#foreach( $oppty in $OpportunityList )
#foreach( $entry in $oppty.entrySet() )
$entry.getKey() = $entry.getValue().class $entry.getValue()
#end
#end

 

Barry_Moffat2
Level 2

Thanks Sanford, shot you a PM with the output.

SanfordWhiteman
Level 10 - Community Moderator

Please highlight the code using the syntax highlighter so it's readable. Then we'll continue.

syntax_vtl.png

Barry_Moffat2
Level 2
#if( !$OpportunityList.isEmpty() )



#set( $OpportunityListRecentFirst = $sorter.sort($OpportunityList,["vu_vurpurpose:desc","vu_vurattached:desc"]) )
${OpportunityListRecentFirst[0].rss_hbcemail}
#end

Sorry, my bad. Thanks for taking a look, Sanford.

SanfordWhiteman
Level 10 - Community Moderator

Could you edit your initial post instead? B/c otherwise this keeps happening when people look at old posts.

Barry_Moffat2
Level 2

Yes, done.