Implementation

  1. Open your JavaScript code activity.

  2. Copy the script offered in Example of a script into the JavaScript code window.

  3. In the Label field, enter the name of the script, i.e.

    <%= vars.deliveryId %>
    

  4. Close the JavaScript code activity.

  5. Save your workflow.

Details of the script

This section details the various parts of the script and their operating mode.

  • The first part of the script is a query. The queryDef command lets you recover from the NmsDelivery table the deliveries created by executing the targeting workflow and to sort them based on their estimated rate of opens, then the information from the delivery with the highest rate of opens is recovered.

    // query the database to find the winner (best open rate)
       var winner = xtk.queryDef.create(
         <queryDef schema="nms:delivery" operation="get">
           <select>
             <node expr="@id"/>
             <node expr="@label"/>
             <node expr="[@operation-id]"/>
           </select>
           <where>
             <condition expr={"@FCP=0 and [@workflow-id]= " + instance.id}/>
           </where>
           <orderBy>
             <node expr="[indicators/@estimatedRecipientOpenRatio]" sortDesc="true"/>
           </orderBy>
         </queryDef>).ExecuteQuery()
    
  • The delivery with the highest rate of opens is duplicated.

     // create a new delivery object and initialize it by doing a copy of
     // the winner delivery
    var delivery = nms.delivery.create()
    delivery.Duplicate("nms:delivery|" + winner.@id)
    
  • The label of the duplicated delivery is modified, and the word final is added to it.

    // append 'final' to the delivery label
    delivery.label = winner.@label + " final"
    
  • The delivery is copied into the campaign dashboard.

    // link the delivery to the operation to make sure it will be displayed in
    // the campaign dashboard. This attribute needs to be set manually here since
    // the Duplicate() method has reset it to its default value => 0
    delivery.operation_id = winner.@["operation-id"]
    delivery.workflow_id = winner.@["workflow-id"]
    
    // adjust some delivery parameters to make it compatible with the
    // "Prepare and start" option selected in the Delivery tab of this activity
    delivery.scheduling.validationMode = "manual"
    delivery.scheduling.delayed = 0
    
  • The delivery is saved in the database.

    // save the delivery in database
    delivery.save()
    
  • The unique identifier of the duplicated delivery is stored in the workflow variable.

    // store the new delivery Id in event variables
    vars.deliveryId = delivery.id