views:

414

answers:

2

I need to get the GUID of a record from Dynamics CRM 4 workflow. It's a record that's created during the workflow's execution. I thought of writing a w/f assembly that accepts a lookup and returns a string containing the GUID (which is enough for my purpose). However, the Lookup in the assembly must specify the type of entity. As the requirement exists for many entities already, and for many others that will e created by the customer without notice, this won't work for me.

Is there any way to do this easily, or any way to create a lookup parameter for a workflow assembly that will accept any entity type?

+1  A: 

Your best bet would be to create a post create plugin that will set the GUID into a custom field (new_myguid) and then your workflow will be able to read the field as soon as it has been executed.

Focus
Yeah, that's what I went with in the end.
Dan Crowther
+1  A: 

You are right that you cannot access an entity id from the workflow designer natively and that a custom activity would be limited to a single entity per input property.

You could implement Focus's suggestion, but you'd need that custom attribute and plugin on each entity as well.

I think I'd probably do a custom activity and have multiple input properties that all output to a single output property.

Something like this:

[CrmInput("Contact")]
[CrmReferenceTarget("contact")]
public Lookup Contact
{
    get { return (Lookup)GetValue(ContactProperty); }
    set { SetValue(ContactProperty, value); }
}
public static readonly DependencyProperty ContactProperty =
    DependencyProperty.Register("Contact", typeof(Lookup), typeof(YourActivityClass));

[CrmInput("Account")]
[CrmReferenceTarget("account")]
public Lookup Account
{
    get { return (Lookup)GetValue(AccountProperty); }
    set { SetValue(AccountProperty, value); }
}
public static readonly DependencyProperty AccountProperty =
    DependencyProperty.Register("Account", typeof(Lookup), typeof(YourActivityClass));

[CrmOutput("Entity ID")]
public string EntityID
{
    get { return (string)GetValue(EntityIDProperty); }
    set { SetValue(EntityIDProperty, value); }
}
public static readonly DependencyProperty EntityIDProperty =
    DependencyProperty.Register("EntityID", typeof(string), typeof(YourActivityClass));

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
    Lookup[] lookups = new[] { Contact, Account };
    foreach (Lookup lookup in lookups)
    {
        if (lookup != null && lookup.Value != Guid.Empty)
        {
            EntityID = lookup.Value.ToString();
            break;
        }
    }

    return ActivityExecutionStatus.Closed;
}
Corey O'Brien
That won't work because the entities won't be known ahead of time.
Dan Crowther