Microsoft Dynamics CRM 2011

Microsoft Dynamics CRM 2011

Monday, July 28, 2014

PlugIns - How to GET A FIELD VALUE FROM SOME CRM ENTITY - HAVING ITS GUID in a CRM 2013 C# Workflow

by Carmel Schvartzman
  1. This is the Building Block C# code to GET A FIELD VALUE FROM SOME CRM ENTITY - HAVING ITS GUID in a CRM 2013 C# Workflow.
  2. The present code relates to a custom  Plugin / Workflow in Dynamics CRM 2011/2013. Once you have developed your custom workflow, you deploy the assembly containing the plugin in the CRM web server, and register it with the Plugin Registration Tool of the CRM 2011 SDK.
  3. Then you use this code to set up the Xrm Organization Service that you need to interact with the CRM organization:

    protected override void Execute(CodeActivityContext context)
            {
                #region GET THE EXECUTION CONTEXT FROM THE SERVICE PROVIDER VARIABLE :
                execContext = context.GetExtension<IExecutionContext>();
                workflowContext =
                    context.GetExtension<IWorkflowContext>();
                serviceFactory =
                    context.GetExtension<IOrganizationServiceFactory>();
                service =
                    serviceFactory.CreateOrganizationService(workflowContext.UserId);
                     
                ITracingService tracingService = context.GetExtension<ITracingService>();
               #endregion
                ///////////////////////////////////////////////////////////////////////////////////////////
                // Business Logic:
                ExecuteYourBussinessLogicMethod(context, workflowContext, tracingService);
                //////////////////////////////////////////////////////////////////////////////////////////
            }

  4. Then having already working the CRM Organization Service, and having also the GUID id of the required entity object, you can get the value for some selected field, as follows:


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////   For this building Block  you MUST have the entity GUID    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
////  This is the Guid ID of the required record : Guid entityId = new Guid();
////  The fields you need to fetch : ColumnSet fields = new ColumnSet(                new string[]                 {                     "new_name", "new_custom_field"                 }                );

            Entity customEntity = service.Retrieve("new_custom_entity", entityId, fields);


////////  Get safely the source value and copy it to the target field in another entity: if (customEntity.Attributes.ContainsKey("new_custom_field"))                {                    targetEntity["target_field"] = Convert.ToString(customEntity.Attributes["new_custom_field"]);                }





That's all...Enjoy Dynamics CRM 2013!!!


כתב: כרמל שוורצמן