Script To Insert or Update Record

Monish2
Tera Contributor

Hi, I am looking for a script which inserts or updates the row in a specific table. the update criteria is combination of few column values. if this combination don't have the existing unique value in that specific table, insert the row. This script has to run 'OnAfter' on a particular transform map. any leads on this is appreciated. thank you!

1 ACCEPTED SOLUTION

Ct111
Giga Sage

yes

 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

var a = source.u_A;

var b = source.u_B;

var c  = a + ' ' + b;

 

var gr = new GlideRecord('table_in_which_you_to_compare');

gr.addQuery('field_name',c);

gr.query();

if(gr.next())

{

log.info('Record already exist no need for creation');

}

else{

target.field_name = c;

target.update();

}

})(source, map, log, target);

 

I hope this examples gives you some reference to workout your script

 

Mark my ANSWER as CORRECT and HELPFUL if it helps

View solution in original post

6 REPLIES 6

Ct111
Giga Sage

yes

 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

var a = source.u_A;

var b = source.u_B;

var c  = a + ' ' + b;

 

var gr = new GlideRecord('table_in_which_you_to_compare');

gr.addQuery('field_name',c);

gr.query();

if(gr.next())

{

log.info('Record already exist no need for creation');

}

else{

target.field_name = c;

target.update();

}

})(source, map, log, target);

 

I hope this examples gives you some reference to workout your script

 

Mark my ANSWER as CORRECT and HELPFUL if it helps

Monish2
Tera Contributor

Hi, thanks for the quick reply. May be, I need to specify few more details. 

I have a staging table 'A' from which target table 'B' is populated using a transform map. once this is done, the 'OnAfter' Script should load the same data to Table 'C' with the mentioned update logic.

will this script work in that case?  how do I specify the target table 'C' in this script?

 

thanks!

You populate field values of target not the table.

So, lets say you have value that is coming from Staging table A  (that will be  source.field_name)

 

Now based on that value you are searching something in table B  ( that will be glide record table)

 

and once you find or not find data you will be updating value in target table C (that will be target.fieldname)

 

So this script is apt I believe

 

Staging Table : Source Table

FInal table in which you want to put data : target table 

If you are still not clear watch this video I hope you wll get clarity LINK

 

Mark my ANSWER as CORRECT and HELPFUL if it helps