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

SanjivMeher
Kilo Patron
Kilo Patron

Can you try this script? Do you have an owner field in that table? If it is a custom table, it may be u_owner.

 

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 userU = new GlideRecord('reference table 1');
if (userU.get(user_ref))

{ //get user record using the owner sys_id
userU.setValue("owner",userManValue); 
userU.update(); //update record

}

 


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

Hi Sanjiv,

 

Yes, the field in reference table is u_owner but the variable name in catalog item is Owner and newowner.

I ran the script above, but the values did not change in the reference table.It is still the same.

Where can i check for any logs that can direct me if it ran fine?

See screenshot below , i used your script in the workflow.

I have used it with both "Owner" and "u_owner" in setvalue 

NitinBaijal_0-1671744672600.png

 

Ok..correction to line 1

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 userU = new GlideRecord('reference table 1');
if (userU.get(user_ref))

{ //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.