Update a field value for a reference table through business rule/workflow script 4m a catalog item

Nitin Baijal
Tera Expert
 

NitinBaijal_1-1671741209017.png

 

Script to autopopulate the owner name for mailbox in the catalog item - 

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setValue('Owner','');
return;
}

var caller = g_form.getReference('mb', doAlert);
}
function doAlert(caller) {
g_form.setValue('owner', caller.u_owner);


}

 

Task at hand - 

once the user submits the ticket with all this information through a catalog item, replace owner1 with owner2 and close the ticket. so basically, find Mailbox1 in reference table and replace the existing owner of it to the variable value of new owner field from the catalog item.

I am fairly new to this , so some of it makes sense to make me but not all of it.

 

Issues faced - I have tried both business rule and script by executing below script but i am unable to find any clear error messages(maybe I am looking at the wrong places)

 

question - In glide record should i be giving the reference table name because thats where i want the update to happen or sys_user because the owner name resides there?

--------------------------------

var user_ref = current.variable.owner.getValue(); //get the sys_id of the Owner
var userManValue = current.variables.newowner.getValue(); //get the sys_id of the new Owner
var userManDisplayValue = current.variables.newowner.getDisplayValue(); //get the displayValue of the new owner

var userU = new GlideRecord('reference table 1');
if (userU.get(user_ref)){ //get user record using the owner sys_id
userU.setValue("owner",userManValue,userManDisplayValue); //set the owner's value and displayValue (prevents roundtrip to server)
userU.update(); //update record


}

 

Please help me with this, it is getting pretty urgent that i resolve this

3 ACCEPTED SOLUTIONS

Ok..Got it...Please try below code

 

var user_ref = current.variables.owner.getValue(); //get the sys_id of the Owner
var userManValue = current.variables.newowner.getValue(); //get the sys_id of the new Owner

gs.info('------------user_ref -'+user_ref);
gs.info('-------------userManValue -'+userManValue);

var userU = new GlideRecord('u_shared_mailbox');
userU.addQuery('u_owner',user_ref);
userU.query();
if (userU.next())
{ //get user record using the owner sys_id
userU.setValue("u_owner",userManValue); 
userU.update(); //update record
}

 


Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

You sir are a champion 🙂

thank you for hand holding me for this one.

it worked and the value is changed.

 

Few additional requirements that i have are -

- once the RITM is raised, it goes for approval

- once approved, a task is created

- once the task is created, the owner is updated and task is closed

 

I am going to work on these on my own and only if i am stuck somewhere, i will bother you again 🙂

 

you are a rockstar!!!

View solution in original post

 

Can you please try below code?

 

var user_ref = current.variables.owner.getValue(); //get the sys_id of the Owner
var userManValue = current.variables.newowner.getValue(); //get the sys_id of the new Owner
var sharedmailbox = current.variables.shared_mailbox.getValue();//get the sys_id of mailbox selected

gs.info('------------user_ref -'+user_ref);
gs.info('-------------userManValue -'+userManValue);

var userU = new GlideRecord('u_shared_mailbox');
if (user_ref)
       userU.addQuery('u_owner',user_ref);

userU.addQuery('sys_id',sharedmailbox);
userU.query();
if (userU.next())
{ //get user record using the owner sys_id
userU.setValue("u_owner",userManValue); 
userU.update(); //update record
}

 


Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

17 REPLIES 17

 

Can you please try below code?

 

var user_ref = current.variables.owner.getValue(); //get the sys_id of the Owner
var userManValue = current.variables.newowner.getValue(); //get the sys_id of the new Owner
var sharedmailbox = current.variables.shared_mailbox.getValue();//get the sys_id of mailbox selected

gs.info('------------user_ref -'+user_ref);
gs.info('-------------userManValue -'+userManValue);

var userU = new GlideRecord('u_shared_mailbox');
if (user_ref)
       userU.addQuery('u_owner',user_ref);

userU.addQuery('sys_id',sharedmailbox);
userU.query();
if (userU.next())
{ //get user record using the owner sys_id
userU.setValue("u_owner",userManValue); 
userU.update(); //update record
}

 


Please mark this response as correct or helpful if it assisted you with your question.

Hi Sanjiv,

your modified code works like a charm... 🙂

now this might sound weird but can you help me understand this part of the code, I am not too good with code 😞  - 

 

var userU = new GlideRecord('u_shared_mailbox');
if (user_ref)
       userU.addQuery('u_owner',user_ref);

userU.addQuery('sys_id',sharedmailbox);
userU.query();
if (userU.next())

1) I believe , 

if (user_ref)
userU.addQuery('u_owner',user_ref);

is tackling the condition for null values in owner field? correct?

if this statement is false, what happens?

2) why are we using 'sys id' , instead of the field name "u_display_name" in - 

userU.addQuery('sys_id',sharedmailbox);

 

thank you again for hand holding me and being a rockstar!!!

1) I believe , 

if (user_ref)
userU.addQuery('u_owner',user_ref);

is tackling the condition for null values in owner field? correct? Thats correct

if this statement is false, what happens? If false, it wont include owner in the query and only search by mailbox 

2) why are we using 'sys id' , instead of the field name "u_display_name" in - 

userU.addQuery('sys_id',sharedmailbox); 

shared_mailbox is a reference variable in your form. so it stores the sysid and not the display name of the mailbox


Please mark this response as correct or helpful if it assisted you with your question.