Thursday, January 31, 2013

Create and Assign a team when a new record is created in MS CRM 2011



More often we will need to create a Team when a new record is created in MS CRM and assign the team back to same record. This can be easily done through two plugins:
  • Plugin 1: Create a Team
  • Plugin 2: Assign the Team to Record
In some case we will not have the luxury to have this as two plugins and confined to achieve them through single Plugin (or through Custom development). But we will often get “Security Role Privilege Error” when trying to achieve these through single plugin. This happens as it takes some time for the privileges to propagate to the newly created team. 
To resolve this issue we need to delay and check the application until the privilege has been assigned.
Here are steps for achieving this through single plugin:
 
1.       Create Team based on the newly created record.
2.       Assign Security role to the created Team.
3.       Check if the required privilege is assigned to team and delay the application until 
        the privilege is set (see below).
 
bool teamLacksPrivilege = true;            
while (teamLacksPrivilege)
{
       RetrieveTeamPrivilegesRequest retrieveTeamPrivilegesRequest = 
       new RetrieveTeamPrivilegesRequest
       {
                    TeamId = _teamId
                }; 
     RetrieveTeamPrivilegesResponse retrieveTeamPrivilegesResponse =
       (RetrieveTeamPrivilegesResponse)_service.Execute(
       retrieveTeamPrivilegesRequest);
 
     foreach (RolePrivilege rp in 
        retrieveTeamPrivilegesResponse.RolePrivileges)
        {
            if (rp.PrivilegeId == readAccountPrivilege.Id)
            {
                 teamLacksPrivilege = false;
                 break;
            }
            else
            {
                 System.Threading.Thread.CurrentThread.Join(500);
            }
        }
}
 
4.       Assign the Team to created Record.

2 comments:

  1. Have someone ever verified this code is working?

    In my case, retrieveTeamPrivilegesResponse has all neccessary privileges from security role. But in forth step, assignment is failing due to lack of prvReadXXX privilege.

    ReplyDelete
  2. Hello!!! This works for me.
    Did you check for privilege prvReadXXX prior to the step 3 mentioned in this blog. You need to retrieve the prvReadXXX of the record and if prvReadXXX not available then we need to delay Thead until prvRead is assigned to the record. Here is the code which needs to be added prior to step 3.

    // Create a query expression to find the prvReadXXXPrivilege.
    QueryExpression queryReadXXXPrivilege = new QueryExpression
    {
    EntityName = "privilege",
    ColumnSet = new ColumnSet("privilegeid", "name"),
    Criteria = new FilterExpression()
    };
    queryReadXXXPrivilege.Criteria.AddCondition("name",
    ConditionOperator.Equal, "prvReadXXX");

    // Retrieve the prvReadXXX privilege.
    Entity readXXXPrivilege = _serviceProxy.RetrieveMultiple(
    queryReadXXXPrivilege)[0];


    This Retrieved ReadXXXPrivilege Id needs to be checked at below code at step 3 for delaying the thread:
    if (rp.PrivilegeId == readXXXPrivilege.Id)

    Thanks!
    Anitha V

    ReplyDelete