Wednesday, February 29, 2012

Step by step tutorial to add a CRM report on an entity form

Today we are going to add a CRM2011 report on an entity form. The report will run on a current record.
The solution is broken down into 4 steps
  1. Get the ReportViewer URL and the reportId
  2. Add an Iframe on an entity form
  3. Create a JavaScript webresource to set the src(URL) for Iframe.
  4. Call the webresource method on form load event.

How to get a ReportViewer URL and reportId

  • To get the reportviewer url and reportId, run the report from Workplace –Reports section as shown in the following screen shot.Report5
  • The report will look like the following screen shot.report1
  • The only thing we need from the above screen shot is URL at the top of the form. If you can’t see the URL press F11. Copy that URL. It will look like following
  • https://inventive.crm5.dynamics.com/crmreports/viewer/viewer.aspx?action=filter&helpID=Account%20Overview.rdl&id=%7bB13C5C38-BB4B-E111-9453-D8D3855B355E%7d
  • We are going to remove the parts of the URL with yellow background.
  • We don’t need the server and organization path as we will use relative path.
  • We don’t need the helpID parameter as report can run without it.
  • We will change the action = Filter to Run.
  • We will add an another parameter to the URL named “records” to run it on the current record.
  • The modified URL will look like
  • /crmreports/viewer/viewer.aspx?action=run&id=%7bB13C5C38-BB4B-E111-9453-D8D3855B355E%7d&records="

Add an Iframe on an entity form

  • Open the form in the customization mode and add a new tab as shown belowReport2
  • Change the formatting of the section to “One Column” and insert an Iframe. You can set the properties of the Iframe as shown in the following screen shot. Just remember the name of the Iframe is “IFRAME_OverviewReport” not “OverviewReport”.Report3
  • Save the customizations.

Create a JavaScript webresource

  • Create a JavaScript webresource and add the following function to it.
    function showReport() 
    {    
    
        //Get iframe control 
        var iframeObject = Xrm.Page.getControl("IFRAME_OverviewReport");
    
        if (iframeObject != null)
        {
            //URL we created in first step + id of the current record (Xrm.Page.data.entity.getId())
            var strURL = "/crmreports/viewer/viewer.aspx?action=run&id=%7bB13C5C38-BB4B-E111-9453-D8D3855B355E%7d&records=" + Xrm.Page.data.entity.getId();
            
            //Set iframe URL
            iframeObject.setSrc(strURL);
    
        }
    }
  • Check the highlighted line it is the same URL we created earlier in step (How to  a get ReportViewer URL and reportId).
  • Save the changes and publish the webresource.

Call the webresource method on form load event

  • Open the form in the customization mode again and click on form properties.
  • Add the webresource to the form and call the showReport function as shown in the screen shot below.report7
  • Save the form and publish the changes.
  • Test the solution.

Thursday, February 23, 2012

Use of “Filtering Attributes” in plugin registration

Recently one of my clients asked me to make some changes to one of their existing plugin. The plugin was sending an email to a user when “Estimated Revenue” field is updated. The plugin was comparing  the value of “Estimated Revenue” from the  PreEntityImage with value from PostEntityImage. It made me think that why you would use PreEntityImage and PostEntityImage values to do that when you can achieve that by setting  “Filtering Attributes” during plugin registration.
parentcustomer  
This is very handy parameter of step registration process. It is equivalent to “IsDirty” function in JavaScript. If you use “Filtering Attributes” on post update events, you don’t need to compare the value of fields using PreEntityImage and PostEntityImage. The other biggest advantage is that your plugin does not execute on every update event.
Here is some code. This code creates a note for a contact when parentcustomerid field is updated.
protected void ExecutePostContactUpdate(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }             // TODO: Implement your custom Plug-in business logic.
            IPluginExecutionContext context = localContext.PluginExecutionContext;
            IOrganizationService service = localContext.OrganizationService;
            
            // The InputParameters collection contains all the data passed in the message request.
            if (context.InputParameters.Contains("Target") &&
            context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parmameters.
                Entity entity = (Entity)context.InputParameters["Target"];
                
                try
                {
                    Entity task = new Entity("task");
                    task["subject"] = "Account number is missing";
                    task["regardingobjectid"] = new EntityReference("contact", entity.Id);
                    task["description"]= "Parent Customer field has been updated".;

                     // Create the task in Microsoft Dynamics CRM.
                    service.Create(task);
                                        
                }

                 catch (FaultException ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                }

             }
        }
To run this code create a plugin using developer's toolkit on contact entity. Use the values shown in the screen shot to register the update step. Then overwrite the  ExecutePostContactUpdate() method with this code. Deploy the plugin and test it.
Here is link to a step by step tutorial to create a plugin using developers toolkit.

Friday, February 17, 2012

Plugin to update children records when parent record is updated

I love the developer's toolkit. It makes it so easy to deploy plugins. In this blog we are going to create a plugin that will update the Main Phone (“telephone1”) of the contacts when the Main Phone of their parent account is updated. We will use the OrganizationContext to update the contacts.
  1. Create a new solution in CRM2011. I named my solution “CRM Plugin Solution”.
  2. Open Visual Studio 2010. Select File—New –Project. It will display new project templates dialog as shown in the screen sheet belowp1
  3. Select “Dynamics CRM 2011 Package” project. This is also optional. You can go ahead and select “Dynamics CRM 2011 Plugin Library”, but then you cannot deploy the plugin straight from the Visual Studio. You have to use Plugin Registration tool to register the plugin. Enter the name of project/solution.
  4. VS studio will display following dialog. Enter you CRM 2011 server details. Select the solution name we created in step 1 and click okp2
  5. Now right click on the solution and add “Dynamics CRM 2011 Plugin Library” project to the solution. The plugin project will already have a plugin.cs file.
  6. Now sign the plugin assembly. Right click on Plugin Project and select properties. Select Signing tab from left navigation of project property page. On the Signing tab, select the Sign the assembly check box and set the strong name key file of your choice. At a minimum, you must specify a new key file name. Do not protect your key file by using a password.
  7. If you cannot see the “CRM Explorer” window on the upper left side of the VS studio, click on View menu and select “CRM Explorer”. p3
  8. Now expand “Entities” Node. Right Click on “Account” entity and select “Create Plugin”.p4
  9. It will display a following screen. It is equivalent to “Update Step” screen in plugin registration tool. Notice the  “Pre Image Alias” and  “Post Image Alias”. I have passed the “telephone1” attribute to the pre image and post image. You can change of the Class attribute. Press Ok.image_thumb[7]
  10. It will create a PostAccountUpdateContacts.cs file with name mentioned in “Class” attribute in screen shot above. Double click on the class file (PostAccountUpdateContacts.cs).
  11. Add the following references at the top
  12.  using Microsoft.Xrm.Sdk.Client; // to get the OrganizationContext
     using System.Linq; // to use linq queries with OrganizationContext
    
  13. Scroll down the PostAccountUpdateContacts.cs file and look for ExecutePostAccountUpdateContacts method. Overwrite the method with the following code. Look at the comments to understand the code
  14.  protected void ExecutePostAccountUpdateContacts(LocalPluginContext localContext)
    {
        if (localContext == null)
        {
            throw new ArgumentNullException("localContext");
        }
        string oldPhone = ""; // to store the old Main Phone no:
        string newPhone = ""; //// to store the new Main Phone no:
    
        // get the plugin context 
        IPluginExecutionContext context = localContext.PluginExecutionContext;
    
        //Get the IOrganizationService
        IOrganizationService service = localContext.OrganizationService;
              
        //create the service context
        var ServiceContext = new OrganizationServiceContext(service);
        ITracingService tracingService = localContext.TracingService;
    
        // The InputParameters collection contains all the data passed in the message request.
        if (context.InputParameters.Contains("Target") &&
        context.InputParameters["Target"] is Entity)
        {
            // Obtain the target entity from the input parmameters.
            Entity entity = (Entity)context.InputParameters["Target"];
    
            // get the pre entity image
            Entity preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null;
            
            // get the post entity image
            Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;
    
            // get the preimage and postimage telephone1 value
            if (preImageEntity.Attributes.Contains("telephone1"))
            {
                oldPhone = (string)preImageEntity.Attributes["telephone1"];
            }
    
            if (postImageEntity.Attributes.Contains("telephone1"))
            {
                newPhone = (string)postImageEntity.Attributes["telephone1"];
            }
    
            if (newPhone != oldPhone)
            {
                try
                {
                    //Create query to get the related contacts
                    var res = from c in ServiceContext.CreateQuery("contact")
                                where c["parentcustomerid"].Equals(entity.Id)
                                select c;
    
                    foreach (var c in res)
                    {
                        Entity e = (Entity)c;
                        e["telephone1"] = newPhone;
    
                        //ServiceContext.Attach(e);
                        ServiceContext.UpdateObject(e);
                    }
    
                    ServiceContext.SaveChanges();
    
    
                }
                catch (FaultException ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                }
            }
    
        }
    }
    
  15. Now right click on CRM Package project we created in step 2 and select deploy. It will register the plugin assembly as well as step, preEntityImage and postEntityImage for the plugin. 
  16. Update the “Main Phone” on account entity and test the plugin.