A Trick to Identify in Code which User has been Changed in the User Administration Screens

1 minute read time.

Working within the System Admininstration screens can present its own challenges. Please see the article "The SQL Tab Clause and the User Admin screens" for an example of what I mean.

Consider if I am working within a the System Administration screen and I want to trigger a set of behaviour when some aspect of the user is changed. I might want to update all opportunities belonging to the user if their primary team is changed.

We face a challenge in identifing the user.

If we use CRM.GetContextInfo("user","user_userid") then this returns the current logged user (typically the System Administrator).

If we look at the screens properties then we can see where the information we need is in the URL of the screen

http://[servername]/[installname]/eware.dll/Do?SID=194680057128111 &Act=803 &Mode=1 &CLk=T &Key0=11 &Key11=7

The ID of the user being changed is in the Key Value "Key11". Please refer to the article "Long List of Key Values for Sage CRM" . Key11 is the UserAdminID key.

In create scripts we could grab the value in the URL by using Values("Key11"), but as I have noted in the article "More about the Values and FormValues collections"

Tablelevel scripts can't access the information in the QueryString. This means if we want to know which record is being updated then we will have to use another method.

The easiest technique to use is the WhereClause.

A simple test of the WhereClause context when updating the User Admin screen is to use this UpdateRecord event function.

[code language="javascript"]
function UpdateRecord()
{
Valid = false;
ErrorStr = "User updated is:"+WhereClause;
}
[/code]

Other examples of how the WhereClause can be used are contained in the articles

An example to update the opportunities belonging to user with their new team information would look like this:

[code language="javascript"]
function UpdateRecord()
{
var myUser = CRM.FindRecord("users",WhereClause);
if (myUser.user_primarychannelid != Values("user_primarychannelid"))
{
var strSQL = "oppo_status = 'In Progress' and oppo_assigneduserid="+myUser.user_userid;
var myOppoRecord = CRM.FindRecord('opportunity', strSQL);
while (!myOppoRecord.EOF)
{
myOppoRecord.oppo_channelid = Values("user_primarychannelid");
myOppoRecord.NextRecord();
}
myOppoRecord.SaveChanges();
}
}
[/code]