Need help in writing code to ensure problem have atleast one problem task

sravya03
Tera Contributor

Need help in writing code to ensure problem ticket have at least one problem task apart from 2 mandatory Ptasks.

I have written below code to achieve the req based on mandatory ptask short description

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
var action = g_form.getValue('u_action');
var state = g_form.getValue('state');
var number = g_form.getValue('number');
if (state == '108' && action == 'false')
{
    // var rec = new GlideRecord('problem_task');
    // rec.addQuery('short_description','DOES NOT CONTAIN','ABC');
    // // //rec.addQuery('short_description','DOES NOT CONTAIN','XYZ');
    // rec.query();
    // var none = new GlideRecord('problem_task');
    // none.addQuery('problem', number);
    // none.query();
    // if(rec.hasNext() || none.rows.length == 0){
    alert('Please create a problem task' );

    //return;
}
User should have atleast one ptask before moving from one state to another.
3 REPLIES 3

Sandeep Rajput
Tera Patron
Tera Patron

@sravya03 Instead of achieving this implementation through a client script, you should use a business rule instead.

Screenshot 2023-09-15 at 12.00.47 AM.pngScreenshot 2023-09-15 at 12.01.53 AM.png

Here is the BR script.

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

    // Add your code here
    var rec = new GlideRecord('problem_task');
    rec.addEncodedQuery('short_descriptionNOT LIKEABC^short_descriptionNOT LIKEXYZ');
    rec.query();
    if (rec.getRowCount() == 0) {
		gs.addErrorMessage('Please create a problem task');
		gs.setAbortAction(true);
    }

})(current, previous);

Hi Sandeep,

 

The code is not working.

sravya03_0-1695317216197.png

 

sravya03_1-1695317275445.png

 

 

 

Change

gs.setAbortAction(true);

as

current.setAbortAction(true);