Recent Posts

ads

Single record is created in Single Date in microsoft dynamics crm

Requirement:

I have an offer Entity in my microsoft dynamics crm . In that "lvt_startdate" Field is available.so I have to choose one date dependent on the field esteem single record is made by sear date as it were in microsoft crm.
Solution:
Plugin:
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;


namespace ccount
{
    public class ccount : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext Context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            if (Context.InputParameters.Contains("Target") && Context.InputParameters["Target"] is Entity)
            {
                ITracingService TraceService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                IOrganizationServiceFactory ServiceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService Service = ServiceFactory.CreateOrganizationService(Context.UserId);
                Entity Offer = (Entity)Context.InputParameters["Target"];
                try
                {



                    if (Offer.Contains("lvt_startdate"))
                    {
                        DateTime startdate = (DateTime)Offer["lvt_startdate"];

                        if (Offer.Contains("lvt_enddate"))
                        {
                            DateTime enddate = (DateTime)Offer["lvt_enddate"];

                            //DateTime startdate = Offer.GetAttributeValue<DateTime>("lvt_startdate");
                            // DateTime enddate = Offer.GetAttributeValue<DateTime>("lvt_enddate");
                            string fetchXML = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                                                      <entity name='lvt_offer'>
                                                        <attribute name='lvt_offerid' />
                                                        <attribute name='lvt_name' />
                                                        <attribute name='createdon' />
                                                        <order attribute='lvt_name' descending='false' />
                                                        <filter type='and'>
                                                          <condition attribute='lvt_startdate' operator='on' value='" + startdate + @"' />
                                                          <condition attribute='lvt_enddate' operator='on' value='" + enddate + @"' />
                                                        </filter>
                                                      </entity>
                                                    </fetch>";
                            var Collection = Service.RetrieveMultiple(new FetchExpression(fetchXML));
                            if (Collection.Entities.Count > 0)
                            {
                                throw new InvalidPluginExecutionException("offer contains a record");
                            }

                        }

                    }   


                }
                catch (Exception Ex)
                {
                    throw new InvalidPluginExecutionException(Ex.Message);
                    
                }
            }

        }
    }
}


Message:create
stage: postoperation

Post a Comment

1 Comments

  1. Hai sir, i want to contact u. Please give me some contact details to contact u . Please i need a help plsss

    ReplyDelete