The CreatorCon Call for Content is officially open! Get started here.

How to update current user record by using run script activity in workflow

Nani18
Tera Contributor

Hello Experts,

I have created a catalog item to update user details. Fields given below

1.Select User (reference)

2.Name (string)

3.Location type (dropdown)

4.Mobile number (string)

5.Location (reference)

 

created catalog client script on Select user field to auto populate Name , Location type , Mobile number,Location

Now my requirement is 

On workflow of the product once request is approved then automatically I need to update Name, Location type , Mobile number,Location for selected user on sys_user table.

 

How to achieve this by using runscript activity in workflow.

 

Best Regards,

Nani

 

1 ACCEPTED SOLUTION

Unique45
Mega Sage
Mega Sage

Hello @Nani18 ,

Add the run script activity after approval activity and in that add below code:

 

//Add internal name of your variables and fields

var user = new GlideRecord('sys_user');
user.addQuery('sys_id',current.variables.USER_VARIABLE_INTERNAL_NAME);
user.query();
if(user.next()){
user.name = current.variables.name;
user.location_type = current.variables.location_type;
user.mobile_no = current.variables.mobile_no;
user.location = current.variables.location;
user.update();
}

 

Unique45_0-1704890958928.png

Let me know this code is work for you or not.

 

 

Please mark correct/helpful if this helps you!

View solution in original post

3 REPLIES 3

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Nani18 

created catalog client script on Select user field to auto populate Name , Location type , Mobile number,Location

For this you can use the Auto populate feature in SN. 

 

https://www.youtube.com/watch?v=sXuKaV9aSNQ

 

https://www.servicenow.com/community/now-platform-forum/update-a-record-using-flow-designer/m-p/1120...

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

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Shoheb_IbaaBoss
Tera Guru

Hi,

Try below script. This will get the data from Item and update the data in user table.

 

(function execute(/* current, previous */) {

var itemId = workflow.scratchpad.request_item_id; // Assuming you store the request item ID in the workflow scratchpad

// Check if the request item ID is available
if (itemId) {
var reqItem = new GlideRecord('sc_req_item');
if (reqItem.get(itemId)) {
var userId = reqItem.request.requested_for; // Assuming the user ID is stored in the requested_for field

// Update user record fields
var userRecord = new GlideRecord('sys_user');
if (userRecord.get(userId)) {
userRecord.setValue('name', reqItem.variables.name); // Replace with the actual field names
userRecord.setValue('location_type', reqItem.variables.location_type);
userRecord.setValue('mobile_number', reqItem.variables.mobile_number);
userRecord.setValue('location', reqItem.variables.location);

userRecord.update();
gs.info('User record updated successfully.');
} else {
gs.error('User not found.');
}
} else {
gs.error('Request item not found.');
}
} else {
gs.error('Request item ID not available.');
}

})();

 

Regards,

Shoheb

Unique45
Mega Sage
Mega Sage

Hello @Nani18 ,

Add the run script activity after approval activity and in that add below code:

 

//Add internal name of your variables and fields

var user = new GlideRecord('sys_user');
user.addQuery('sys_id',current.variables.USER_VARIABLE_INTERNAL_NAME);
user.query();
if(user.next()){
user.name = current.variables.name;
user.location_type = current.variables.location_type;
user.mobile_no = current.variables.mobile_no;
user.location = current.variables.location;
user.update();
}

 

Unique45_0-1704890958928.png

Let me know this code is work for you or not.

 

 

Please mark correct/helpful if this helps you!