How to check a field value for all change tasks?

Aishwarya Selv1
Tera Contributor

For example, I have added a new field (abc) in change requests form, this abc field is already available in all change tasks form so I have to get the abc field value from all the change tasks and display the maximum value in the abc field of change request form. How will I query it?

1 ACCEPTED SOLUTION

Mohit Kaushik
Mega Sage
Mega Sage

Hi Aishwarya,

Could you please tell me if this solution is for old data or you want to make a generic solution.

For old data you can write a Background script or fix script as shown below:

var ctask = new GlideRecord('change_task');
ctask.query();
while(ctask.query()){
    var change = new GlideRecord('change_request');
    change.addQuery('sys_id',ctask.change_request);
    change.query();
    if(change.next()){
        if(change.abc<ctask.abc){ //instead of abc please use your field name
            change.abc = ctask.abc;
            change.setWorkflow(false);
            change.update();
        }
    }
}

 

Generic Solution:

As @Aman Kumar told you need to create an after Insert and Update Business rule on Change Task table and that should be able to handle your requirement. You can write below script in your Business Rule.

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

    // Add your code here
    var change = new GlideRecord('change_request');
    change.addQuery('sys_id', current.change_request);
    change.query();
    if (change.next()) {
        if (change.abc < current.abc) { //instead of abc please use your field name
            change.abc = current.abc;
            change.update();
        }
    }
})(current, previous);

 

Please mark this as correct and helpful if it resolved the query or lead you in right direction.

Thanks,
Mohit Kaushik
Community Rising Star 2022

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

View solution in original post

7 REPLIES 7

Aman Kumar S
Kilo Patron

You have to write a after insert/update BR on change task table, through which you will check the value on the Change new field to compare if the value on task is greater than the one in change, if found, you will update the Change new field.

For example, you will have no value in change new field, once a change task is created it will check if it is empty, it will update the value, if more change tasks are created, it will check the value in change new field and so on.

 

Feel free to mark correct, If I answered your query.

Will be helpful for future visitors looking for similar questions 🙂

 

Best Regards
Aman Kumar

Actually I should compare all the change tasks value, not change task with change request form

For example:

Change task 1--> abc field value = 2

Change task 2--> abc field value = 3

Change task 3--> abc field value = 1

Max value of these change tasks is 3 (from change task 2)

so now query should compare these 3 change tasks value and display the max value in the abc field of change request form

That is fine, the logic that I mentioned will run on each insert/update of change task or change in these values,

lets say:

change task 1 value is 1 , it will update the change value as 1

change task 2 value is 2, it will update the change value as 2

change task 3 value is 1, it won't make any update to change new value

Best Regards
Aman Kumar

Mohit Kaushik
Mega Sage
Mega Sage

Hi Aishwarya,

Could you please tell me if this solution is for old data or you want to make a generic solution.

For old data you can write a Background script or fix script as shown below:

var ctask = new GlideRecord('change_task');
ctask.query();
while(ctask.query()){
    var change = new GlideRecord('change_request');
    change.addQuery('sys_id',ctask.change_request);
    change.query();
    if(change.next()){
        if(change.abc<ctask.abc){ //instead of abc please use your field name
            change.abc = ctask.abc;
            change.setWorkflow(false);
            change.update();
        }
    }
}

 

Generic Solution:

As @Aman Kumar told you need to create an after Insert and Update Business rule on Change Task table and that should be able to handle your requirement. You can write below script in your Business Rule.

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

    // Add your code here
    var change = new GlideRecord('change_request');
    change.addQuery('sys_id', current.change_request);
    change.query();
    if (change.next()) {
        if (change.abc < current.abc) { //instead of abc please use your field name
            change.abc = current.abc;
            change.update();
        }
    }
})(current, previous);

 

Please mark this as correct and helpful if it resolved the query or lead you in right direction.

Thanks,
Mohit Kaushik
Community Rising Star 2022

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)