Manage seeds through JS

mustufam5967803

04-03-2019

As you aware, we don't have control on computed deliveries hence I am managing seeds through JS Script. The requirement I have is: There are two folders 'ABC-Seeds' and 'XYZ-Seeds', I need to target to complete folder of 'ABC-seeds' and 2 ids of 'XYZ-Seeds' folder, Email IDs are: 'email1' and 'email2'. The JavaScript I am using is as follows:

// 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]"/>

        <node expr="[@workflow-id]"/>

        <node expr="[scheduling/@contactDate]"/>

          <node expr="[output/seedList/where/condition/@expr]"/>

      </select>

      <where>

        <condition expr={"@FCP=0 and @internalName = 'Em123' and @isModel=0"}/>

      </where>

      <orderBy>

        <node expr="[indicators/@estimatedRecipientOpenRatio]" sortDesc="true"/>

      </orderBy>

    </queryDef>).ExecuteQuery()

  // 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)

  // append 'winner' to the delivery label

  delivery.label = winner.@label + " Test"

  delivery.operation_id = winner.@["operation-id"]

  delivery.workflow_id = winner.@["workflow-id"]

  delivery.output.seedList.humanCond = "Query: folder equal to ABC-Seeds or (folder equal to XYZ-Seeds and email of recipient is included in 'email1,email2')"

  delivery.output.seedList.where.displayFilter = "folder equal to ABC-Seeds or (folder equal to XYZ-Seeds and email of recipient is included in 'email1,email2')"

  delivery.output.seedList.where.condition.expr= ("[@folder-id] = 9787930 or [@folder-id] = 882531 and [custom_nms_recipient/@email] IN ('email2' , 'email2')")

  // save the delivery in database

  delivery.save()

  // store the new delivery Id in event variables

  vars.deliveryId = delivery.id

    // Print Log Details

    logInfo(vars.deliveryId)

    logInfo(delivery.label)

    logInfo(winner.output.seedList.where.condition.@expr)

The challanges here I am facing is:

1. The Filtering Condition is taking up as foriegn key of folder id(Below is the screenshot for reference)

2. Everytime it is appending the condition and not replacing the condition with the new computed delivery in Filerting Condition

1704386_pastedImage_1.png

Please have a look on this and Prompt response will be appreciated.

Accepted Solutions (1)

Accepted Solutions (1)

geoffreyl240454

08-04-2019

Hi mustufam59678037​,

I think that the conditions were not in the winner delivery.

The conditions must be there in the original delivery (winner delivery) when you create the duplicated delivery:

- If yes, the JS code will modifying the values of the duplicated delivery conditions so the provided code will be good.

- If not, the JS code need to be modified in order to create the conditions in the duplicated delivery and not just modifying them.

Many thanks,

Geoffrey.

Answers (8)

Answers (8)

geoffreyl240454

05-04-2019

Hello mustufam59678037​,

If I have this conditions on my original delivery (winner delivery in your case):

1725813_pastedImage_0.png

I can replace the conditions with this JS code:

delivery.output.seedList.where[0].condition[0].boolOperator = 'AND';

delivery.output.seedList.where[0].condition[0].expr = "[custom_nms_recipient/@email] IN ('xyz123@email.com', 'xyz123@ymail.com')";

delivery.output.seedList.where[0].condition[0].condition[0].boolOperator = 'AND';

delivery.output.seedList.where[0].condition[0].condition[0].condition[0].boolOperator = 'OR';

delivery.output.seedList.where[0].condition[0].condition[0].condition[0].expr = '[folder/@id]=9787930';

delivery.output.seedList.where[0].condition[0].condition[0].condition[1].expr = '[folder/@id]=882531';

And my duplicated delivery contains the new conditions:

1725821_pastedImage_2.png

I hope that helps.

Thanks and Regards,

Geoffrey.

mustufam5967803

08-04-2019

Hi geoffreyl24045476,

Thank you for your quick response.

Tested the condition by removing "[0]" from  "where" as well as from "condition", however getting an error saying index out of range.

1728714_pastedImage_4.png

mustufam5967803

08-04-2019

Hi Geoffrey

geoffreyl24045476, Thank you for your response on this.

I tried the above provided Script, however getting an error saying delivery.output.seedList.where[0] is undefined, below is the screenshot for reference getting in Display Logs.

1728709_pastedImage_6.png

Here is the complete code I am trying with:

// query the database to find the winner (best open rate)

  var winner = xtk.queryDef.create(

    <queryDef schema="nms:delivery" operation="select">

      <select>

        <node expr="@id"/>

        <node expr="@label"/>

        <node expr="[@operation-id]"/>

        <node expr="[@workflow-id]"/>

        <node expr="[scheduling/@contactDate]"/>

        <node expr="[output/seedList/where/condition/@expr]"/>

      </select>

      <where>

        <condition expr={"@FCP=0 and @internalName = 'Em123' and @isModel=0"}/>

      </where>

      <orderBy>

        <node expr="[indicators/@estimatedRecipientOpenRatio]" sortDesc="true"/>

      </orderBy>

    </queryDef>).ExecuteQuery()

  // 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)

  // append 'winner' to the delivery label

  delivery.label = winner.@label + " Test"

  delivery.operation_id = winner.@["operation-id"]

  delivery.workflow_id = winner.@["workflow-id"]

  delivery.output.seedList.humanCond = "Query: folder equal to ABC-Seeds or (folder equal to XYZ-Seeds and email of recipient is included in 'email1,email2')"

  delivery.output.seedList.where.displayFilter = "folder equal to ABC-Seeds or (folder equal to XYZ-Seeds and email of recipient is included in 'email1,email2')"

  delivery.output.seedList.where[0].condition[0].boolOperator = 'AND'; 

  delivery.output.seedList.where[0].condition[0].expr = "[custom_nms_recipient/@email] IN ('xyz123@email.com', 'xyz123@ymail.com')"; 

  delivery.output.seedList.where[0].condition[0].condition[0].boolOperator = 'AND'; 

  delivery.output.seedList.where[0].condition[0].condition[0].condition[0].boolOperator = 'OR'; 

  delivery.output.seedList.where[0].condition[0].condition[0].condition[0].expr = '[folder/@id]=9787930'; 

  delivery.output.seedList.where[0].condition[0].condition[0].condition[1].expr = '[folder/@id]=882531';

  // save the delivery in database

  delivery.save()

  // store the new delivery Id in event variables

  vars.deliveryId = delivery.id

  

// Print Log Details@

  logInfo(vars.deliveryId)

  logInfo(delivery.label)

  logInfo(winner.output.seedList.where.condition.@expr)

Regards,

Mustufa