Run script on workflow level

LaraReddy
Tera Guru

Hi All,

Can anyone please help us on the below issue.

We wrote the below run script on workflow level to add / remove the users from selected  groups but some reasons it's not working properly.

var myString = current.variable_pool.u_users.toString();
workflow.info('Splitting Pool: ' + myString);
//var myString = current.variables.u_users;

var currentUser = gs.getUserID();  // Logged in user
var impUser = new GlideImpersonate();
impUser.impersonate('6816f79cc0a8016401c5a33be04be441'); // Impersonate Admin to update record
var mySplitResult = myString.split(",");

for(i = 0; i < mySplitResult.length; i++)
{  
workflow.info('Adding user: ' + mySplitResult[i]);  
var rec = new GlideRecord('sys_user_grmember');
rec.group = current.variables.u_group;
rec.user =  mySplitResult[i];
if (rec.insert()) {
    workflow.info('User added successfully: ' + mySplitResult[i]);
} else {
    workflow.info('Failed to add user: ' + mySplitResult[i] + ', Error: ' + rec.getErrorMessage());
}
}

impUser.impersonate(currentUser); // End impersonate of admin

Note: on logs level, It's returning the "Failed to add user" info and Error returned as Undefined.


Advance thanks.
 
1 ACCEPTED SOLUTION

Hi LaraReddy,

 

It seems weird, but you need to dry run the code and do some try and checks. Definitely, you cannot do that over the prod. Might need to clone from prod to DEV and try some try checks there. That is the only thing I can suggest right now. Let me know how it goes.

 

Thanks!!

Simran

 

 

View solution in original post

5 REPLIES 5

Mark Manders
Mega Patron

Does the impersonation work? Normally you can't impersonate some one with a higher role (like an admin, if you only have ITIL). What if you create a flow for it and run that as admin? Does that work? No impersonation needed.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Hi Mark,

Many thanks for the response.

Yes the impersonation is working fine but coming to add / remove the users functionality is not working.

Coming to creating a flow, the existing workflow which currently in place is working fine on lower environment level and the same run script is not working on prod level.

We do compared the Roles, ACL's on all the environments level and same process is available but not sure why this script not working on prod level.

Advance thanks,  

Simran12
Giga Expert

Hi LaraReddy,

 

I have run your script in background on my PDI, and it works for me, just added couple of custom values to test. My script is as below, added my comment with the same where you need to make changes -

 

var myString = '62826bf03710200044e0bfc8bcbe5df1, a8f98bb0eb32010045e1a5115206fe3a';

// Users sys_id. I hope your varaible pool returns sys_id's
gs.info('Splitting Pool: ' + myString); // Might be workflow.log won't work. I am not sure yet.

var currentUser = gs.getUserID(); // Logged in user
var impUser = new GlideImpersonate();
impUser.impersonate('0a826bf03710200044e0bfc8bcbe5d7a'); // Add your Admin record sys_id
var mySplitResult = myString.split(",");

for(i = 0; i < mySplitResult.length; i++)
{
gs.info('Adding user: ' + mySplitResult[i]);
var rec = new GlideRecord('sys_user_grmember');
rec.group = '019ad92ec7230010393d265c95c260dd'; //Check your "current.variables.u_group" is reference to group table
rec.user = mySplitResult[i];
if (rec.insert()) {
gs.info('User added successfully: ' + mySplitResult[i]);
} else {
gs.info('Failed to add user: ' + mySplitResult[i] + ', Error: ' + rec.getErrorMessage());
}
}

impUser.impersonate(currentUser); // End impersonate of admin

 

Try this changes and let me know if any issue. Happy to help!

 

Mark this answer correct, if it works for you!!

 

Thanks!

Simran

Hi Simran,

Many thanks for the response.

We checked your suggestions on our script level and it's same as you mentioned.

But not sure why it's not working only prod level because the same script working fine on level environments level.

We also checked all the roles , ACL's and it's completely same on all three env level.

Advance thanks.