run script activity in workflow

levino
Giga Guru

 

Hi there

the first part of the code executes but not the second part , that is update , group status to false

 

Please advise

 

Thanks

Levino

 

 

 

workflow.scratchpad.Description = current.variables.description;
workflow.scratchpad.NewGroupName = current.variables.new_group_name;
workflow.scratchpad.ExistingGroupSysId = current.variables.existing_group;
gs.info("workflow_runscript: " + workflow.scratchpad.NewGroupName);
var gr = new GlideRecord('sys_user_group'); //To create group 
gr.initialize();
gr.name = workflow.scratchpad.NewGroupName;
gr.type = 'a23a29e8dbec8850304c147a3a96191f';
gr.company = 'd9057fc1db98d7005070326f7c961926';
var grSysid = gr.insert();
//gs.info("workflow_runscript group_sysid: "+grSysid);

var ExistingGroup = 'workflow.scratchpad.ExistingGroupSysId';
var grName = new GlideRecord('sys_user_group');
grName.addQuery('sys_id', ExistingGroup); // give the sys_id here
grName.query();
if (grName.next()) {
    grName.active = false;
    grName.update();
}

 

1 ACCEPTED SOLUTION

Maddysunil
Kilo Sage

@levino 

It seems that the issue lies in how you're retrieving the value of ExistingGroupSysId from the scratchpad and using it to update the sys_user_group record.

In your code, you're encapsulating the variable name workflow.scratchpad.ExistingGroupSysId within quotes, which treats it as a string literal instead of accessing the value stored in the scratchpad. As a result, the query doesn't return any records, and the subsequent update operation is not executed. Try with below updated code

 

 

workflow.scratchpad.Description = current.variables.description;
workflow.scratchpad.NewGroupName = current.variables.new_group_name;
workflow.scratchpad.ExistingGroupSysId = current.variables.existing_group;
gs.info("workflow_runscript: " + workflow.scratchpad.NewGroupName);

// Create a new group
var gr = new GlideRecord('sys_user_group');
gr.initialize();
gr.name = workflow.scratchpad.NewGroupName;
gr.type = 'a23a29e8dbec8850304c147a3a96191f';
gr.company = 'd9057fc1db98d7005070326f7c961926';
var grSysid = gr.insert();

// Update existing group status
var ExistingGroup = workflow.scratchpad.ExistingGroupSysId;
var grName = new GlideRecord('sys_user_group');
grName.addQuery('sys_id', ExistingGroup);
grName.query();
if (grName.next()) {
    grName.active = false;
    grName.update();
}

 

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

View solution in original post

2 REPLIES 2

Maddysunil
Kilo Sage

@levino 

It seems that the issue lies in how you're retrieving the value of ExistingGroupSysId from the scratchpad and using it to update the sys_user_group record.

In your code, you're encapsulating the variable name workflow.scratchpad.ExistingGroupSysId within quotes, which treats it as a string literal instead of accessing the value stored in the scratchpad. As a result, the query doesn't return any records, and the subsequent update operation is not executed. Try with below updated code

 

 

workflow.scratchpad.Description = current.variables.description;
workflow.scratchpad.NewGroupName = current.variables.new_group_name;
workflow.scratchpad.ExistingGroupSysId = current.variables.existing_group;
gs.info("workflow_runscript: " + workflow.scratchpad.NewGroupName);

// Create a new group
var gr = new GlideRecord('sys_user_group');
gr.initialize();
gr.name = workflow.scratchpad.NewGroupName;
gr.type = 'a23a29e8dbec8850304c147a3a96191f';
gr.company = 'd9057fc1db98d7005070326f7c961926';
var grSysid = gr.insert();

// Update existing group status
var ExistingGroup = workflow.scratchpad.ExistingGroupSysId;
var grName = new GlideRecord('sys_user_group');
grName.addQuery('sys_id', ExistingGroup);
grName.query();
if (grName.next()) {
    grName.active = false;
    grName.update();
}

 

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Community Alums
Not applicable

Hi @levino ,

In the given script you mentioned group name as in Strings, so it would not able to take the dynamic value 

workflow.scratchpad.Description = current.variables.description;
workflow.scratchpad.NewGroupName = current.variables.new_group_name;
workflow.scratchpad.ExistingGroupSysId = current.variables.existing_group;
gs.info("workflow_runscript: " + workflow.scratchpad.NewGroupName);
var gr = new GlideRecord('sys_user_group'); //To create group 
gr.initialize();
gr.name = workflow.scratchpad.NewGroupName;
gr.type = 'a23a29e8dbec8850304c147a3a96191f';
gr.company = 'd9057fc1db98d7005070326f7c961926';
var grSysid = gr.insert();
//gs.info("workflow_runscript group_sysid: "+grSysid);

var ExistingGroup = workflow.scratchpad.ExistingGroupSysId;
var grName = new GlideRecord('sys_user_group');
grName.addQuery('sys_id', ExistingGroup); // give the sys_id here
grName.query();
if (grName.next()) {
    grName.active = false;
    grName.update();
}

 

Please mark my answer correct and helpful if this helps you
 
Thanks and Regards 
Sarthak