Using fix script same table we need to copy from one field to other field

mani55
Tera Contributor

Using fix script same table we need to copy from one field to other field i tried with below code but it's not working let me know my mistake 

var ga=new GlideRecord('sn_compliance_policy_exception');
ga.addEncodedQuery('policy=87420b0bc3ef71103cbbfb1f0501317b^cmdb_ciISEMPTY');
ga.query();
while(ga.next()){
	ga.cmdb_ci=ga.user_input;
	//current.name = current.u_imei_string;
	ga.update();
}
1 ACCEPTED SOLUTION

Hi, you didn't change anything in the script I gave you right ?
For me it works fine, the record comes up in the cmdb_ci field correctly. Also one thing, you are sure right that the input provided in the user_input field is the name of the cmdb_ci item ? if the name doesn't match then the cmdb_ci wont get populated.

View solution in original post

16 REPLIES 16

i checked background script it's not showing any thing

var ga=new GlideRecord('sn_compliance_policy_exception');
ga.addEncodedQuery('policy=87420b0bc3ef71103cbbfb1f0501317b^cmdb_ciISEMPTY');
ga.query();
while(ga.next()){
	gs.print('the rowa'+ga.getRowCount());
    var gN = new GlideRecord('cmdb_ci_pc_hardware');
    gN.addQuery('name', ga.user_input);
    gN.query();
    if (gN.next()) {
		//gs.print('the rowa'+gN.getRowCount());
        ga.cmdb_ci = gN.sys_id;
        ga.update();
    }
}

@mani55 

The script is working perfectly fine for me.
1. Check whether the Encoded query you have given is right or not and whether it successfully picks any record or not.

2. Check whether any cmdb_ci_pc_hardware record exists with the same name which the user input field has. If not, then the scriptwont work.

 

For me after execution:

soumyadeep10_0-1764246099163.png

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Ankur Bawiskar
Tera Patron
Tera Patron

@mani55 

it will only work if both the field type is same

user_input and cmdb_ci both refer to same table?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Deepak Shaerma
Mega Sage
Mega Sage

Hi @mani55 

Simply use this script:

var ga = new GlideRecord('sn_compliance_policy_exception');
// 1. Ensure the query is actually finding records
ga.addEncodedQuery('policy=87420b0bc3ef71103cbbfb1f0501317b^cmdb_ciISEMPTY'); 
// Optional: restrict row count for testing
// ga.setLimit(10); 
ga.query();

while(ga.next()){
    // 2. Ensure 'user_input' actually has data to copy
    if(ga.user_input){
        // 3. Make sure Workflow/Business Rules don't block this
        ga.setWorkflow(false); 
        ga.autoSysFields(false);
        
        // 4. Copy the value
        ga.cmdb_ci = ga.user_input;
        ga.update();
    }
}



Happy to help! If this resolved your issue, kindly mark it as the correct answer and Helpful and close the thread so others can benefit too.

Warm Regards,

Deepak Sharma

Community Rising Star 2025



soumyadeep10
Tera Guru

@mani55 

Kindly check my response, this working as expected for me:

(Change the field names accordingly if required)

 

var ga=new GlideRecord('sn_compliance_policy_exception');
ga.addEncodedQuery('policy=87420b0bc3ef71103cbbfb1f0501317b^cmdb_ciISEMPTY');
ga.query();
if (ga.next()){
    var gN = new GlideRecord('cmdb_ci_pc_hardware');
    gN.addQuery('name', ga.user_input);
    gN.query();
    if (gN.next()) {
        ga.cmdb_ci = gN.sys_id;
        ga.update();
    }
}

 *************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.