protected override void Execute(CodeActivityContext executionContext) { //Create the tracing service ITracingService tracingService = executionContext.GetExtension <ITracingService>(); //Create the context IWorkflowContext context = executionContext.GetExtension <IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); tracingService.Trace("Creating Account"); tracingService.Trace("Creating Account"); Entity entity = new Entity("account"); entity.Attributes["name"] = AccountName.Get <string>(executionContext); Guid entityId = service.Create(entity); tracingService.Trace("Account created with Id {0}", entityId.ToString()); tracingService.Trace("Create a task for the account"); Entity newTask = new Entity("task"); newTask["subject"] = TaskSubject.Get <string>(executionContext); newTask["regardingobjectid"] = new EntityReference("account", entityId); Guid taskId = service.Create(newTask); tracingService.Trace("Task has been created"); Entity updateTask = new Entity("task"); updateTask.Id = taskId; updateTask["subject"] = UpdatedTaskSubject.Get <string>(executionContext); service.Update(updateTask); tracingService.Trace("Task has been updated"); }
protected override void Execute(CodeActivityContext executionContext) { //Create the tracing service ITracingService tracingService = executionContext.GetExtension <ITracingService>(); //Create the context IWorkflowContext context = executionContext.GetExtension <IWorkflowContext>(); IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>(); IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); tracingService.Trace("Creating Account"); Account entity = new Account(); entity.Name = AccountName.Get <string>(executionContext); Guid entityId = service.Create(entity); tracingService.Trace("Account created with Id {0}", entityId.ToString()); tracingService.Trace("Create a task for the account"); Task newTask = new Task(); newTask.Subject = TaskSubject.Get <string>(executionContext); newTask.RegardingObjectId = new EntityReference(Account.EntityLogicalName, entityId); Guid taskId = service.Create(newTask); tracingService.Trace("Task has been created"); tracingService.Trace("Retrieve the task using QueryByAttribute"); QueryByAttribute query = new QueryByAttribute(); query.Attributes.AddRange(new string[] { "regardingobjectid" }); query.ColumnSet = new ColumnSet(new string[] { "subject" }); query.EntityName = Task.EntityLogicalName; query.Values.AddRange(new object[] { entityId }); tracingService.Trace("Executing the Query for entity {0}", query.EntityName); //Execute using a request to test the OOB (XRM) message contracts RetrieveMultipleRequest request = new RetrieveMultipleRequest(); request.Query = query; Collection <Entity> entityList = ((RetrieveMultipleResponse)service.Execute(request)).EntityCollection.Entities; //Execute a request from the CRM message assembly tracingService.Trace("Executing a WhoAmIRequest"); service.Execute(new WhoAmIRequest()); if (1 != entityList.Count) { tracingService.Trace("The entity list was too long"); throw new InvalidPluginExecutionException("Query did not execute correctly"); } else { tracingService.Trace("Casting the Task from RetrieveMultiple to strong type"); Task retrievedTask = (Task)entityList[0]; if (retrievedTask.ActivityId != taskId) { throw new InvalidPluginExecutionException("Incorrect task was retrieved"); } tracingService.Trace("Retrieving the entity from IOrganizationService"); //Retrieve the task using Retrieve retrievedTask = (Task)service.Retrieve(Task.EntityLogicalName, retrievedTask.Id, new ColumnSet("subject")); if (!string.Equals(newTask.Subject, retrievedTask.Subject, StringComparison.Ordinal)) { throw new InvalidPluginExecutionException("Task's subject did not get retrieved correctly"); } //Update the task retrievedTask.Subject = UpdatedTaskSubject.Get <string>(executionContext); service.Update(retrievedTask); } }