Showing posts with label CRMService. Show all posts
Showing posts with label CRMService. Show all posts

Thursday, 13 March 2008

Retrieve Team details from a User Id in CRM 3.0

In CRM 3.0, I needed to get some of the team information of a specified user which I could not find any sample code on how to do this but with the help of the SDK, I wrote this little method to retrieve the team id or list of teams ids that a specified user belongs to. The code is written in C#.

The code makes a call to the web service and retrieves the teamid using the Request and Retrieve classes RetrieveTeamsSystemUserRequest & RetrieveTeamsSystemUserResponse on the team entity.

The user performing this action from within CRM must have access rights on the team entity instance and thats why I used the best practice impersonation technique.

In this case, I’m only retrieving the teamid attribute value (Guid) but you can easily extend it to retrieve any of the following team attributes by creating a ColumnSet and set the attributes to retrieve for this or each team:

businessunitid Specifies the ID of the business unit with which the team is associated.
createdby Specifies the ID of the user who created the team.
createdon Specifies the date and time when the team was created.
description Contains the description of the team.
emailaddress Specifies the e-mail address for the team.
importsequencenumber
modifiedby Specifies the ID of the user who last modified the team.
modifiedon Specifies the date and time when the team was last modified.
name Specifies the name of the team.
organizationid Specifies the ID of the organization associated with the team.
teamid Specifies the ID for the team.

The EntityId is basically the SystemUserId performing this action.

I then passed an instance of the RetrieveTeamsSystemUserRequest class as the request parameter in the web service Execute method.

Finally, extracting the Guid from the returned instance of the response class (RetrieveTeamsSystemUserResponse) after casting it to the team entity.


private Guid GetTeamId(Guid userId)

{

Guid teamid = Guid.Empty;

try

{

CrmService service = new CrmService();

service.Credentials = System.Net.CredentialCache.DefaultCredentials;

// impersonate the user who performed the action

service.CallerIdValue = new CallerId();

service.CallerIdValue.CallerGuid = userId;

// Retrieves the team id of which the specified user is a member

RetrieveTeamsSystemUserRequest retrieveTeam = new RetrieveTeamsSystemUserRequest();

ColumnSet tcs = new ColumnSet();

tcs.Attributes = new String[] {"teamid"};

retrieveTeam.EntityId = userId;

retrieveTeam.ColumnSet = tcs;

//Get the teams details of which the specified user is a member

RetrieveTeamsSystemUserResponse responseTeam = (RetrieveTeamsSystemUserResponse)service.Execute(retrieveTeam);

teamid = ((team)((responseTeam.BusinessEntityCollection.BusinessEntities.GetValue(0)))).teamid.Value;

}

catch(Exception ex)

{

// TODO: Log errors

}

return teamid;

}

Thursday, 6 March 2008

Create recurring tasks or activities in CRM 3.0

There was a troubleshooting general issue in Microsoft Dynamics CRM 3.0 in regards to creating recurring activities such as appointment, service activity or tasks which is NOT possible to do in CRM.

The Q&A can be found at the following link:

http://www.microsoft.com/dynamics/crm/using/troubleshooting/tsgeneral.mspx

How do I create recurring appointments and service activities?
A. You cannot create recurring appointments or service activities in Microsoft Dynamics CRM. Appointments and service activities are created one at a time. If a recurring appointment is created in Microsoft Dynamics CRM client for Microsoft Office Outlook, the recurrence will be lost after synchronization with Microsoft Dynamics CRM.


Anyhow, I had a requirement to allow the users to create recurring tasks in CRM 3.0 task activity in particular. It should be noted that this is a lot easier to do in CRM 4.0 as all you would need is a simple workflow to create the recurring tasks when the initial task is being created.

This added feature shall simply capture the start and end dates of the recurrence event. It also captures the recurring pattern (i.e. Daily, Weekly, Monthly, Quarterly or Annually).

I then added a number of attributes and a new tab in the task activity to capture the above info and based on that I created a callout to create the tasks based on the values entered in the above fields as shown below.



The following code snippet is taken from the callout showing how the tasks have been created based on the recurring pattern inserted by the user.

// if recurring is set to Yes and recurring dates are
also set then create the additional tasks

if(new_isrecurring && new_recurringstart != string.Empty && new_recurringend != string.Empty && new_recurringpattern != 0)
{
DateTime addRecurringStartDate = DateTime.Parse(new_recurringstart);

DateTime addRecurringEndDate = DateTime.Parse(new_recurringend);


while
(addRecurringStartDate < addRecurringEndDate)
{

// Re-Assign the new_recurringstart & new_recurringend
new_recurringstart = addRecurringStartDate.ToString();
new_recurringend = addRecurringEndDate.ToString();

switch
(new_recurringpattern)
{

case
1: //Daily
addRecurringStartDate = addRecurringStartDate.AddDays(1);

if(addRecurringStartDate <= addRecurringEndDate)
// Call the Create Task method here
CreateAdditionalTask(....);
break;

case 2: //"Weekly":
addRecurringStartDate = addRecurringStartDate.AddDays(7);

if(addRecurringStartDate <= addRecurringEndDate)
// Call the Create Task method here
CreateAdditionalTask(....);
break;

case 3: //"Monthly":
addRecurringStartDate = addRecurringStartDate.AddMonths(1);

if
(addRecurringStartDate <= addRecurringEndDate)

// Call the Create Task method here
CreateAdditionalTask(....);
break;

case
4: //"Quarterly":
addRecurringStartDate = addRecurringStartDate.AddMonths(3);

if(addRecurringStartDate <= addRecurringEndDate)
// Call the Create Task method here
CreateAdditionalTask(....);
break;

case 5: //"Annually":
addRecurringStartDate = addRecurringStartDate.AddYears(1);

if(addRecurringStartDate <= addRecurringEndDate)
// Call the Create Task method here
CreateAdditionalTask(....);
break;


Obviously, you will need to call the CRMService web service and create the tasks from your callout.

In addition, you may need to cater for bank holidays and weekends and exclude them from your calculation of days, weeks, monthly and years. There was not a need to do that in this particular instance but if you need a guideline on how to do it or have any further queries regarding any of the above then please post it here or email me with it.