locked
Passing values between forms CRM 2013 plugin RRS feed

  • Question

  • Hello,

    I am trying to pass values from the case form to the case resolution form.  I can do it the other way round, but I would like to be able to pick a value from a lookup on the case form and pass that to the case resolution subject field and the case resolution description from the case form to the description field on the case resolution description field.  Is this possible? I am registering the plugin as pre-operation.  My code is:

        public class ResolveCase : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                Guid incidentId = Guid.Empty;
                string incident_resolution = string.Empty;
    
                // Get the execution context from the service provider
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                if (context.InputParameters.Contains("IncidentResolution") && context.PrimaryEntityName == "incident" && context.MessageName == "Close")
                {
                    // Get the target entity from the input parameters
                    Entity entity = (Entity)context.InputParameters["IncidentResolution"];
    
                    incidentId = ((EntityReference)entity.Attributes["incidentid"]).Id;
                    incident_resolution = string.Format("{0}{1}{2}", entity.Attributes["subject"].ToString(), Environment.NewLine, entity.Attributes["description"].ToString());
    
                    if (incidentId != Guid.Empty)
                    {
                        try
                        {
                            Entity incident = service.Retrieve("incident", ((EntityReference)entity["incidentid"]).Id, new ColumnSet() { AllColumns = true });
                            if (!incident.Contains("new_statusreason") || incident["new_ststusreason"] == null)
                            {
                                throw new InvalidPluginExecutionException("The Case Resolution 'Resolution Code' field cannot be empty");
                            }
                            else
                            {
                                // Enter the new_resolution code in to the resolution subject field and 
                                // the new_resolution description in to the resolution description field
                                entity["subject"] = incident.GetAttributeValue<string>("new_statusreason").ToString();
                                entity["description"] = incident.GetAttributeValue<string>("new_resolutiondescription").ToString();
    
                                //incident["new_resolutiondescription"] = incident_resolution;
                                incident["new_dateresolved"] = DateTime.Now;
                                service.Update(incident);
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new InvalidPluginExecutionException(ex.Message);
                        }
                    }
                }
            }
        }
    

    Thank in advance

    Duane

    Thursday, October 23, 2014 9:01 AM

All replies

  • If I understand correctly you want to set the values on the IncidentResolution record.  Why not register your plugin pre-create on the IncidentResolution Entity instead of the Incident entity.  You can then use the IncidentId from the IncidentResolution to grab the Incident record values you want and set them there before the resolution is created.
    Thursday, October 23, 2014 8:17 PM
  • Thanks for the reply, but as far as I am aware I am running the plugin as a create on the incidentresolution and this is registered as a pre-operation step, I changed the close to create but I makes no difference, the values from the Case form are still not displayed on the resolution popup, even if I save the Case before clicking Resolve.

    Any further help would be appreciated.

    Thanks

    Duane

    Friday, October 24, 2014 10:25 AM
  • You code has conditional logic to only do anything if the plugin is registered on the Close message of the incident entity

    if (context.InputParameters.Contains("IncidentResolution") && context.PrimaryEntityName == "incident" && context.MessageName == "Close")
                

    If you register the plugin on the on the Close message of the incident entity, what happens ?

    Or you could register the code on the pre-operation step of the Create message on the incidentresolution entity, but you'd have to change the conditional logic, and you would also get the incidentresolution Entity instance via context.InputParameters["Target"]


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Friday, October 24, 2014 11:07 AM
    Moderator
  • Hello,

    The only I am able to get any plugin to fire is using the code I have placed above.  I can get the values from the incidentresolution and save them in the incident form but a cannot get the values to go from the incident form on to the incidentresolution.  If I register the plugin as anything other than what I have, it doesn't seem to fire.  I have the writing of field values with an exception and the exception is never thrown, unless I register as a close on the incident resolution.

    I guess what I am trying to do is fire a plugin before the resolve case popup appears, or if possible, perform a case resolution without even showing the resolve case popup, but still check for open activities.

    Thanks

    Duane


    • Edited by Duane Stead Friday, October 24, 2014 3:35 PM
    Friday, October 24, 2014 1:54 PM
  • Hello,

    Has anybody got any further suggestions or code examples? or am I trying to do the impossible?

    Thanks

    Duane

    Tuesday, October 28, 2014 9:21 AM
  • Hi Duane,

    You can achieve your requirement using unsupported approach.

    What you need to do?

    1. Open "dlg_closecase.aspx" file from C:\Program Files\Microsoft Dynamics CRM\CRMWeb\CS\cases

        assuming CRM is installed in C drive.

    2. Search for function windowOnLoad( ).

    3. Add following  below line of JavaScript in function

     // ********** Unsuported JS Added ********//
        if (window.opener != null) {
    
            $get('subject').value = window.opener.Xrm.Page.data.entity.attributes.get("new_statusreason").getValue()[0].name;
            $get('description').value = window.opener.Xrm.Page.data.entity.attributes.get("new_resolutiondescription").getValue();
        }
        // ********** Unsuported JS Added ********//
    After adding above JS.

    function windowOnLoad( )
    {
       
        // ********** Unsuported JS Added ********//
        if (window.opener != null) {
    
            $get('subject').value = window.opener.Xrm.Page.data.entity.attributes.get("new_statusreason").getValue()[0].name;
            $get('description').value = window.opener.Xrm.Page.data.entity.attributes.get("new_resolutiondescription").getValue();
        }
        // ********** Unsuported JS Added ********//
    
    $get('totaltimespent').value = formatDuration(_iTotalTime);
    
    if (typeof(LOCID_CONFIRM_PARENT)!="undefined" && !confirm(LOCID_CONFIRM_PARENT))
    {
    closeWindow();
    }
    
    if (typeof(LOCID_CONFIRM_CONTRACTDETAILSTAT)!="undefined" && !confirm(LOCID_CONFIRM_CONTRACTDETAILSTAT))
    {
    closeWindow();
    }
    
    if (typeof(LOCID_CONFIRM_ACTIVITIES)!="undefined")
    {
    alert(LOCID_CONFIRM_ACTIVITIES);
    closeWindow();
    }
    }

    4.Save the ASPX file.

    5. Try to resolve the Case.Below is the desired result

    Hope this helps.


    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer". Thanks and Regards, Mohammad Yusuf Ansari http://microxrm.blogspot.in



    Tuesday, October 28, 2014 11:48 AM
  • Hi Mohammad,

    I added the lines you suggested but the resolution fields are still empty! I Rebooted the server after making the change but no different.  I am using CRM 2013, but the JS file looked the same as yours, so I just added the lines in the place you suggested.

    Thanks

    Duane

    Tuesday, October 28, 2014 1:08 PM
  • Hi Duane,

    Ideally it should work.I have not tested  it in CRM 2013. Remove IF Condition and try below to see if it  works.

       // ********** Unsuported JS Added ********//
       
            $get('subject').value = "Test Subject";
            $get('description').value = "Test Description";
        
        // ********** Unsuported JS Added ********//

    Regards,


    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer". Thanks and Regards, Mohammad Yusuf Ansari http://microxrm.blogspot.in


    Tuesday, October 28, 2014 3:23 PM
  • Hi Mohammad,

    Putting just test subject and test description in the JS file has worked.  It didn't with your original code, even if I remove the if statement.

    Thanks

    Duane

    Tuesday, October 28, 2014 3:45 PM
  • Hi Duane,

    The problem lies in "window.opener" syntax as I am not sure how CRM 2013 we can access the XRM page from Case Close dialog Box.

    I do not have CRM 2013 Server access.I will try and let you know.

    Thanks,


    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer". Thanks and Regards, Mohammad Yusuf Ansari http://microxrm.blogspot.in

    Tuesday, October 28, 2014 6:10 PM