GET/SET column(field) values using C# Plugin in D365 CE

Introduction: In this blog, you’ll learn how to GET and SET field values of different data types in D365 CE(CRM) using Plugin.

Single Line of Text and Multiple Lines of Text

GET

if (entity.Contains("vv_name") && entity["vv_name"] != null)
   {
       string name = (string)entity["vv_name"];
   }

SET

 entity["vv_name"] = "sample record";

Two Options

GET

if (entity.Contains("vv_interested") && entity["vv_interested"] != null)
  {
     bool interested = (bool)entity["vv_interested"];
  }

SET

entity["vv_interested"] = true;

Note: Yes: true and No: false

Option Set

GET

if (entity.Contains("vv_topic") && entity["vv_topic"] != null)
  {
     OptionSetValue topic = (OptionSetValue)entity["vv_topic"];
     int selectedTopicValue = topic.Value;
  }

SET

 entity["vv_topic"] = new OptionSetValue(772500004);

Whole Number

GET

if (entity.Contains("vv_age") && entity["vv_age"] != null)
  {
    int age = (int)entity["vv_age"];
  }

SET

entity["vv_age"] = 24;

Decimal Number

GET

if (entity.Contains("vv_dn") && entity["vv_dn"] != null)
  {
     decimal dn = (decimal)entity["vv_dn"];
  }

SET

entity["vv_dn"] = (decimal)67.45;

Floating Point Number

GET

if (entity.Contains("vv_fpn") && entity["vv_fpn"] != null)
  {
      double fpn = (double)entity["vv_fpn"];
  }

SET

entity["vv_fpn"] = 0.0078;

Date

GET

if (entity.Contains("vv_date") && entity["vv_date"] != null)
 {
    DateTime date = (DateTime)entity["vv_date"];
 }

SET

entity["vv_date"] = DateTime.Now;

Currency

GET

if (entity.Contains("vv_amount") && entity["vv_amount"] != null)
 {
   Money amount = (Money)entity["vv_amount"];
   decimal amountValue = amount.Value;
 }

SET

Money amount = new Money(); 
amount.Value = new decimal(780.67);
entity["vv_amount"] = amount;

Multi Select Option set

GET

if (entity.Contains("vv_country") && entity["vv_country"] != null)
 {
  OptionSetValueCollection countries = (OptionSetValueCollection)entity["vv_country"];

    foreach (var c in countries)
      {
         int country = c.Value; 
       }
  }

SET

 int[] country = { 772500002, 772500006, 772500007 };
 OptionSetValueCollection countries = new OptionSetValueCollection();

 foreach (var c in country)
  {
       countries.Add(new OptionSetValue(c));
  }

entity["vv_country"] = countries;

Lookup and Customer

GET

if (entity.Contains("vv_author") && entity["vv_author"] != null)
 {
     Guid authorId = ((EntityReference)entity["vv_author"]).Id;
 }  

SET

entity["vv_author"] = new EntityReference("contact", new Guid("3793ebd4-ac3f-eb11-a813-000d3a56963a"));

Leave a Reply

Your email address will not be published. Required fields are marked *