locked
Can't Get ActualcloseDate/ActualValue in Plugin when Opportunity Close as won/lose RRS feed

  • Question

  • 【env:】 crm 2016

    【what i want to do:】 

    1.there are three plugins ,[Plugin A] is register at Opportunity pre-operation,[Plugin B],[Plugin C] is register at Opportunity post-operation.

    2.opportunity close as won/lost . opportunity update event start.

    3.[plugin A] Check the actualclosedate in inputparameter,if the check result is true then set opportunity.flag = "1".

    4.[plugin B] if opportunity.flag =="1" then do sth

    5.[plugin C] if opportunity.flag !="1" then do sth

    【question is:】 actualclosedate is null in inputparameter.

    i had try to move [Plugin A] into post-operation and use postimg to get actualclosedate,i can get actualclosedate but there is another issue. when i  save opportunity

                    //m_orgContext.ClearChanges();
                    //m_orgContext.Attach(m_enOp);
                    //m_orgContext.UpdateObject(m_enOp);
                    //m_orgContext.SaveChanges();
                    //m_orgContext.Detach(m_enOp);


    it got bussiness process error."you cannot update "Est.Close Date in Half Year(hidden in form)",since it is changed up automatically. 

    here is my post-operation  plugin code

    using System;
    using System.Linq;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Client;
    using Binding;
    
    namespace Opportunity_Post_PluginA_Plugin
    {
    
        public class Opportunity_Post_PluginA_Plugin : IPlugin
        {
            #region Class Data Member
            private IPluginExecutionContext m_context;
            private IOrganizationServiceFactory m_factory;
            private ITracingService m_tracingService;
            private IOrganizationService m_service;
            private OrganizationServiceContext m_orgContext;
    
            private Opportunity m_enOp;
            private Opportunity m_enUpdatedOP;
            #endregion
    
    
            /// <summary>
            /// void Execute(IServiceProvider serviceProvider)
            /// </summary>
            /// <param name="serviceProvider">IServiceProvider serviceProvider</param>
            public void Execute(IServiceProvider serviceProvider)
            {
                try
                {
                   // Opportunity enOP = new Opportunity();
                    //init
                    if (!IsInitSuccess(serviceProvider))
                    {
                        return;
                    }
    
                    if(CheckExist())
                    {
                        if (m_enUpdatedOP.new_UpdateFlg == "1")
                        {
                            m_enUpdatedOP.new_UpdateFlg = "0";
    
                        }
                        else
                        {
                            m_enUpdatedOP.new_UpdateFlg = "1";
                        }
                    }
                    else
                    {
                        if (m_enUpdatedOP.new_opFlg == "1")
                        {
                            if (m_enUpdatedOP.new_createflg == "1")
                            {
                                m_enUpdatedOP.new_createflg = "0";
    
                            }
                            else
                            {
                                m_enUpdatedOP.new_createflg = "1";
                            }
                        }
                    }
                    // i just want to update new_createflg and new_UpdateFlg , but exist plugin post Can't update XXXX field . what should i do?
                    //save changes
                    m_orgContext.ClearChanges();
                    m_orgContext.Attach(m_enUpdatedOP);
                    m_orgContext.UpdateObject(m_enUpdatedOP);
                    m_orgContext.SaveChanges();
                    m_orgContext.Detach(m_enUpdatedOP);
    
                }
                catch (Exception e)
                {
                    m_tracingService.Trace("Exception: {0}", e.ToString());
    
                    throw (e.InnerException == null) ? new InvalidPluginExecutionException(e.Message)
                                                      : new InvalidPluginExecutionException(e.InnerException.Message);
                }
            }
    
            private bool IsInitSuccess(IServiceProvider serviceProvider)
            {
                bool bInitSuccess = true;
    
                m_context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                if (!(m_context.InputParameters.Contains("Target"))
                    || !(m_context.InputParameters["Target"] is Entity)
                    || (m_context.PrimaryEntityName != Opportunity.EntityLogicalName)
                    || (m_context.MessageName != "Update")
                    || (m_context.Depth >= 2)
                    )
                {
                    bInitSuccess = false;
                    return bInitSuccess;
                }
    
                m_factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                if (m_factory == null)
                {
                    bInitSuccess = false;
                    throw new InvalidPluginExecutionException("Failed to retrieve the organization service.");
                }
    
                m_tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
                if (m_tracingService == null)
                {
                    bInitSuccess = false;
                    throw new InvalidPluginExecutionException("Failed to retrieve the tracing service.");
                }
    
                m_service = m_factory.CreateOrganizationService(m_context.UserId);
                m_orgContext = new OrganizationServiceContext(m_service);
                // Obtain the target entity from the input parmameters.
                m_enOp = ((Entity)m_context.InputParameters["Target"]).ToEntity<Opportunity>();
    
                var query = from CN in m_orgContext.CreateQuery<Opportunity>()
                            where CN.Id == m_enOp.Id
                            select CN;
    
                if (query.ToList().Any() == false)
                {
                    bInitSuccess = false;
                    return bInitSuccess;
                }
                m_enUpdatedOP = query.FirstOrDefault();
    
                m_tracingService.Trace("yes flag is saved before open close oppo form ,inputparam has no yes flag so get the saved yesflag and clean it at the end of this plugin");
                if (m_enOp == null || m_enOp.StateCode == null || m_enUpdatedOP == null)
                {
                    bInitSuccess = false;
                    return bInitSuccess;
                }
    
                m_tracingService.Trace("test input parameter");
                if (m_enOp.StateCode == null)
                {
                    m_tracingService.Trace("m_enOp.StateCode is null");
                }
                else
                {
                    m_tracingService.Trace("m_enOp.StateCode {0}", m_enOp.StateCode);
                }
    
                if (m_enOp.ActualCloseDate == null)
                {
                    m_tracingService.Trace("m_enOp.ActualCloseDate is null");
                }
                else
                {
                    m_tracingService.Trace("m_enOp.ActualCloseDate {0}", m_enOp.ActualCloseDate);
                }
    
                if (m_enUpdatedOP.ActualCloseDate == null)
                {
                    m_tracingService.Trace("m_enOp.ActualCloseDate is null");
                }
                else
                {
                    m_tracingService.Trace("m_enOp.ActualCloseDate {0}", m_enUpdatedOP.ActualCloseDate);
                }
    
                return bInitSuccess;
            }
    
    
            private bool CheckExist()
            {
                bool bBCExistFlg = true;
    
                IQueryable<new_budgetcontrol> query;
    
                DateTime opClosedate = new DateTime();
    
                if(m_enOp.StateCode == OpportunityState.Open)
                {
                    opClosedate = m_enUpdatedOP.EstimatedCloseDate.Value;
                }
                else
                {
                    opClosedate = m_enUpdatedOP.ActualCloseDate.Value;
                }
    
                //on the premise that the same fiscal year BC records are exist
                if (opClosedate.Month >= 4)
                {
                    query = from CN in m_orgContext.CreateQuery<new_budgetcontrol>()
                            where (CN.new_OpportunityName.Id == m_enUpdatedOP.Id
                                   && CN.new_FY != null
                                   && (int)CN.new_FY.Value == (int)opClosedate.Year
                                   )
                            select CN;
                }
                else
                {
                    query = from CN in m_orgContext.CreateQuery<new_budgetcontrol>()
                            where (CN.new_OpportunityName.Id == m_enUpdatedOP.Id
                                   && CN.new_FY != null
                                   && (int)CN.new_FY.Value == (int)opClosedate.Year - 1
                                   )
                            select CN;
    
                }
    
                if (query.ToList().Any() == false)
                {
                    bBCExistFlg = false;
                }
                else
                {
                    bBCExistFlg = true;
                }
    
                return bBCExistFlg;
            }
    
        }
    }
    

    • Edited by che70 Friday, October 27, 2017 2:49 AM
    Wednesday, October 25, 2017 9:17 AM

Answers

  • Hi,Moh

    Thank you for your answer.

    the link below had solve my issue. : )

    community.dynamics.com/crm/f/117/p/255821/717441#717441

    Thank you for give me good advices.

    Kind regards, 

    Che

    • Marked as answer by che70 Monday, October 30, 2017 4:38 AM
    Monday, October 30, 2017 4:37 AM

All replies

  • Hi, 

    do you try to update the Est. Close Date in your plugins ? 

    Kr,

    Moh

    Wednesday, October 25, 2017 1:33 PM
  • i didn't update est.Close Date ,here is my code .

    T T

    //opObj is inputparameter var query = from CN in m_orgContext.CreateQuery<Opportunity>() where CN.Id == opObj.Id select CN; if (query.ToList().Any() == false) { bInitSuccess = false; return bInitSuccess; } m_enOp = query.FirstOrDefault(); //update a flag and save it m_enOp.new_BCUpdateFlg = "3"; //m_orgContext.UpdateObject(m_enOp); //save changes m_orgContext.ClearChanges(); m_orgContext.Attach(m_enOp); m_orgContext.UpdateObject(m_enOp); m_orgContext.SaveChanges(); m_orgContext.Detach(m_enOp);


    Friday, October 27, 2017 12:09 AM
  • if you want to update an opportunity I suggest you to create an opportunity with just the information that you want to update and the id because in your code you update all fields already present in the opportunity.  ( With the field "Est.Close Date") 

    I think it will resolve your problem. 

    Kind regards, 

    Moh

    Friday, October 27, 2017 11:50 AM
  • Hi,Moh

    Thank you for your answer.

    the link below had solve my issue. : )

    community.dynamics.com/crm/f/117/p/255821/717441#717441

    Thank you for give me good advices.

    Kind regards, 

    Che

    • Marked as answer by che70 Monday, October 30, 2017 4:38 AM
    Monday, October 30, 2017 4:37 AM