CRM 4.0 Workflow links - add ID to attribute

CRM 4.0 Workflow links - add ID to attribute

Post by Tony » Fri, 12 Sep 2008 17:50:19


i;
I recently read this thread and see the problems with automatically adding the entity id when creating a case, opporunity, contact etc.
Here is code for a workflow plugin for getting the guid of an incident. It can easily be modified to use other entities. Once completed simply create a workflow and reference this custom workflow plugin from it. I wish I could say it was all my own work but I just changed the code a bit from Aamir Bashir CRM blog
-much thanks to him for doing most of the work.

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Workflow;
using Microsoft.Crm.Workflow.Activities;
using Microsoft.Crm.Sdk.Query;

namespace EntityURL
{
[CrmWorkflowActivity("Add ID to Case URL field")]

public partial class Activity1 : SequenceActivity
{

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{


IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));

IWorkflowContext context = contextService.Context;

ICrmService crmService = context.CreateCrmService();

WhoAmIRequest systemUserRequest = new WhoAmIRequest();

WhoAmIResponse systemUser = (WhoAmIResponse)crmService.Execute(systemUserRequest);

ColumnSet cols = new ColumnSet();
//Getting the ID of entity which unqiue itself

Guid guidID = new Guid(this.objectID.Value.ToString());

// i am using before GUID and "}" after.This field will hold the GUID of account

String Number = "{" + guidID + "}";

TargetRetrieveIncident target = new TargetRetrieveIncident();


target.EntityId = guidID;
RetrieveRequest getIncident = new RetrieveRequest();

getIncident.ReturnDynamicEntities = true;

getIncident.Target = target;

getIncident.ColumnSet = new AllColumns();

RetrieveResponse retrieved = (RetrieveResponse)crmService.Execute(getIncident);

DynamicEntity regardingIncident = (DynamicEntity)retrieved.BusinessEntity;
//I am using custom field to hold the value of UID.

if (regardingIncident.Properties.Contains("ba_entityurl"))
{

regardingIncident.Properties["ba_entityurl"] = Number;

}

else
{

regardingIncident.Properties.Add(new StringProperty("ba_entityurl", Number));
}

crmService.Update(regardingIncident);


return ActivityExecutionStatus.Closed;

}


public static DependencyProperty objectIDProperty = DependencyProperty.Register("objectID", typeof(Lookup), typeof(Activity1));



[CrmInput("objectID")]

[CrmReferenceTarget("incident")]


public Lookup objectID
{

get
{

return (Lookup)base.GetValue(objectIDProperty);

}

set
{