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

Hi Sanjiv,

I ran the updated one but no Joy.

I dont know if this is the correct way to identify any issue but i ran the script in system definition> Scripts-Background and i see the error below  - 

Evaluator: com.glide.script.RhinoEcmaError: Cannot read property "variables" from null
   script : Line(1) column(0)
==>   1: var user_ref = current.variables.owner.getValue(); //get the sys_id of the Owner
      2: var userManValue = current.variables.newowner.getValue(); //get the sys_id of the new Owner
      3: var userU = new GlideRecord('u_shared_mailbox');
      4: if (userU.get(user_ref))

This might be completely irrelevant but i am just grasping at straws now 🙂

Is there any other way to identify how the script is running?

 

thank you so much for your help with this.

 

Can you confirm, you are running this Business rule on requested item table? Also added few debug statements to check if we are getting the right values

 

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.addInfoMessage('user_ref -'+user_ref);

gs.addInfoMessage('userManValue -'+userManValue);
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.

Hey Sanjiv,

 

I was running this as a workflow script. will it not work there?

I can create a business rule and run it on request item table and see if that solves the problem.

what do you suggest?

Workflow script is fine, if the workflow is running on requested item table. I have added another debug statement. Please check the logs, if you are getting values there.

 

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('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.

NitinBaijal_0-1671752204064.png

NitinBaijal_1-1671752230668.pngNitinBaijal_2-1671752356678.png

I created a business rule and ran it, the business rule is getting called but i think the script is not working. see logs above

 

thanks again for your help Sanjiv