We’ve developed some resources to help you work effectively from home during COVID-19 Click to learn more

Update usergroup programmatically

Hi,

I'm trying to update a users primary group programatically.

I have one user who has a primary group and no other groups. When I change the primary group using this (pseudo)code:

soUser.GroupID = someGroupID;
soUser.Save();

I get the following result:

Re: Update usergroup programmatically

The user-groups are stored in the UserGroupLink table.
You need to alter the usergrouplink record that corresponds to the primary group.
The COM API is currently lacking the methods to modify the user-group memberships.

By: Christian Mogensen 13 May 2008

Re: Update usergroup programmatically

Hello Christian,
Thank you for your response.

Yes, I noticed that and I tried another approach:
After I change the user group I added an SQL-query:

Delete from crm5.UserGroupLink where assoc_id = currentAssocID and usergroup_id = oldGroupID

This works as far as deleting the link in the database. What I now have left in the database is the following:

- One row in associate where group_idx is the new primary group
- One row in usergrouplink where usergroup_id is the new primary group.

No row that includes the old primary group - just the way I want it.

Still, when I look at the user in SOAdmin the users primary group is the correct one, but the old primary group is still there as an other group. Why is this?

When I try to remove the other group in SOAdmin nothing happens - it pops right back to other groups when I delete it. It's as if it has lost it's connection to the user in the databas (which it has - no row exists with that connection) but it is cached in SOAdmin or something. Is there any place in the database where a users usergroups are stored other than associate and usergrouplink? How can I solve this problem?

Best regards
Jenny

By: Jenny Lidén 13 May 2008

Re: Update usergroup programmatically

Hello Christian!

I don't mean to stress you in any way, but this is a quite urgent matter to us.
Do you have any ideas of a solution to my problem?
Best Regards
Jenny

By: Jenny Lidén 14 May 2008

Re: Update usergroup programmatically

Have you tried to deleting the cache on the client and restarting Admin (I guess you have already restarted Admin after the change)?

By: Atle Bjerck 15 May 2008

Re: Update usergroup programmatically

Hello Atle!

That did the trick, thank you so much!

I really appreciate your help, I'm not very familiar with SuperOffices cache...

Best Regards
Jenny Lidén

By: Jenny Lidén 15 May 2008

Re: Update usergroup programmatically

I do not think anyone normally cares much about the cache...

I have had some problems when I have added certain system information into the superoffice database outside Admin (both Preferences and Usergroups). It seems that Admin doesn't invalidate its caches the same way as the client does - it probably supposes that no one else is changing this information.

I therefore always delete the cache for these tables before I do the change outside Admin, and then Admin is updated correctly. If you dont delete the cache and do other changes in Admin after your update, your changes could be lost...

Remember that on new clients the Cache doesn't reside under \Windows\SOCache anymore, but under the users Local Settings directory... Smile

Atle

By: Atle Bjerck 16 May 2008

RE: Update usergroup programmatically

Sorry to bring up an old topic.

 

I was trying to add secondary usergroups via CRMScript, but it seems i can only modify the primary one, and not get or modify secondary ones - so i guess the methods are still lacking, even in version 9?

 

 

By: Dennis Aagaard Mortensgaard 26 Aug 2020

RE: Update usergroup programmatically

You could use the NSUserAgent in CRMScript to accomplish this I guess. I think this only works Onsite, as the UserAgent is restricted in Online?

By: Stian Andre Olsen 27 Aug 2020

RE: Update usergroup programmatically

Hi Dennis!

Usergroup management should probably not be done in CRMScripts. That said, in the API's, secondary groups are called OtherGroups, and management of such is only allowed in onsite installations.

Assuming you are in an onsite installation:

Void AddSecondaryUserGroup(Integer userId, Integer userGroupId)
{
  NSUserAgent ua;

  // get all user groups
  NSUserGroup[] userGroups = ua.GetAllUserGroups(false); // false => do not get deleted user groups

  // get a user...  
  NSUser user = ua.GetUser(userId);

  // get users primary usergroup.
  NSUserGroup primaryGrp = user.GetUserGroup();
  Integer priGrpId = primaryGrp.GetId();
  
  // simple validation
  if(priGrpId == userGroupId)
    throw "You may not assign users primary group to the secondary group collection!";
  
  // get users secondary userGroups
  NSUserGroup[] secondaryGrps = user.GetOtherGroups();

  // find the secondary user group by id and 
  // add it to users secondary groups
  for(Integer i = 0; i < userGroups.length(); i++)
  {
    // do not add primary group to secondary groups
    if(userGroupId == userGroups[i].GetId())
    {
      secondaryGrps.pushBack(userGroups[i]);
      break;
    }
  }

  // assign the secondary user groups to the user
  user.SetOtherGroups(secondaryGrps);

  // save the user.
  user = ua.SaveUser(user);  
}

 

 

By: Tony Yates 27 Aug 2020

RE: Update usergroup programmatically

Well the issue is, if you are trying to mass update installations with 100+ users, it would be great to be able to mass add or remove users to a specific secondary group via scripting (in online)

But i got the answer - it's only possible in onsite installations.

By: Dennis Aagaard Mortensgaard 23 Sep 2020