locked
CRM 2011 to 2013 Upgrade Plug In Assistance RRS feed

  • Question

  • First an apologie!  We do not have access to Customer Source and I am in need of some help regarding a plugin that originated in 4, worked in 2011 and is failing in 2013.  The most obvious references pointed out on TechNet are present including the use of DynamicEntities and the plug in itself is short and sweet.  But I am new to plugins, the SDK and VS.  When I open it in VS 2013 it builds just fine when I go to run it I get an error of referencing the 4.0 SDK which makes sense as the problem I need guidance on how to fix it.  I've read many articles and have checked or think I have checked the basics as far as eradicating CRM 4.0 references but I don't know what I don't know.  That's where my expertise comes to a screeching halt :) Any help would be greatly appreciate! I'd be delighted to attached the assembly.
    Thursday, September 25, 2014 5:22 PM

All replies

  • post the plugin code

    My blog: www.crmanswers.net - Rockstar 365 Profile

    Thursday, September 25, 2014 5:37 PM
  • full solution

    using System;

    using Xrm = Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Crm.Sdk;

    using Crm=Microsoft.Crm.Sdk;

    using Microsoft.Xrm.Sdk.Query;

    using Microsoft.Crm.SdkTypeProxy;

    using Microsoft.Xrm.Sdk.Metadata;

    using Microsoft.Crm.SdkTypeProxy.Metadata;

    namespace crm_close_opportunity_paid_in_full

    {

           public class SubscriptionPaidInFullCloseOpportunity : Xrm.IPlugin

           {

            public SubscriptionPaidInFullCloseOpportunity()

            {

            }

            public void Execute(Crm.IPluginExecutionContext context)

            {

                string str_guid_id = "";

                CrmMoney invoicedamount = null;

                CrmMoney paidamount = null;

                CrmMoney currentbalancedue = null;

                Guid accountid = Guid.Empty;

                Guid subscriptiontypeid = Guid.Empty;

                //only execute on POST stage

                if (context.Stage == MessageProcessingStage.BeforeMainOperationOutsideTransaction || context.Depth > 1)

                    return;

                 try

                {

                    DynamicEntity entity = (DynamicEntity)context.InputParameters[ParameterName.Target];

                    if (entity.Properties.Contains("gs1_subscriptionid"))

                        str_guid_id = (((Key)entity["gs1_subscriptionid"]).Value).ToString();

                    if (entity.Properties.Contains("gs1_invoicedamount"))

                        invoicedamount = (CrmMoney)entity["gs1_invoicedamount"];

                    if (entity.Properties.Contains("gs1_currentpaidamt"))

                        paidamount = (CrmMoney)entity["gs1_currentpaidamt"];

                    if (entity.Properties.Contains("gs1_currentbalancedue"))

                        currentbalancedue = (CrmMoney)entity["gs1_currentbalancedue"];

                    if (entity.Properties.Contains("gs1_accountid"))

                        accountid = ((Lookup)entity["gs1_accountid"]).Value;

                    if (entity.Properties.Contains("gs1_subscriptiontypeid"))

                        subscriptiontypeid = ((Lookup)entity["gs1_subscriptiontypeid"]).Value;

                }

                catch (Exception e)

                {

                    throw new Xrm.InvalidPluginExecutionException(e.StackTrace);

                }

                //throw new InvalidPluginExecutionException(accountid.ToString() + " - " + subscriptiontypeid.ToString() + " - " + str_guid_id);

                 if (currentbalancedue.Value == Convert.ToDecimal(0))

                {

                    ICrmService crmService = context.CreateCrmService(true);

                    if ((accountid == Guid.Empty || subscriptiontypeid == Guid.Empty) && str_guid_id != "")

                        update_opportunty_by_subid(crmService, str_guid_id);

                    else

                        update_opportunty_by_actid(crmService, accountid, subscriptiontypeid);

                }

            }

            private void update_opportunty_by_actid(ICrmService crmService, Guid accountid, Guid subscriptiontypeid)

            {

                FilterExpression fe = new FilterExpression();

                fe.FilterOperator = LogicalOperator.And;

                fe.AddCondition("accountid", ConditionOperator.Equal, new string[] { accountid.ToString() });

                fe.AddCondition("gs1_subscriptiontypeid", ConditionOperator.Equal, new string[] { subscriptiontypeid.ToString() });

                fe.AddCondition("actualclosedate", ConditionOperator.Null);

               QueryExpression qe = new QueryExpression();

                qe.EntityName = EntityName.opportunity.ToString();

                qe.Criteria = fe;

                RetrieveMultipleRequest retrieveMultipleRequest = new RetrieveMultipleRequest();

                retrieveMultipleRequest.Query = (Microsoft.Crm.Sdk.Query.QueryBase)qe;

                retrieveMultipleRequest.ReturnDynamicEntities = true;

                RetrieveMultipleResponse retrieveMultipleResponse = (RetrieveMultipleResponse)crmService.Execute(retrieveMultipleRequest);

                foreach (DynamicEntity dynEntity in retrieveMultipleResponse.BusinessEntityCollection.BusinessEntities)

                {

                    WinOpportunityRequest request = null;

                    opportunityclose close = new opportunityclose();

                    close.opportunityid = new Lookup();

                    close.opportunityid.Value = ((Key)dynEntity.Properties["opportunityid"]).Value;

                    close.actualend = new CrmDateTime(DateTime.Now.ToString());

                    request = new WinOpportunityRequest();

                    request.OpportunityClose = close;

                    request.Status = 3;

                    crmService.Execute(request);

                }

            }

            private void update_opportunty_by_subid(ICrmService crmService, string str_guid_id)

            {

                FilterExpression fe = new FilterExpression();

                fe.FilterOperator = LogicalOperator.And;

                fe.AddCondition("gs1_subscriptionid", ConditionOperator.Equal, new string[] { str_guid_id });

                fe.AddCondition("actualclosedate", ConditionOperator.Null);

                QueryExpression qe = new QueryExpression();

                qe.EntityName = EntityName.opportunity.ToString();

                qe.Criteria = fe;

                RetrieveMultipleRequest retrieveMultipleRequest = new RetrieveMultipleRequest();

                retrieveMultipleRequest.Query = qe;

                retrieveMultipleRequest.ReturnDynamicEntities = true;

                RetrieveMultipleResponse retrieveMultipleResponse = (RetrieveMultipleResponse)crmService.Execute(retrieveMultipleRequest);

                foreach (DynamicEntity dynEntity in retrieveMultipleResponse.BusinessEntityCollection.BusinessEntities)

                {

                    WinOpportunityRequest request = null;

                    opportunityclose close = new opportunityclose();

                    close.opportunityid = new Lookup();

                    close.opportunityid.Value = ((Key)dynEntity.Properties["opportunityid"]).Value;

                    close.actualend = new CrmDateTime(DateTime.Now.ToString());

                    request = new WinOpportunityRequest();

                    request.OpportunityClose = close;

                    request.Status = 3;

                    crmService.Execute(request);

                }

            }

           }

    }

    Friday, September 26, 2014 3:06 PM
  • Found a local C# programmer who has no knowledge of CRM and made some progress until we hit this patch almost at the end.

    QueryExpression qe = new QueryExpression();

                qe.EntityName = EntityName.opportunity.ToString();

                qe.Criteria = fe;

                RetrieveMultipleRequest retrieveMultipleRequest = new RetrieveMultipleRequest();

                retrieveMultipleRequest.Query = qe;

                retrieveMultipleRequest.ReturnDynamicEntities = true;

    Friday, September 26, 2014 3:06 PM
  • Please refer to the following articles as many things have changed since crm4 especially datatypes, DynamicsEntity, Key etc to .Net Types, Entity, EntityReference for lookups 

    http://msdn.microsoft.com/en-us/library/gg328507.aspx

    http://crmbusiness.wordpress.com/2011/01/28/type-mapping-between-versions-crm-2011-and-crm-4/

    http://crmbusiness.wordpress.com/2014/04/07/crm-2013-step-by-step-update-plugin-tutorial-using-the-crm-2013-development-toolkit/

    http://msdn.microsoft.com/en-us/library/gg328300.aspx


    Hope this helps. Amar

    CRM Forum Guidance on how to Help Us Help You

    • Proposed as answer by KriSree Tuesday, September 30, 2014 8:53 AM
    Friday, September 26, 2014 4:11 PM
  • it is going to be a pain for some time. Best place to start work would be to download crm sdk and look into the examples.

    if this is your only plugin, I could revise the code for you to get it working in CRM 2013.

    please let me know.

    regards

    Jithesh

    Saturday, September 27, 2014 9:54 AM
  • Hello

    Jitesh can I just tell you how happy I was to find your reply this morning! A million thanks!  Yes this is the only plugin.  I have downloaded the SDK however I am so out of practice it would take me forever to work through this.  What do I need to do so that you can revise the plugin?  After that is down I will step through it and try and use it as a base for practice plugins so that I can learn from this!!!!  Thank You!!!

    Monday, September 29, 2014 12:07 PM
  • Please do not forget to add .net framework 4.0, sdk dlls etc. The code is not compiled/tested. I am sure it should work 95%, leaving the actual debugging to you :)

    using System;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Sdk.Messages;
    using Microsoft.Crm.Sdk.Messages;
    
    
    namespace crm_close_opportunity_paid_in_full
    {
    
        public class SubscriptionPaidInFullCloseOpportunity : IPlugin
        {
    
            public SubscriptionPaidInFullCloseOpportunity()
            {
    
            }
    
    
            public void Execute(IServiceProvider serviceProvider)
            {
                IPluginExecutionContext context =
                  (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                string str_guid_id = "";
                float invoicedamount = 0.0f;
                float paidamount=0.0f;
                float currentbalancedue=0.0f;
                Guid accountid = Guid.Empty;
                Guid subscriptiontypeid = Guid.Empty;
    
                //only execute on POST stage
    
                if (context.Stage == 10 || context.Depth > 1)
                    return;
    
    
                try
                {
    
                    Entity entity = (Entity)context.InputParameters["Target"];
                    if (entity.Contains("gs1_subscriptionid"))
                        str_guid_id = (((EntityReference)entity["gs1_subscriptionid"]).Id).ToString();
    
                    if (entity.Contains("gs1_invoicedamount"))
                        invoicedamount = entity.GetAttributeValue<float>("gs1_invoicedamount");
    
                    if (entity.Contains("gs1_currentpaidamt"))
                        paidamount = entity.GetAttributeValue<float>("gs1_currentpaidamt");
    
                    if (entity.Contains("gs1_currentbalancedue"))
                        currentbalancedue = entity.GetAttributeValue<float>("gs1_currentbalancedue");
    
                    if (entity.Contains("gs1_accountid"))
                        accountid = ((EntityReference)entity["gs1_accountid"]).Id;
    
                    if (entity.Contains("gs1_subscriptiontypeid"))
                        subscriptiontypeid = ((EntityReference)entity["gs1_subscriptiontypeid"]).Id;
    
                }
    
                catch (Exception e)
                {
    
                    throw new InvalidPluginExecutionException(OperationStatus.Failed, e.StackTrace);
    
                }
    
    
                //throw new InvalidPluginExecutionException(accountid.ToString() + " - " + subscriptiontypeid.ToString() + " - " + str_guid_id);
    
    
                if (currentbalancedue == 0)
                {
    
                    
                    if ((accountid == Guid.Empty || subscriptiontypeid == Guid.Empty) && str_guid_id != "")
                        update_opportunty_by_subid(service, str_guid_id);
                    else
                        update_opportunty_by_actid(service, accountid, subscriptiontypeid);
    
                }
    
            }
    
    
            private void update_opportunty_by_actid(IOrganizationService crmService, Guid accountid, Guid subscriptiontypeid)
            {
    
                FilterExpression fe = new FilterExpression();
                fe.FilterOperator = LogicalOperator.And;
                fe.AddCondition("accountid", ConditionOperator.Equal, new string[] { accountid.ToString() });
                fe.AddCondition("gs1_subscriptiontypeid", ConditionOperator.Equal, new string[] { subscriptiontypeid.ToString() });
                fe.AddCondition("actualclosedate", ConditionOperator.Null);
    
                QueryExpression qe = new QueryExpression();
                qe.EntityName = "opportunity";
    
                qe.Criteria = fe;
    
    
                RetrieveMultipleRequest retrieveMultipleRequest = new RetrieveMultipleRequest();
                retrieveMultipleRequest.Query = qe;
    
                //retrieveMultipleRequest.ReturnDynamicEntities = true;
    
    
                RetrieveMultipleResponse retrieveMultipleResponse = (RetrieveMultipleResponse)crmService.Execute(retrieveMultipleRequest);
    
    
                foreach (Entity dynEntity in retrieveMultipleResponse.EntityCollection.Entities)
                {
    
                    WinOpportunityRequest request = null;
    
                    Entity close = new Entity("opportunityclose");
                    EntityReference opp = (EntityReference)dynEntity["opportunityid"];
                    close["opportunityid"] = opp;
                    close["actualend"] = DateTime.Now.ToLocalTime();
    
    
                    request = new WinOpportunityRequest();
                    request.OpportunityClose = close;
                    request.Status = new OptionSetValue(3);
                    crmService.Execute(request);
                }
    
            }
    
    
            private void update_opportunty_by_subid(IOrganizationService crmService, string str_guid_id)
            {
    
                FilterExpression fe = new FilterExpression();
                fe.FilterOperator = LogicalOperator.And;
                fe.AddCondition("gs1_subscriptionid", ConditionOperator.Equal, new string[] { str_guid_id });
                fe.AddCondition("actualclosedate", ConditionOperator.Null);
    
                QueryExpression qe = new QueryExpression();
                qe.EntityName = "opportunity";
    
                qe.Criteria = fe;
    
    
                RetrieveMultipleRequest retrieveMultipleRequest = new RetrieveMultipleRequest();
    
                retrieveMultipleRequest.Query = qe;
    
                //retrieveMultipleRequest.ReturnDynamicEntities = true;
    
    
                RetrieveMultipleResponse retrieveMultipleResponse = (RetrieveMultipleResponse)crmService.Execute(retrieveMultipleRequest);
    
    
                foreach (Entity dynEntity in retrieveMultipleResponse.EntityCollection.Entities)
                {
    
                    WinOpportunityRequest request = null;
    
                    Entity close = new Entity("opportunityclose");
                    EntityReference opp = (EntityReference)dynEntity["opportunityid"];
                    close["opportunityid"] = opp;
                    close["actualend"] = DateTime.Now.ToLocalTime();
    
    
                    request = new WinOpportunityRequest();
                    request.OpportunityClose = close;
                    request.Status = new OptionSetValue(3);
                    crmService.Execute(request);
    
                }
    
            }
    
        }
    
    }
    

    Monday, September 29, 2014 1:16 PM
  • context.Stage > 10 might be the issue. Are you able to debug the plugin

    regards

    Jithesh

    Tuesday, September 30, 2014 8:31 PM
  • Hi KAB2015,

    I only suggested to share your code, in order to other users can help you, as Jithesh did. I didn't write any code in this thread, only Jithesh did. Please mark his answers as verified if they helped you.


    My blog: www.crmanswers.net - Rockstar 365 Profile

    Wednesday, October 1, 2014 1:34 PM