Wednesday 30 April 2014

Basic Plug-in in CRM

Plug-in

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

namespace student
{
    public class students: IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            Entity targetEntity=null;
            if(context.InputParameters.Contains("Target")&&context.InputParameters["Target"]is Entity)
            {
                targetEntity = (Entity)context.InputParameters["Target"];
                if(targetEntity.LogicalName!="new_selectedapplicant")
                {
                    return;
                }
            }
            else
            {
                return;
            }
            try
            {
                IOrganizationService service = factory.CreateOrganizationService(context.UserId);
                string a = targetEntity.FormattedValues["new_groupselected"];
           
   
             


                string b = ((string)targetEntity["new_name"]);
                string c = ((string)targetEntity["new_lastname"]);
                string d = ((string)targetEntity["new_fathersname"]);
                string e = ((string)targetEntity["new_emailid"]);
                string f = ((string)targetEntity["new_schoolname"]);
                decimal g = ((decimal)targetEntity["new_english"]);
                decimal h = ((decimal)targetEntity["new_hindi"]);
                decimal i = ((decimal)targetEntity["new_science"]);
                decimal j = ((decimal)targetEntity["new_maths"]);
                Entity contact = new Entity();
                if (a=="science")
                {
                    contact.LogicalName = "new_sciencestudent";
                    contact.Attributes.Add("new_name", b);
                    contact.Attributes.Add("new_lastname", c);
                    contact.Attributes.Add("new_fathersname", d);
                    contact.Attributes.Add("new_emailid", e);
                    contact.Attributes.Add("new_schoolname", f);
                    contact.Attributes.Add("new_english", g);
                    contact.Attributes.Add("new_hindi", h);
                    contact.Attributes.Add("new_science", i);
                    contact.Attributes.Add("new_maths", j);
                    service.Create(contact);
                }
                else if (a =="commerce")
                {
                    contact.LogicalName = "new_commercestudent";
                    contact.Attributes.Add("new_name", b);
                    contact.Attributes.Add("new_lastname", c);
                    contact.Attributes.Add("new_fathersname", d);
                    contact.Attributes.Add("new_emailid", e);
                    contact.Attributes.Add("new_schoolname", f);
                    contact.Attributes.Add("new_english", g);
                    contact.Attributes.Add("new_hindi", h);
                    contact.Attributes.Add("new_science", i);
                    contact.Attributes.Add("new_maths", j);
                    service.Create(contact);


                }
                else
                {
                    contact.LogicalName = "new_artsstudent";
                    contact.Attributes.Add("new_name", b);
                    contact.Attributes.Add("new_lastname", c);
                    contact.Attributes.Add("new_fathersname", d);
                    contact.Attributes.Add("new_emailid", e);
                    contact.Attributes.Add("new_schoolname", f);
                    contact.Attributes.Add("new_english", g);
                    contact.Attributes.Add("new_hindi", h);
                    contact.Attributes.Add("new_science", i);
                    contact.Attributes.Add("new_maths", j);
                    service.Create(contact);
                }
            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                throw new InvalidPluginExecutionException(string.Concat("an error occured in plugin ", ex.Message, Environment.NewLine, ex.StackTrace));
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(string.Concat("An error occured in plugin " , ex.Message, Environment.NewLine, ex.StackTrace));

            }
       
        }
}
}

No comments:

Post a Comment