Business Rule don't pass sys_id to script include

JohnDF
Mega Sage

Hi everyone,

I created a simple BR to pass the sys id of the latest records from the rm_relaease table to a script include to use the sys id in the script include for further updates.

here is the BR:

 

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

  // Get the latest record from rm_release table
  var latestReleaseRecord = new GlideRecord('rm_release');
  latestReleaseRecord.orderByDesc('sys_created_on');
  latestReleaseRecord.addEncodedQuery('numberSTARTSWITHRLSE'); 
  latestReleaseRecord.setLimit(1);// Order by sys_created_on to get the latest record
  latestReleaseRecord.query();

  // Check if a record exists and retrieve the latest sys_id
  while (latestReleaseRecord.next()) {
    var parentSysID = latestReleaseRecord.getValue('sys_id'); // Get the sys_id of the latest record
gs.addInfoMessage(parentSysID);
    // Call the Script Include
    new REupdateReleasePhaseRecords().updateRecords(parentSysID); // Call the function and pass sys_ids

  }
})(current, previous);

 


And here the script include:

 

 

var REupdateReleasePhaseRecords = Class.create();
REupdateReleasePhaseRecords.prototype ={
    initialize: function() {},
updateRecords: function(parentSysID) {
    // Get the current record
	//gs.sleep(3000);
	gs.info('Script include updateRecords called with sys_id: ' + parentSysID);
    var gr = new GlideRecord('rm_release_phase');
    gr.addEncodedQuery('parent.sys_idSTARTSWITH' + parentSysID);
    gr.query();
    // Loop through the related rm_release_phase records
    while (gr.next()) {
	gs.info('Updating record: ' + gr.sys_id);
      // Update the desired fields in the rm_release_phase records
      gr.start_date = ''; // clear
      gr.end_date = ''; // clear
      gr.update();
    }
},
    type: 'REupdateReleasePhaseRecords'
};

 

I called the BR part in the background script and get the following error:

Background message, type:info, message: 808262661b30da50f66a96859b4bcb91
*** Script: Script include updateRecords called with sys_id: undefined

Can you help me what could be the problem?

1 ACCEPTED SOLUTION

Hello @JohnDF 

Please make the change in below line of code and remove the STARTSWITH in the query because you are only get the sys_id of the record so can only query like    gr.addEncodedQuery('parent.sys_id'+ parentSysID)

  gr.addEncodedQuery('parent.sys_idSTARTSWITH' + parentSysID);

 

View solution in original post

5 REPLIES 5

Sanjay191
Tera Sage

Hello @JohnDF 

If you fatch only one record then no need to use while loop,  it gives the undefine, please use the if statement instead of while loop and also you can use the get method.
and second thing is that you can apply the same logic in your BR no need to create other script include.

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Thank You

Hi @Sanjay191 thanks for reply
Ok I changed the while into if, but I get the same error in background script.

What do you mean with the get mehtod? Where should I replace that? Please let the script include there. There is a reason.

Hello @JohnDF 

Please make the change in below line of code and remove the STARTSWITH in the query because you are only get the sys_id of the record so can only query like    gr.addEncodedQuery('parent.sys_id'+ parentSysID)

  gr.addEncodedQuery('parent.sys_idSTARTSWITH' + parentSysID);

 

Abhishek_Thakur
Mega Sage

Hello @JohnDF ,

Please follow the below code it might help you.

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

  // Get the latest record from rm_release table
  var latestReleaseRecord = new GlideRecord('rm_release');
  latestReleaseRecord.orderByDesc('sys_created_on');
  latestReleaseRecord.addEncodedQuery('numberSTARTSWITHRLSE'); 
  latestReleaseRecord.setLimit(1);// Order by sys_created_on to get the latest record
  latestReleaseRecord.query();

  // Check if a record exists and retrieve the latest sys_id
  if(latestReleaseRecord.next()) {
    var parentSysID = latestReleaseRecord.getUniqeValue(); // Get the sys_id of the latest record
gs.addInfoMessage(parentSysID);
    // Call the Script Include
    new REupdateReleasePhaseRecords().updateRecords(parentSysID); // Call the function and pass sys_ids

  }
})(current, previous);

 

Script Include

var REupdateReleasePhaseRecords = Class.create();
REupdateReleasePhaseRecords.prototype ={
    initialize: function() {},
updateRecords: function(parentSysID) {
    // Get the current record
	//gs.sleep(3000);
	gs.info('Script include updateRecords called with sys_id: ' + parentSysID);
    var gr = new GlideRecord('rm_release_phase');
    gr.addEncodedQuery('parent.sys_idSTARTSWITH' + parentSysID);
    gr.query();
    // Loop through the related rm_release_phase records
    while (gr.next()) {
	gs.info('Updating record: ' + gr.getUniqeValue());
      // Update the desired fields in the rm_release_phase records
      gr.start_date = ''; // clear
      gr.end_date = ''; // clear
      gr.update();
    }
},
    type: 'REupdateReleasePhaseRecords'
};

 

Please mark my answer as accepted solution and give thumbs up, if it helps you.