I need a fix script to set a value to the same as another field

Jerome MAISETTI
Mega Guru

Hello
I would like to run a fix script to set a value of a field to the same as another field.

On the task table I have a field "Case's Vendor", and where this is "--None"" , I would like to force the value to be the same as the field "Asset's Vendor". (Which is a field in the cmdb_ci table) .

Here is my idea.

var grTask = new GlideRecord("task");
    grTask.addEncodedQuery("cmdb_ci.u_asset_s_vendor!=");
    grTask.query();
    while (grTask.next()) {
        var assetVendor = g_form.getValue('cmdb_ci.u_asset_s_vendor');
        grTask.u_case_s_vendor = assetVendor;
        grTask.update();

    }

 

 

Do you think it can work ?

Thanks

Jérôme

1 ACCEPTED SOLUTION

Aman Kumar S
Kilo Patron

g_form doesn't work in fix scripts:

var grTask = new GlideRecord("task");
    grTask.addEncodedQuery("cmdb_ci.u_asset_s_vendor!=");
    grTask.query();
    while (grTask.next()) {
        var assetVendor = grTask.cmdb_ci.u_asset_s_vendor;
        grTask.u_case_s_vendor = assetVendor;
        grTask.update();

    }

 

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

Will be helpful for future visitors looking for similar questions 🙂

Best Regards
Aman Kumar

View solution in original post

3 REPLIES 3

Aman Kumar S
Kilo Patron

g_form doesn't work in fix scripts:

var grTask = new GlideRecord("task");
    grTask.addEncodedQuery("cmdb_ci.u_asset_s_vendor!=");
    grTask.query();
    while (grTask.next()) {
        var assetVendor = grTask.cmdb_ci.u_asset_s_vendor;
        grTask.u_case_s_vendor = assetVendor;
        grTask.update();

    }

 

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

Will be helpful for future visitors looking for similar questions 🙂

Best Regards
Aman Kumar

Martin Ivanov
Giga Sage
Giga Sage
var grTask = new GlideRecord("task");
    grTask.addEncodedQuery("cmdb_ci.u_asset_s_vendor!=");
    grTask.query();
    while (grTask.next()) {
        var assetVendor = grTask.cmdb_ci.u_asset_s_vendor;
        grTask.u_case_s_vendor = assetVendor;
        grTask.update();

    }

Please mark Correct and Helpful if my answer helps you resolve your issue. Thanks!
Martin Ivanov
Community Rising Star 2022


Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

your query is wrong; ensure you give correct query

Sharing my inputs

1) use a function to wrap the code

2) use try catch block to handle the exception

3) use setWorkflow(false) to avoid any BR from triggering on update if you wish for faster update

updateRecords();

function updateRecords(){
	try{
		var grTask = new GlideRecord("task");
		grTask.addEncodedQuery("u_asset_s_vendorISEMPTY"); // give correct query based on what value --None-- has
		grTask.query();
		while (grTask.next()) {
			var assetVendor = grTask.cmdb_ci.u_asset_s_vendor;
			grTask.u_case_s_vendor = assetVendor;
			grTask.setWorkflow(false);
			grTask.update();
		}
	}
	catch(ex){
		gs.info(ex);
	}
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader