Manipulating Tasks

I have a client that requires a separate application integrated with CRM. They need to record when a third party assigns a number to a customer they have. The third party will first assign a temporary number to the customer on first registration with their web service. After a number of days the third party will have verified the registration by snail mail and will issue a permanent number.

On first registration the return value of the web service being called is the temporary number, we record this and send it to CRM to be put against the customer's record. We then poll another web service with the temporary number until a permanent number is returned. This is then recorded against the CRM record.

However my client has manual process to go through before the customer record can be updated and they don't want the integration code to automatically update the custom field. To this end they want tasks created for staff to phone the customer and talk to them as part of the process. They also want to be able to track when a customer has not received their permanent number in due course.

To this end I need to create a number of tasks. First is to inform the CRM operator that a new customer has registered on the public facing website (not automatically brining the details across as hoaxers can enter crap). Second is to log a task when the customer is registered with the third party and their temp number is assigned. Third is to raise a task when the third party comes back with a permanent number.

Due to the way they want to process this information and report on it when the second task is raised the operator assigns it to the account it references (needs a human to do this to mitigate matching problems). When the integration code receives the permanent code it closes off the temp code task and opens a task for the permanent one (again phone calls have to be made before the record can be updated). In reporting we will simply run off all tasks that are open with a certain subject and are more than x days old.

As this requires me programmatically manipulate tasks (something I had not done before) I thought I would post my solution here to show how easy it is. It took me more time to write this post that produce this partial solution.

public Guid CloseTask(Guid taskid, string newMessage)


account theAccount;

task theTask = (task) _service.Retrieve(EntityName.task.ToString(), taskid, new AllColumns());

theAccount = (account) _service.Retrieve(EntityName.account.ToString(),theTask.regardingobjectid.Value, new AllColumns());

SetStateTaskRequest closeTask = new SetStateTaskRequest();

closeTask.EntityId = theTask.activityid.Value;

closeTask.TaskState = TaskState.Completed;

closeTask.TaskStatus = -1; //To allow CRM to decide

SetStateTaskResponse response = (SetStateTaskResponse) _service.Execute(closeTask);

task t = new task();

t.description = newMessage;

t.subject = "Task Example";

t.regardingobjectid = new Lookup();

t.regardingobjectid.type = EntityName.account.ToString(); =;

t.regardingobjectid.Value = theAccount.accountid.Value;

return _service.Create(t);


Please note that this is not a copy of the production code, this is just what I have done today on my laptop given that my client is closed for the Glasgow Fair. For starters I need to define a tight list of columns for the returned objects and not tie up bandwidth bringing across 99% of both objects when I am not using them.

Obviously the thing to note is the use of the SetStateTaskRequest object to change the state of the task rather than changing the statuscode member of the task object. There is one of these special classes for each of the entities that you would use in CRM and changing the state of them requires this method in order to do it properly.

My test bed first creates a simple task, then when I am running in break I can alter the task created to point to an account, I then run this function above and a new task is created, taking the original account reference with it.

In production the system storing new registrations from my client's web site will hold a table that will have the temp number, the original task guid and the date it was all created as well as space for the permanent number when it arrives. This allows me to pass in the task guid and take the matched account to the next task that is opened.

Any comments on this or any way you think it could be done better I would be delighted to hear.

1 Response to “Manipulating Tasks”:

  1. Anonymous says:

    Hi ,
    i want to get all the attributes display name of an entity in a dropdown list in .net using can i get the entity's attributes display name listed using metadata service.Do u have any code?