Updating a record from within a Javascript Step

Avatar

Avatar
Validate 10
Level 2
eriku40433679
Level 2

Likes

10 likes

Total Posts

33 posts

Correct reply

2 solutions
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 10
View profile

Avatar
Validate 10
Level 2
eriku40433679
Level 2

Likes

10 likes

Total Posts

33 posts

Correct reply

2 solutions
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 10
View profile
eriku40433679
Level 2

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)

Avatar

Avatar
Springboard
MVP
Amit_Kumar
MVP

Likes

329 likes

Total Posts

649 posts

Correct reply

231 solutions
Top badges earned
Springboard
Ignite 5
Ignite 3
Ignite 1
Validate 10
View profile

Avatar
Springboard
MVP
Amit_Kumar
MVP

Likes

329 likes

Total Posts

649 posts

Correct reply

231 solutions
Top badges earned
Springboard
Ignite 5
Ignite 3
Ignite 1
Validate 10
View profile
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)

Avatar

Avatar
Validate 10
Level 3
robayzma
Level 3

Likes

13 likes

Total Posts

53 posts

Correct reply

2 solutions
Top badges earned
Validate 10
Validate 1
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Validate 10
Level 3
robayzma
Level 3

Likes

13 likes

Total Posts

53 posts

Correct reply

2 solutions
Top badges earned
Validate 10
Validate 1
Ignite 5
Ignite 3
Ignite 1
View profile
robayzma
Level 3

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

Avatar

Avatar
Validate 1
Level 2
poorvab56881543
Level 2

Likes

7 likes

Total Posts

9 posts

Correct reply

1 solution
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 1
Affirm 1
View profile

Avatar
Validate 1
Level 2
poorvab56881543
Level 2

Likes

7 likes

Total Posts

9 posts

Correct reply

1 solution
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 1
Affirm 1
View profile
poorvab56881543
Level 2

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

Avatar

Avatar
Validate 10
Level 2
eriku40433679
Level 2

Likes

10 likes

Total Posts

33 posts

Correct reply

2 solutions
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 10
View profile

Avatar
Validate 10
Level 2
eriku40433679
Level 2

Likes

10 likes

Total Posts

33 posts

Correct reply

2 solutions
Top badges earned
Validate 10
Validate 1
Boost 5
Boost 3
Boost 10
View profile
eriku40433679
Level 2

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!