Friday 18 December 2015

Retrieve Stage Name in the field for Business Process flow

Retrieve Stage Name in the field for Business Process flow


     Recently I have a requirement that we need to show the stage name in the field. I solved it. So here I wanna share how I have done this.

     For achieving this we need to create one workflow.

Step1: Create one field where you wanna get stage name. Let's say field name is "Stage".And insert it in to form.

Step2: Create a real-time workflow. Select "Starts when" as Record Created and Record Fields change like the following screenshot.


In the record, fields change the option to click on the select button and add following fields.


Step 3: Add an update step in the workflow. and click on "Set Properties" Button and click on your field(Stage). Then in your right side navigation bar, you will get one option like Look For. In that select following options.



Step4: Click add and press Ok. Save and Activate workflow. Then go to your entity UI. Then you will get a result like following.



Hope it helps you.
Thank you.





Tuesday 15 December 2015

Retrieve the status of an Entity Record through plugin

Retrieve the status of an Entity Record through plugin


For writing any plugin you should add following references


Microsoft.Crm.Sdk.Proxy
Microsoft.Xrm.Sdk
System.Runtime.Serialization

After that write plugin steps and Register Image like
 if (context.PreEntityImages.Contains("PreImage"))
    {
         Entity myPreImage = (Entity)context.PreEntityImages["PreImage"];
    }


Then write a logic like this 

var status = myPreImage.GetAttributeValue<OptionSetValue>("statecode").Value;

Here the Various States and Status reasons I mentioned here.


So If Record is giving value "0" Means it is active or inactive.
So Final Code is


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;

namespace ActiveEntity
{
    public class Class1 : IPlugin
    {
        public void Execute(IServiceProvider ServiceProvider)
        {
            try
            {
                IOrganizationServiceFactory ServiceFactory = (IOrganizationServiceFactory)ServiceProvider.GetService(typeof(IOrganizationServiceFactory));

                IExecutionContext context = (IExecutionContext)ServiceProvider.GetService(typeof(IExecutionContext));

                IOrganizationService Service = ServiceFactory.CreateOrganizationService(context.UserId);

                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {

                    if (context.PreEntityImages.Contains("PreImage"))
                    {
                        Entity entity = (Entity)context.InputParameters["Target"];
                        Entity myPreImage = (Entity)context.PreEntityImages["PreImage"];
                        if (myPreImage.Attributes.Contains("statecode"))
                        {
                            var status = myPreImage.GetAttributeValue<OptionSetValue>("statecode").Value;

                            if (status == 0)
                            {
                                //Your Custom Logic
                            }
                        }
                    }
                }
            }

            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
    }

}

After Build this plugin make sure Signing the plugin then Register Assembly and Step.
After that Register an Image.

Then you will get a result.

Hope it helps you,
Thank you,
Shiva Ram