Onbefore Script for transform map

Rajitha Sunkara
Tera Contributor

Only group members who have an active user record in sys_user table should be added to the group when the transform runs.

Group members should only be added to an active group in the sys_user_group table.

So columns should be mapped as SOEID as user.user_name and group name as group_name.

So for we need onbefore scripting we have created that scripting but it is showing inactive users also.

The script is in the attachment.

Source Field: u_group_name

Target Field Mapping: group.name

Target Table:sys_user_grmember.

1 ACCEPTED SOLUTION

Pavankumar_1
Mega Patron

Hi,

You can try this script:

on Sys_user table first you will check whether user is active or not

var grusr = new GlideRecord("sys_user");
grusr.addActiveQuery();
grusr.addQuery("user_name", source.SOEID ); // i am assuming soeid is source user field
grusr.query();
if(grusr.next()){ //if user is active then go to the sys_user_grmember table add user to that group

 var grusrmbr = new GlideRecord("sys_user_grmember");
                grusrmbr.addQuery("group.name", u_group_name); //u_group_name is source grup name
                grusrmbr.query();
                if (grusrmbr.next()) {
                    target.user = source.SOEID;
                } else {
                    target.user = '';

                }
    
}

 

Thanks,

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

5 REPLIES 5

Rahul RJ
Giga Sage

Hi Rajitha,

 

code have issue in grRec.query(); in line 7 and grRec.next() in if condition.

 

Regards,

RJ

 

Aman Kumar S
Kilo Patron

It would be more convenient to help, if you would have posted the script as text instead of screenshot, nevertheless,

var grRec = new GlideRecord("sys_user");
grRec.addActiveQuery();
grRec.addQuery("user_name", source.u_group_name);// is this user name or group name, confirm on this?
grRec.query();
if(grRec.next()){
    target.user = source.u_group_name;
}
Best Regards
Aman Kumar

Rajitha Sunkara
Tera Contributor

It is grp name.

Pavankumar_1
Mega Patron

Hi,

You can try this script:

on Sys_user table first you will check whether user is active or not

var grusr = new GlideRecord("sys_user");
grusr.addActiveQuery();
grusr.addQuery("user_name", source.SOEID ); // i am assuming soeid is source user field
grusr.query();
if(grusr.next()){ //if user is active then go to the sys_user_grmember table add user to that group

 var grusrmbr = new GlideRecord("sys_user_grmember");
                grusrmbr.addQuery("group.name", u_group_name); //u_group_name is source grup name
                grusrmbr.query();
                if (grusrmbr.next()) {
                    target.user = source.SOEID;
                } else {
                    target.user = '';

                }
    
}

 

Thanks,

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar