locked
Create a record based on From date and to Date in CRM 2011 Plugins RRS feed

  • Question

  • Hi All,

          In an Opportunity Screen am having a Start date,Enddate and estimated Value. When ever User creates a Record i need to take the Count of the Month and split the estimated value and create a record in Another entity. I have tried below code its working for some condition and not for some conditon. My code is below.

       DateTime startdate = (DateTime)outEntity.Attributes["new_startdate"];
       DateTime enddate = (DateTime)outEntity.Attributes["new_enddate"];
       Money estrevenue = new Money(((Microsoft.Xrm.Sdk.Money)(outEntity.Attributes["estimatedvalue"])).Value);
       int sub = (enddate.Subtract(startdate).Days / 30);
       int estrev = (Convert.ToInt32(estrevenue.Value));
       int estimateddiv = (estrev / sub);

                                for (int i = 0; i < sub; i++)
                                {
                                    Entity revenue = new Entity();
                                    revenue.LogicalName = "new_projection";
                                    revenue.Attributes.Add("new_opportunityid", new EntityReference("opportunity", OppID));
                                    revenue.Attributes.Add("ownerid", new EntityReference("systemuser", ownerid));
                                    revenue.Attributes.Add("transactioncurrencyid", new EntityReference("transactioncurrency", (((EntityReference)(outEntity.Attributes["transactioncurrencyid"])).Id)));
                                    revenue.Attributes["new_date"] = (startdate).AddMonths(i);
                                    revenue.Attributes["createdon"] = startdate.AddMonths(-2);
                                    revenue.Attributes["modifiedon"] = startdate.AddMonths(-1);
                                    revenue.Attributes["overriddencreatedon"] = startdate.AddMonths(-2);
                                    revenue.Attributes["new_amount"] = new Money(decimal.Parse(estimateddiv.ToString()));
                                    service.Create(revenue);

                                }

    Above code works when i put start date as 1/jan/2015 and 30/Feb/2015 (Split up 1 month and inserting Perfect)

    But if i use  26/jan/2015 and 36/jan/2015 if shows the error like "System date cannot be set to a date in the future".

    Please guide here. Thanks in advance

    Monday, November 24, 2014 6:31 AM

All replies