Labels

Wednesday, January 15, 2014

Copy user groups from one user to another X++

Hi All,

Below job can be used to copy user groups from one user to another.

Relevant for AX 2009, AX 4.0 and Axapta 3.0

static void AXEcopyUserGroup(Args _args)
{
    usergrouplist usergrouplist,ins;
    userid src,dest;
    dialogfield srcdf,destdf;
    dialog dialog;
    ;
    dialog = new dialog("copying user's user groups");
    srcdf = dialog.addField(typeid(userid),"Source");
    destdf = dialog.addField(typeid(userid),"Destination");
    

    
    if(dialog.run())
    {
        src = srcdf.value();
        dest = destdf.value();
        if(src!="" && dest !="")
        {
            ttsbegin;
            while select * from usergrouplist
            where usergrouplist.userId == dest
            {
                info(strfmt("Before update: '%1'-'%2'",usergrouplist.userid,usergrouplist.groupId));
            }
            
            info("Deleting user groups from destination user");
            delete_from usergrouplist
                where  usergrouplist.userId == dest;
                
                
            info("Copying user groups to destination user");
            while select * from usergrouplist
            where usergrouplist.userId == src
            {
                info(strfmt("source user: '%1'-'%2'",usergrouplist.userid,usergrouplist.groupId));
                ins.clear();
                ins.data(usergrouplist);
                ins.UserId = dest;
                ins.insert();
            }
            ttscommit;
            info("User groups has been copied!");
        }
    }
    else
    {
        info("Canceled by user");
    }
    
   
}

No comments:

Post a Comment