Script doesnt work on fix script

Black Coder
Tera Guru

What is the issue in below code?

We are trying to update the 'VIP" field  based on company code. Since background script doesn't support for scoped applications we were trying from Fix scripts. But still doesnt work

Points

* sn_hr_core_profile table is from Human Resource core application

* vip filed is a reference field from sys_user table

 

var vipUser = new GlideRecord('sn_hr_core_profile');
vipUser.addEncodedQuery('u_payroll_company_codeIN2Q3,2ZC,CS3,CSF,EUP,GVP,NRV,Q3U,VH6,VKH,XH7');
vipUser.query();
while(vipUser.next()){
vipUser.setValue('user.vip', true);
vipUser.update();
}

 

 

1 ACCEPTED SOLUTION

Peter Bodelier
Giga Sage

Hi @Black Coder,

 

Try this:

var vipUserProfile = new GlideRecord('sn_hr_core_profile');
vipUserProfile .addEncodedQuery('u_payroll_company_codeIN2Q3,2ZC,CS3,CSF,EUP,GVP,NRV,Q3U,VH6,VKH,XH7');
vipUserProfile .query();
while(vipUserProfile .next()){
var vipUser = new GlideRecord('sys_user');
vipUser.get(vipUserProfile.getValue('user'));
vipUser.setValue('vip', true);
vipUser.update();
}

Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

View solution in original post

3 REPLIES 3

Peter Bodelier
Giga Sage

Hi @Black Coder,

 

Try this:

var vipUserProfile = new GlideRecord('sn_hr_core_profile');
vipUserProfile .addEncodedQuery('u_payroll_company_codeIN2Q3,2ZC,CS3,CSF,EUP,GVP,NRV,Q3U,VH6,VKH,XH7');
vipUserProfile .query();
while(vipUserProfile .next()){
var vipUser = new GlideRecord('sys_user');
vipUser.get(vipUserProfile.getValue('user'));
vipUser.setValue('vip', true);
vipUser.update();
}

Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

SwarnadeepNandy
Mega Sage

Hello @Black Coder,

There are a few possible issues in your code. One issue is that the setValue method expects a field name and a value as arguments, but you are passing a dot-walked reference to the vip field of the user table. This may cause an error or an unexpected result. You can fix this by using the setRefRecord method instead, which allows you to set a reference field to a GlideRecord object. For example, you can use something like this:

//Create a GlideRecord object for the sys_user table
var user = new GlideRecord('sys_user');

//Query for the user with vip set to true
user.addQuery('vip', true);
user.query();

//Check if the user exists
if (user.next()) {
  //Set the user field of the vipUser record to the user record
  vipUser.setRefRecord(user);
}

Another issue could be that the update method may not work properly in a scoped application, especially if the target table or field has any ACLs or business rules that prevent or modify the update. You can fix this by using the _update method instead, which bypasses any ACLs or business rules and performs a direct update. For example, you can use something like this:

//Update the vipUser record without triggering any ACLs or business rules
vipUser._update();

A third issue is that the addEncodedQuery method may not be compatible with some operators or values, such as IN or comma-separated lists. You can fix this by using the addQuery method instead, which allows you to add individual conditions to your query. For example, you can use something like this:

//Add multiple conditions to query by u_payroll_company_code
vipUser.addQuery('u_payroll_company_code', '2Q3');
vipUser.addOrCondition('u_payroll_company_code', '2ZC');
vipUser.addOrCondition('u_payroll_company_code', 'CS3');
//and so on for the rest of the values

 

Hope this helps.

 

Kind Regards,

Swarnadeep Nandy

Community Alums
Not applicable

Hello,
Try this:

 

var vipUser = new GlideRecord('sn_hr_core_profile');
vipUser.addEncodedQuery('u_payroll_company_codeIN2Q3,2ZC,CS3,CSF,EUP,GVP,NRV,Q3U,VH6,VKH,XH7');
vipUser.query();
while (vipUser.next()) {
var user = vipUser.user.getRefRecord();
if (user) {
user.setValue('vip', true);
user.update();
}
}

 

Please mark the reply as helpful if it helped you in finding solution

 

Thanks,
Priyanka