Expand my Community achievements bar.

SOLVED

Access to targetData and recipients from Delivery Script

Avatar

Level 4

Hi,

I'm working on a workflow and I would like to be able to read the 'Target Extension' (targetData object) of the workflow on a script that I want to include on the 'Script' tab of a Delivery in the workflow (see img below) Also I would need to have more details about the recipient list fed from the workflow into the delivery.

1286112_pastedImage_1.png

I can use with no problem the objects targetData and recipient on the body of the delivery itself, but not on this script tab.

Can anyone give a hand please?

Thanks in advance

Guillermo

1 Accepted Solution

Avatar

Correct answer by
Level 10

Olà Guillermo,

As with a JS activity in a workflow, it is not possible to get directly the xpath (storage path) of a Query activity in the Delivery script tab.
So you can't use directly targetData object, which is undefined.

Good:

logInfo("[WKFxxx] delivery.internalName = " + delivery.internalName)

Wrong:

logInfo("[WKFxxx] delivery.targetData.id= " + delivery.targetData.id)

Hence, you must do a queryDef inside your Delivery script tab, as below (of course, adapt it with your own need, "query" of temp:query beeing the internal name by default of a query activity; and I have added firstName column in the Additional data section of the Query):

var queryTemp = xtk.queryDef.create(

    <queryDef schema="temp:query" operation="select">

      <select>

        <node expr="@firstName"/>

      </select>

      <where>

      </where>

    </queryDef>)

try

{

  var res = queryTemp.ExecuteQuery();

  logInfo("[WKF345] get the values of temp:query after a Query task execution whose name is query");

  logInfo("[WKF345] Count of values = " + res.length());

}

catch(e)

{

  logWarning("[WKF345] Error in getting the values of temp:query after a Query task execution whose name is query");

  logError("[WKF345] Error number: " + e); // or use logWarning if the treatment must go on despite this error

}

for each(var elt in res)

  logInfo("[WKF345] elt.firstName = " + elt.@firstName);

Depending of what you have to do, it is easier to use the <%= targetData.xxxx %> in the delivery body, or you can also do the global queryDef directly in the Delivery script tab (replacing the Query activity box), but to my mind it is 

less easier to read and maintain, and duplicate from a workflow to another for other needs; but it is a personal choice of course.

Regards
Jean-Serge

View solution in original post

4 Replies

Avatar

Correct answer by
Level 10

Olà Guillermo,

As with a JS activity in a workflow, it is not possible to get directly the xpath (storage path) of a Query activity in the Delivery script tab.
So you can't use directly targetData object, which is undefined.

Good:

logInfo("[WKFxxx] delivery.internalName = " + delivery.internalName)

Wrong:

logInfo("[WKFxxx] delivery.targetData.id= " + delivery.targetData.id)

Hence, you must do a queryDef inside your Delivery script tab, as below (of course, adapt it with your own need, "query" of temp:query beeing the internal name by default of a query activity; and I have added firstName column in the Additional data section of the Query):

var queryTemp = xtk.queryDef.create(

    <queryDef schema="temp:query" operation="select">

      <select>

        <node expr="@firstName"/>

      </select>

      <where>

      </where>

    </queryDef>)

try

{

  var res = queryTemp.ExecuteQuery();

  logInfo("[WKF345] get the values of temp:query after a Query task execution whose name is query");

  logInfo("[WKF345] Count of values = " + res.length());

}

catch(e)

{

  logWarning("[WKF345] Error in getting the values of temp:query after a Query task execution whose name is query");

  logError("[WKF345] Error number: " + e); // or use logWarning if the treatment must go on despite this error

}

for each(var elt in res)

  logInfo("[WKF345] elt.firstName = " + elt.@firstName);

Depending of what you have to do, it is easier to use the <%= targetData.xxxx %> in the delivery body, or you can also do the global queryDef directly in the Delivery script tab (replacing the Query activity box), but to my mind it is 

less easier to read and maintain, and duplicate from a workflow to another for other needs; but it is a personal choice of course.

Regards
Jean-Serge

Avatar

Level 4

Thanks very much for your answer Jean-Serge. I'll try your solution as soon as I can.

To give you more information about why using the script tab instead of the body, apart from what you say, is that I need to modify the emali subject line of the delivery depending on the data coming from the workflow, and I don't know how to do that from the delivery itself, as if I try to set the value of delivery.mailParameters.subject I get a 'read only' error. On the other hand, from the script tab it works perfectly.

Regards

Guillermo

Avatar

Level 10

In your specific use case, use rather the easiest way:

Click on the Subject (objet in French) of your template mail, then you can do whatever you want with Javascript and targetData object ready for you, and most of all, accurate to the specific recipientId (or Subscriber or whatever is your delivery mapping):

1286157_pastedImage_0.png

Because, in the Delivery script tab, it is more for global treatment, with specific treatment in the for each loop.

The same kind of use case is when you want to attach a file depending on the targetData context: click on Attach link, then add your file and click on Advanced detail form to add your Javascript test with targetData object usable.
But do it in the Delivery script if the test for adding or not the attached file is far more complex than the data of targetData.
(hoping I am enough clear, sorry English is not my native language.)

Regards
J-Serge

Avatar

Level 4

Thanks so much for your effort Jean-Serge. Both are very good answers.

Regards

Guillermo