How to get and set values in plugins?

 

How to get and set values in plugins? 

Ans.

 using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.Xrm.Sdk;


namespace pluginsetgetApp

{

    public class pluginsetgetApp : IPlugin

    {

        public void Execute(IServiceProvider serviceProvider)

        {

            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

            IOrganizationService service = factory.CreateOrganizationService(context.UserId);

            ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

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

            {

                try

                {


                    tracer.Trace("started trace");

                    Entity targetEntity= (Entity)context.InputParameters["Target"];

 lookup get

                   EntityReference account = targetEntity.Contains("new_account") ?   

                                    (EntityReference)targetEntity["new_account"] : null;

                    if (account != null)

                        tracing.Trace("account is: ", account);

lookup set

                    EntityReference account1 = new EntityReference("account", new Guid("80ac35a0-01af-ea11-a812-000d3a8b3ec6"));

                    targetEntity["new_account"] = account1;

 singleline get                  

                      string name = targetEntity.Contains("new_name") ?

                    (string)targetEntity["new_name"] : null;

                    if (name != null)

                        tracing.Trace("your name:"+name);

singleline set

                    string name1 = string.Empty;

                    name1 = "hari";

                    targetEntity["new_name"] = name1;

                     tracing.Trace("your name:"+name1);

multiline get 

                   string description = targetEntity.Contains("new_description") ?

                  (string)targetEntity["new_description"] : null;

                    if (description != null)

                        tracing.Trace("description is:"+ description);

multiline set 

                    string description1 = string.Empty;

                    description1 = "newdescription";

                    targetEntity["new_description"] = description1;

                    tracing.Trace("descriptisn is:", description1);

datetime get

                    DateTime join = targetEntity.Contains("new_join") ? (DateTime)targetEntity

                        ["new_join"] :    DateTime.Now;

                    if (join != DateTime.Now) ;

                    tracing.Trace("join date is:", join);

datetime set

                    DateTime join1 = new DateTime(2000, 12, 20);

                    targetEntity["new_join"] = join1;

                    tracing.Trace("join date is:", join1);

 currency get

                        Money salary = targetEntity.Contains("new_salary") ?   

                     (Money)targetEntity["new_salary"] : null;

                    if (salary != null)

                        tracing.Trace("salary is:", salary);

 currency set

                    Money salary1 = new Money(2500);

                    targetEntity["new_salary"] = salary1;

                    tracing.Trace("salary is:", salary1);

decimal get

                    decimal mobile = targetEntity.Contains("new_mobile") ?

                           (decimal)targetEntity["new_mobile"] : 0;

                    if (mobile != 0)

                        tracing.Trace("mobile number is:", mobile);

decimal set

                    decimal mobile1 = 8787878909;

                    targetEntity["new_mobile"] = mobile1;

                    tracing.Trace("mobile number is:", mobile1);

whole get

                    double whole = targetEntity.Contains("new_whole") ? (double)targetEntity
                            ["new_whole"] : 0;
                    if (whole != 0)
                        tracing.Trace("student fee is:", whole);
whole set
                    double whole1 = 2000;
                    targetEntity["new_whole"] = whole1;
                    tracing.Trace(" your fees:", whole1);

two option get

                    bool gender = targetEntity.Contains("new_gender") ? (bool)targetEntity

                   ["new_gender"] : true;

                    if (gender != true)

                        tracing.Trace("gender is: ", gender);

two option set

                    bool gender1 = new bool();

                    gender1 = true;

                    targetEntity["new_gender"] = gender1;

floating Number 

                    double weight = targetEntity.Contains("new_weight") ? (double)targetEntity

                    ["new_weight"] : 0;

                    if (weight != 0)

                        tracing.Trace("weight is: ", weight);

floating Number 

                    double weight1 = 50.34;

                    targetEntity["new_weight"] = weight1;

                    tracing.Trace("weight is:", weight1);

multi select option get

                    OptionSetValueCollection location = targetEntity.Contains("new_studentlocation") ?

                   (OptionSetValueCollection)targetEntity["new_studentlocation"] : null;

                    if (location != null)

                        foreach (var item in location)

                        {

                            tracing.Trace("location", location);

                        }

multi select option set

                    int[] country = { 100000001, 100000002 };

                    OptionSetValueCollection location1 = new OptionSetValueCollection();

                    foreach (var item in country)

                    {

                        location1.Add(new OptionSetValue(item));

                    }

                    targetEntity["new_studentlocation"] = location1;

                    tracing.Trace("no of sub :{0}", location1.Count);

                    tracing.Trace("sub is:{0}", location1.ToString());

optionset get

                         OptionSetValue branch = targetEntity.Contains("new_studentbranch") ?     

                        (OptionSetValue)targetEntity["new_studentbranch"] : null;

                    if (branch != null)

                        tracing.Trace("branch", branch);

optionset set

                    OptionSetValue branch1 = new OptionSetValue(100000003);

                    targetEntity["new_studentbranch"] = branch1;

                    tracing.Trace(branch1.ToString());

                }

                catch (Exception ex)

                {

                    throw new InvalidPluginExecutionException(ex.Message);

                }


            }

        }

    }

}

Comments