Updating a record from within a Javascript Step

eriku40433679

07-02-2018

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

Accepted Solutions (1)

Accepted Solutions (1)

Amit_Kumar

MVP

08-02-2018

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

Answers (3)

Answers (3)

poorvab56881543

17-09-2019

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

robayzma

23-03-2018

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

eriku40433679

08-02-2018

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!