views:

713

answers:

1

I'm creating a Dynamics CRM workflow assembly to be executed when a new Note is created on another record of any type. I need to be able to access a property Prop1 on that newly created Note entity to accomplish other tasks.

Previously I've only accessed values that were input from a field or from the user, but never on a property of a newly created entity. Any guidance would be appreciated.

UPDATE: This is regarding CRM 4.0.

More information while I wait: Ultimately, this workflow assembly will create an email that contains a link to the parent entity of the newly created Note record. The property I need to get is the AnnotationId. Once the Note record is created, I will be retrieving the ObjectId and ObjectTypeCode based on the AnnotationId of the newly created Note. (In case you were curious)

+5  A: 

Ok so if your using 4.0 custom workflows and not 3.0 callouts, you should add a workflow assembly, and use the Context service and executing context of your workflow to pull the values from the new note.

See the example below on how to access a record using the context service and the ID of your current context of execution (that should be your note)

    /// <summary>
    /// The Execute method is called by the workflow runtime to execute an activity.
    /// </summary>
    /// <param name="executionContext"> The context for the activity</param>
    /// <returns></returns>
    protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
    {

        // Get the context service.
        IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
        IWorkflowContext context = contextService.Context;

        // Use the context service to create an instance of CrmService.
        ICrmService crmService = context.CreateCrmService(true);
        BusinessEntity newNote = GetNote(crmService, context.PrimaryEntityId);

        string noteAttrib;

         noteAttrib = newNote.Properties.Contains("AnnotationId") ? ((Lookup)newNote.Properties["annotationid"]).name.ToString() : null;

        return ActivityExecutionStatus.Closed;
    }

GetNotes method would be a standard query for notes by Id through a CRM service call, here is an example slightly modified from MSDN to return a note:

private BusinessEntity getNote(ICrmService service, guid noteid)
{
  // Create the column set object that indicates the fields to be retrieved.
  ColumnSet cols = new ColumnSet();

  // Set the columns to retrieve, you can use allColumns but its good practice to specify:
  cols.Attributes = new string [] {"name"};

  // Create the target object for the request.
  TargetRetrieveAnnotation target = new TargetRetrieveAnnotation();

  // Set the properties of the target object.
  // EntityId is the GUID of the record being retrieved.
  target.EntityId = noteid;

  // Create the request object.
  RetrieveRequest retrieve = new RetrieveRequest();

  // Set the properties of the request object.
  retrieve.Target = target;
  retrieve.ColumnSet = cols;

  // Execute the request.
  RetrieveResponse retrieved = (RetrieveResponse)service.Execute(retrieve);
  return RetrieveResponse;
}
Tj Kellie
I want to thank you for this code sample, I really appreciate it. I have one further question. I'm not sure how to make a query for a BusinessEntity such as notes. I've never written a workflow like this before. Can you give me a hint regarding the inside of GetNotes()? Thanks again.
splatto
Ok, I edited the code to show the GetNotes() function, hope that helps you out. Workflow assemblies are one of my favorite features of the MSCRM sdk.
Tj Kellie
Excellent code sample, helped me out a lot
armannvg