10 Replies Latest reply on May 8, 2017 3:00 PM by Sanford Whiteman

    Pulling Salesforce Opportunity Value using email scripting

    Benjamin Ortiz

      Can anyone provide a basic example of pulling an opportunity field value as a token using email scripting?

        • Re: Pulling Salesforce Opportunity Value using email scripting
          Sanford Whiteman

          Opportunities are available in the Velocity context as OpportunityList. This is an ArrayList, or array of objects. Visualize it like this:

           

          [
             {
                "property1" : "apple",
                "property2" : 12
              },
             {
                "property1" : "pear",
                "property2" : 34
              }
          ]
          

           

          Reference an item in the array with square brackets.  OpportunityList[0] is the first item.

           

          Reference properties of an item using dot-notation or square brackets (brackets are safer): OpportunityList[0]["property2"] is the number 12.

           

          If you haven't already, you should check my posts at http://blog.teknkl.com/tag/velocity if you're going to dip into email scripting.

          1 of 1 people found this helpful
            • Re: Pulling Salesforce Opportunity Value using email scripting
              Benjamin Ortiz

              Thanks for the reply Sanford. Basically what I am trying to accomplish is if an Opportunity meets certain criteria, then send an email with an opportunity field as a token in that email.

               

              So IF Opportunity Stage = 'ABC' then send email with the Opportunity field 'DUE DATE' as a token. 

               

              What I am trying to wrap my head around is how the value is pulled from the 'DUE DATE' field when multiple Opportunities are associated with a single Contact in SFDC.

                • Re: Pulling Salesforce Opportunity Value using email scripting
                  Sanford Whiteman

                  Thanks for the reply Sanford. Basically what I am trying to accomplish is if an Opportunity meets certain criteria, then send an email with an opportunity field as a token in that email.

                   

                  So IF Opportunity Stage = 'ABC' then send email with the Opportunity field 'DUE DATE' as a token.

                   

                  What I am trying to wrap my head around is how the value is pulled from the 'DUE DATE' field when multiple Opportunities are associated with a single Contact in SFDC.

                  It's pulled from the DueDate property of the Opportunity with the stage you want (obviously if your field is actually all caps DUEDATE, use that spelling).

                   

                  So say this is how your OpportunityList looks:

                   

                    {
                      "Stage" : "pear",
                      "DueDate" : "2017-07-03"
                    },
                    {
                      "Stage" : "apple",
                      "DueDate" : "2017-07-09"
                    },
                    {
                      "Stage" : "orange",
                      "DueDate" : "2017-06-01"
                    },
                    {
                      "Stage" : "apple",
                      "DueDate" : "2017-05-09"
                    }
                  

                   

                  Then you'd first filter the list to get the Opportunities matching your stage, sorting by date in case there's more than one:

                   

                  #set( $filteredOppties = [] )
                  #foreach( $oppty in $sorter.sort($OpportunityList, "DueDate") )
                  #if( $oppty["Stage"] == "apple" )
                  #set( $tmp = $filteredOppties.add($oppty) )
                  #end
                  #end
                  

                   

                  Now

                   

                  ${filteredOppties}
                  

                   

                  is a list of opportunities with Stage "apple", ordered by DueDate ascending.

                   

                  And

                   

                  ${filteredOppties[0]}
                  

                   

                  is the first such opportunity.

                   

                  And

                   

                  ${filteredOppties[0]["DueDate"]}
                  

                   

                  is the DueDate field of the first opportunity.

                   

                  It's all quite linear (it's hard to write complex code in VTL).

                  2 of 2 people found this helpful