Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

Updating a record from within a Javascript Step

eriku40433679
Level 3
Level 3

Hi guys,

I'm trying to perform some string manipulation on the result set of a previous query. I'm using an intermediary Javascript step to contain the logic.

var schemaName = vars.targetSchema.substr(vars.targetSchema.indexOf(":") + 1); 

logInfo(schemaName);

 

var query = xtk.queryDef.create( 

  <queryDef schema={vars.targetSchema} operation="select"> 

    <select> 

      <node expr="@id"/> 

      <node expr="@description"/> 

    </select> 

  </queryDef> 

); 

result = query.ExecuteQuery(); 

 

 

for each (var e in result) { 

     logInfo(e.@description + e.@id);

    

     // manipulation and assignment logic goes here

     // db update logic goes here

}

// better if the db update goes here and is batched

I'm able to retrieve the fields from the previous step and print the columns out. I'm also able to perform the manipulation required on the description field but I can't, for the life of me, figure out how to SET and UPDATE the field in question.

Should I use a separate sqlExec call using an update sql query as it iterates over each item? Feels a bit inefficient and 'baking' in the string values directly to the sql query seems wrong and dangerous...

Many thanks.

Cheers!

--Erik

1 Accepted Solution
Amit_Kumar
Correct answer by
Community Advisor
Community Advisor

Hi Erik,

Use this:

var schemaName = vars.targetSchema.substr(vars.targetSchema.indexOf(":") + 1);   

logInfo(schemaName); 

   

var query = xtk.queryDef.create(   

  <queryDef schema={vars.targetSchema} operation="select">   

    <select>   

      <node expr="@id"/>   

      <node expr="@description"/>   

    </select>   

  </queryDef>   

);   

var resultSet = query.ExecuteQuery();   

// update the name of schemaName from your query   

var collection = <{schemaName}-collection _operation="update" xtkschema={vars.targetSchema}/> ;

for each (var row in resultSet) {   

var updatedDescription = String(row .@description);

// manipulation and assignment logic goes here

// update the name of schemaName from your query

var schemaNameProgram = <{schemaName} id={row.@id} description={updatedDescription} _key = '@id' xtkschema={vars.targetSchema}/>;

collection.appendChild(schemaNameProgram);

}

  xtk.session.WriteCollection(collection);

Regards,

Amit

View solution in original post

4 Replies
Amit_Kumar
Correct answer by
Community Advisor
Community Advisor

Hi Erik,

Use this:

var schemaName = vars.targetSchema.substr(vars.targetSchema.indexOf(":") + 1);   

logInfo(schemaName); 

   

var query = xtk.queryDef.create(   

  <queryDef schema={vars.targetSchema} operation="select">   

    <select>   

      <node expr="@id"/>   

      <node expr="@description"/>   

    </select>   

  </queryDef>   

);   

var resultSet = query.ExecuteQuery();   

// update the name of schemaName from your query   

var collection = <{schemaName}-collection _operation="update" xtkschema={vars.targetSchema}/> ;

for each (var row in resultSet) {   

var updatedDescription = String(row .@description);

// manipulation and assignment logic goes here

// update the name of schemaName from your query

var schemaNameProgram = <{schemaName} id={row.@id} description={updatedDescription} _key = '@id' xtkschema={vars.targetSchema}/>;

collection.appendChild(schemaNameProgram);

}

  xtk.session.WriteCollection(collection);

Regards,

Amit

View solution in original post

eriku40433679
Level 3
Level 3

Excellent. Thanks Amit! Works like a charm. Hard coded the schema name as per your inline instructions and it works just fine for this specific workflow.

I'd like to know if there's a way for me to use the schemaName variable I've already extracted on the first line to make the collection variable a bit more flexible?

Cheers!

robayzma
Level 4
Level 4

Hi Amit,

when I run the code above in a Javascript Code step I get this error:

23/03/2018 10:11:40 js JST-310000 Error while compiling script 'WKF151/js' line 4: invalid XML tag syntax (line='var collection = <{schemaName}-collection _operation="update" xtkschema={vars.targetSchema}/> ; ' token='-collection _operation="update" xtkschema={vars.targetSchem

23/03/2018 10:11:40 js a}/> ; ').

My resultset comes from a dedupe operation that again comes from an intersect. the schemaname is temp:Intersect

What am I missing?

Kind regards

Robert

poorvab56881543
Level 2
Level 2

Hi Amit,

Thanks for sharing the above snippet to update collection in a DB schema.

I am working over a project where I need to sort customer rankings available under multiple product columns and build Campaign according to product ranking. I am able to sort the product rankings for each customer using querydef and setting it into event variable. However when I am trying to read the rankings for each customer through an Enrichment Activity, I am getting ranking of the last customer corresponding to each customer record instead of their own respective rankings. I believe this is due to the last record I am updating in the for loop.

Can you please suggest a way out to read modified data of each customer specifically within a Campaign workflow.

Thanks,

Poorva