Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to insert SCTASK worknotes when worknotes is added to RITM , the same worknotes i want on SCTASK

sanasayyad
Tera Contributor

I have written a business rule on sc_req_item i.e after insert here is the code 

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here

    current.work_notes = "test";
    current.update();

    //Now i want same thing on sc_task table so i have written this

    var id = current.sys_id;

    var grp = new GlideRecord("sc_task");
    grp.addQuery('request_item', id);
    gs.info("The ID is " + id);
    grp.query();

    gs.info("testing query: Row count = " + grp.getRowCount());

    if (grp.next()) {
        gs.info("test");
        grp.work_notes = "test ";
        grp.update();
    }
    else
    {
        gs.log("No sc_task records found for request item: " + id);
    }


})(current, previous);

In this i am not getting record by doing  grp.getRowCount() in system log i am getting  record as 0.
1 ACCEPTED SOLUTION

VikMach
Mega Sage

Hi @sanasayyad,
Here is the revised code. It should be after update.
Try this. Your code has few misses. Updated them below.

(function executeRule(current, previous /*null when async*/) {

	var sys_id = current.getUniqueValue();


	var scTask = new GlideRecord('sc_task');
	scTask.addActiveQuery();
	scTask.addQuery('request_item', sys_id);
	scTask.query();

	if(scTask.hasNext()){

		var currentWorkNotes = current.work_notes.getJournalEntry(1);

		while(scTask.next()){

			if(scTask.work_notes.getJournalEntry(1) == '' || currentWorkNotes.indexOf(scTask.work_notes.getJournalEntry(1)) <0){

				scTask.work_notes = currentWorkNotes;
				scTask.update();
			}
		}
	}
})(current, previous);


Let me know if this worked.

Regards,
Vikas K

View solution in original post

1 REPLY 1

VikMach
Mega Sage

Hi @sanasayyad,
Here is the revised code. It should be after update.
Try this. Your code has few misses. Updated them below.

(function executeRule(current, previous /*null when async*/) {

	var sys_id = current.getUniqueValue();


	var scTask = new GlideRecord('sc_task');
	scTask.addActiveQuery();
	scTask.addQuery('request_item', sys_id);
	scTask.query();

	if(scTask.hasNext()){

		var currentWorkNotes = current.work_notes.getJournalEntry(1);

		while(scTask.next()){

			if(scTask.work_notes.getJournalEntry(1) == '' || currentWorkNotes.indexOf(scTask.work_notes.getJournalEntry(1)) <0){

				scTask.work_notes = currentWorkNotes;
				scTask.update();
			}
		}
	}
})(current, previous);


Let me know if this worked.

Regards,
Vikas K