Onbefore Transform script not working - record still updating

DanielCordick
Mega Patron
Mega Patron

I have an onbefore transform script running, which should not update the record if the work notes are the same. The record keeps updating the work notes even if there is no change and the Source string equals the target record string.

comments in this case are >>> "Testing 123"

here is my script

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

var notes = target.work_notes.getJournalEntry(1);

var worknoteContent = notes.split("(Work notes)\n");

var lastWorknote = worknoteContent[1];

// gs.log(source.u_comments);  
// gs.log(lastWorknote);

if (source.u_comments == lastWorknote) {

ignore = true;
}
// Add your code here

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

1 ACCEPTED SOLUTION

DanielCordick
Mega Patron
Mega Patron

IF anyone is looking to compare strings in an onBefore transform script, the geniuses at SN helped me with this

 

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

var notes = target.work_notes.getJournalEntry(1);

var worknoteContent = notes.split("(Work notes)\n");


var lastWorknote = worknoteContent[1];

var string = lastWorknote.toString();


var string1 = source.u_comments;


//gs.log("------->>> SOURCE = " + source.u_comments + "------->>> TARGET = " + lastWorknote + "-->String" + string + "-->u_comments:" + source.u_comments.toString() + "WorknoteContent:" + worknoteContent + "-->lastworkfnote:" + lastWorknote + "Equals : " + string.indexOf(string1));
// gs.info("------->>> TARGET = " + lastWorknote);

//var string1=source.u_comments.toString();


//if (string == source.u_comments.toString()) {
if (string.indexOf(string1) >= 0) {

ignore = true;
}

// Add your code here

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

 

View solution in original post

7 REPLIES 7

MrMuhammad
Giga Sage

You are comparing comments with worknotes. try replacing worknotes with u_comments in the if condition 

if (source.u_work_notes == lastWorknote) // please replace u_work_notes with actual name of worknotes field in the staging table.

Please mark this correct & helpful if it answered your question.

 

Regards,
Muhammad

Hi muhammed u_comments, is a string field on my staging table, I am trying to compare the string on my staging table to the last work note on the target table. If source.u_comments = Test and the last work note on the target = Test, it should ignore insert. But this isn’t happening with my current code

Ah okay. if you want to compare with only last worknotes entered. Then in the getJournalEntry() function you need to pass -1 rather than 1. 1 is for all the worknotes. 

 

try this. i have placed some logs let me know what you get in logs. 

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

  var notes = target.work_notes.getJournalEntry(-1);

  gs.log("SH - " + source.u_comments);  
  gs.info("SH - " +notes);

  if (source.u_comments == notes) {
   ignore = true;
  }

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

 

Please mark this correct & helpful if it answered your question.

Thanks & Regards,
Sharjeel

 

Regards,
Muhammad

-1 is for all journal entry's, i got that from the docs 

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/no-namespace/c_GlideElementSc... 

 

find_real_file.png

Script for the image above

 

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

var notes = target.work_notes.getJournalEntry(1);

var worknoteContent = notes.split("(Work notes)\n");

var lastWorknote = worknoteContent[1];

gs.log("------->>> SOURCE = " + source.u_comments);
gs.info("------->>> TARGET = " + lastWorknote);

if (source.u_comments == lastWorknote) {

ignore = true;
}

// Add your code here

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