Showing posts with label 2013. Show all posts
Showing posts with label 2013. Show all posts

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

Wednesday, 2 September 2015

Important javascript Functions for CRM

Important javascript Functions for CRM


Getting the value of optionset


Xrm.Page.getAttribute("field logical name").getValue();

Getting the Text of optionset

Xrm.Page.getAttribute("fieldlogical name").getText();

Getting the GUID of Lookup


Xrm.Page.getAttribute("fieldlogical name").getValue()[0].id;

Getting the Text of Lookup


Xrm.Page.getAttribute("fieldlogical name ").getValue()[0].name;

Getting the value of single line text


Xrm.Page.getAttribute("fieldlogical name").getValue();

Setting value to the field


Xrm.Page.getAttribute("fieldlogicalname").setValue("Value we want to set");

Getting Entity Name


Xrm.Page.data.entity.getEntityName();

For refresh the Form


Location.Reload();

Save the page

Xrm.Page.data.entity.save();