Thursday, August 20, 2009

How to copy the favorites from one user to another

The favorites menu is a popular thing in Dynamics Ax. It has been around for quite some time now in Ax, in one form or another.
A common request that I get from users is: "Hey, I like this guy's favorites menu, can't I copy it to me own settings?'.
The answer in standard Ax is: No, you can't. But as with almost anything in Ax: It can be done with a little modification.

In order to be able to copy the favorites menu, you have to know where it is stored. This is done in the system table SysPersonalization, in the 'Buffer' field. We are looking for records in this table with the elementtype of UserMenu.

This next job will copy the favorites menu from user A to user B. (If user B has a favorities menu setup, it will be lost, as it first gets deleted.)

server static void FavoritesJob(Args _args)
{ SysPersonalization FromSysPersonalization;
SysPersonalization ToSysPersonalization;
UserId FromUserId='UserA';
UserId ToUserId='UserB';
;

ttsbegin;

// step 1 - delete current favorites menu from user
while select forupdate ToSysPersonalization
where ToSysPersonalization.ElementType==UtilElementType::UserMenu
&& ToSysPersonalization.UserId==ToUserId
{
ToSysPersonalization.doDelete();
}

// step 2 - duplicate from user A
while select FromSysPersonalization
where FromSysPersonalization.UserId==FromUserId
&& FromSysPersonalization.ElementType==UtilElementType::UserMenu
{
ToSysPersonalization.data(FromSysPersonalization);
ToSysPersonalization.UserId=ToUserId;
ToSysPersonalization.doInsert();
}

ttscommit;
}

(use at own risk)

Note: I have used the methods dodelete() and doinsert(), instead of delete() and insert(). Reason: You would run into trouble with access permissions otherwise.

9 comments:

  1. But how can I use the "job" thing?

    ReplyDelete
  2. If you dont have authorization to open aot you can't.
    If you have, edit job's FromUserId and ToUserId variables then hit the F5 .

    ReplyDelete
  3. works on 2012 as well.

    ReplyDelete
  4. Many thanks works great !!!

    ReplyDelete
  5. You can also add table SysLastValue to it (like I did) in order to copy queries etc.

    ReplyDelete
    Replies
    1. Can you post your working code to copy queries as well ?

      Delete
  6. The favorites are copied but not the actual queries linked to those favorites, right ?

    ReplyDelete
  7. Thanks for this, lost a users favorites after a crash and this worked great and saved time. Appreciated!

    ReplyDelete