CRM Crate

How to read and write values in different fields using plugins?

We will learn how to read / get and write / set values in various different fields on Microsoft Dynamics 365 CRM using plugins. A field in Microsoft CRM is the equivalent of a column in a database table, here the only difference is that an entity correlates to a table. We can perform all types of operations using the plugins where there can be scenarios where we have to set or get values from an entity field to suffice the operation logic. In the below examples, we have performed get and set operations of multiple fields in the retrieved Target entity.

Read & Write values in string or text field

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;

namespace CRM_Crate___My_Sample_Plugin
{
    public class Plugin : IPlugin
    {

        //Main Execute Method.
        public void Execute(IServiceProvider serviceProvider)
        {

            //Initializing Service Context.
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);
            try
            {

                //Defining Entity Object.
                Entity eTarget = null;
                if (context.MessageName == "Create")
                {

                    //Retrieving Target Entity.
                    eTarget = (context.InputParameters.Contains("Target") && context.InputParameters["Target"] != null) ?
                    context.InputParameters["Target"] as Entity : null;

                    //Triggering The Logic If Target Is Not Null.
                    if (eTarget != null)
                    {

                        //Reading Value From String Field.
                        string readField = eTarget.GetAttributeValue<string>("fieldschemaname");

                        //Setting Value In String Field.
                        eTarget.Attributes.Add("fieldschemaname", "Any Text Value");

                        //Sending Update Request.
                        service.Update(eTarget);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
    }
}

Read & Write values in Option Set field

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;

namespace CRM_Crate___My_Sample_Plugin
{
    public class Plugin : IPlugin
    {

        //Main Execute Method.
        public void Execute(IServiceProvider serviceProvider)
        {

            //Initializing Service Context.
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);
            try
            {

                //Defining Entity Object.
                Entity eTarget = null;
                if (context.MessageName == "Create")
                {

                    //Retrieving Target Entity.
                    eTarget = (context.InputParameters.Contains("Target") && context.InputParameters["Target"] != null) ?
                    context.InputParameters["Target"] as Entity : null;

                    //Triggering The Logic If Target Is Not Null.
                    if (eTarget != null)
                    {

                        //Reading Value From Option Set Field.
                        int readField = eTarget.GetAttributeValue<OptionSetValue>("fieldschemaname").Value;

                        //Setting Value In Option Set Field.
                        eTarget.Attributes.Add("fieldschemaname", new OptionSetValue(100));

                        //Sending Update Request.
                        service.Update(eTarget);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
    }
}

Read & Write values in Whole Number or Numerical field

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;

namespace CRM_Crate___My_Sample_Plugin
{
    public class Plugin : IPlugin
    {

        //Main Execute Method.
        public void Execute(IServiceProvider serviceProvider)
        {

            //Initializing Service Context.
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);
            try
            {

                //Defining Entity Object.
                Entity eTarget = null;
                if (context.MessageName == "Create")
                {

                    //Retrieving Target Entity.
                    eTarget = (context.InputParameters.Contains("Target") && context.InputParameters["Target"] != null) ?
                    context.InputParameters["Target"] as Entity : null;

                    //Triggering The Logic If Target Is Not Null.
                    if (eTarget != null)
                    {

                        //Reading Value From Whole Number Field.
                        int readField = eTarget.GetAttributeValue<int>("fieldschemaname");

                        //Setting Value In Whole Number Field.
                        eTarget.Attributes.Add("fieldschemaname", 100);

                        //Sending Update Request.
                        service.Update(eTarget);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
    }
}

Read & Write values in Lookup field

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;

namespace CRM_Crate___My_Sample_Plugin
{
    public class Plugin : IPlugin
    {

        //Main Execute Method.
        public void Execute(IServiceProvider serviceProvider)
        {

            //Initializing Service Context.
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);
            try
            {

                //Defining Entity Object.
                Entity eTarget = null;
                if (context.MessageName == "Create")
                {

                    //Retrieving Target Entity.
                    eTarget = (context.InputParameters.Contains("Target") && context.InputParameters["Target"] != null) ?
                    context.InputParameters["Target"] as Entity : null;

                    //Triggering The Logic If Target Is Not Null.
                    if (eTarget != null)
                    {

                        //Reading Value From Lookup Field.
                        EntityReference readField = eTarget.GetAttributeValue<EntityRefrence>("fieldschemaname");

                        //Setting Value In Lookup Field.
                        Guid recordId = new Guid("8a2eab03-faa6-ea11-a812-000d3a8b66df"); 
                        eTarget.Attributes.Add("fieldschemaname", new EntityReference("entityschemaname", recordId));

                        //Sending Update Request.
                        service.Update(eTarget);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
    }
}

Read & Write values in Date Time field

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;

namespace CRM_Crate___My_Sample_Plugin
{
    public class Plugin : IPlugin
    {

        //Main Execute Method.
        public void Execute(IServiceProvider serviceProvider)
        {

            //Initializing Service Context.
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);
            try
            {

                //Defining Entity Object.
                Entity eTarget = null;
                if (context.MessageName == "Create")
                {

                    //Retrieving Target Entity.
                    eTarget = (context.InputParameters.Contains("Target") && context.InputParameters["Target"] != null) ?
                    context.InputParameters["Target"] as Entity : null;

                    //Triggering The Logic If Target Is Not Null.
                    if (eTarget != null)
                    {

                        //Reading Value From DateTime Field.
                        DateTime readField = eTarget.GetAttributeValue<DateTime>("fieldschemaname");

                        //Setting Value In DateTime Field.
                        DateTime anyDateTime = DateTime.Now;
                        eTarget.Attributes.Add("fieldschemaname", anyDateTime);

                        //Sending Update Request.
                        service.Update(eTarget);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
    }
}

Read & Write values in Currency field

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;

namespace CRM_Crate___My_Sample_Plugin
{
    public class Plugin : IPlugin
    {

        //Main Execute Method.
        public void Execute(IServiceProvider serviceProvider)
        {

            //Initializing Service Context.
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);
            try
            {

                //Defining Entity Object.
                Entity eTarget = null;
                if (context.MessageName == "Create")
                {

                    //Retrieving Target Entity.
                    eTarget = (context.InputParameters.Contains("Target") && context.InputParameters["Target"] != null) ?
                    context.InputParameters["Target"] as Entity : null;

                    //Triggering The Logic If Target Is Not Null.
                    if (eTarget != null)
                    {

                        //Reading Value From Money Field.
                        Money readField = eTarget.GetAttributeValue<Money>("fieldschemaname");
                        Decimal readFieldInDecimal = eTarget.GetAttributeValue<Money>("fieldschemaname").Value;

                        //Setting Value In Money Field.
                        Money anyMoneyValue = new Money(1000);
                        eTarget.Attributes.Add("fieldschemaname", anyMoneyValue);

                        //Sending Update Request.
                        service.Update(eTarget);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
    }
}

CRM Crate

All In One Platform For Learning Microsoft CRM.

Facebook
Twitter
LinkedIn
5 3 votes
Article Rating
Subscribe
Notify of
5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Understanding plugin images in Microsoft Dynamics CRM - CRM Crate
3 years ago

[…] Update different types of fields using a plugins. […]

Ramesh Jadhav
3 years ago

This was very helpfull. Great.

Model-Driven App | Upload, Download & Delete a File using Power Automate - CRM Crate
1 year ago

[…] file attribute / column is used for storing file data up to a specified maximum size. A custom or customizable table can […]

How to retrieve multiple records in plugin using Query Expression - CRM Crate
21 days ago

[…] Read & Write values in fields using Plugins […]

Read, update, create and delete record using plugins - CRM Crate
21 days ago

[…] Reading & writing in fields using a plugin […]

error: CRM Crate Security Engine - Disabled Right Click & Selection!

Congratulations!

Well Done,
Welcome to CRM Crate

Stay tuned with us and get all latest updates and learning in Microsoft CRM and related techonologes.