Adding a “Complete/Send & New” button to Activities in CRM 2011/2013 (Part 1 of 2)

If you have ever had to send multiple emails/faxes to the same person or create follow-up tasks for completed phone calls/tasks, you may have wondered why CRM doesn’t come with a button for “Complete & New.” Just like the “Save & New” button that comes OOTB on every entity except it would call the send/complete operation after the Save and then create a new entity. If this is a common process for your users, then adding this button will cut down on button clicks and make the data entry process more streamlined. In this post, I will walk you through the steps to add this button to your own instance.

The Script

New_activity.js

This script applies to any activity so, if you don’t have one already, create a new Jscript web resource named “new_activity.js”.  The basic process is as follows …

  1. Retrieve the Entity Name and Object Type Code.
  2. Email and Fax activities have a distinct “complete” function from the other activities.  So, call the “send” method if email or fax and “SaveAsCompleted” for other activities.
  3. Both of these methods trigger a save operation.  If the save fails due to missing data, then the form is left dirty.  In that case, we would want to interrupt the operation so that a new form doesn’t open until the previous one is saved and closed.  So, we check the Xrm.Page.data.entity.getIsDirty() property and just return if it’s true.  Otherwise, continue on.
  4. If the Regarding field is populated, which it is in nearly all situations, then we want to retain this on the new entity.  So, we retrieve the value and pass it into the “extraqs” parameter on the “new” entity URL. 
    1. If additional fields need to be copied over, you can use the “extraqs” also, but there are other sites that go over using this parameter and that isn’t the topic of this post.
  5. If the Regarding field isn’t populated, then we use the built-in Xrm.Utility.openEntityForm() function to open a new blank activity record.

Copy the script below and paste it into the script editor window for the new web resource.  (I have also included the JS file in zip file attached to this post.) 

function SaveAsCompleteAndNew(){     
     var entityName = Xrm.Page.data.entity.getEntityName();
     var entityType = XrmServiceToolkit.Common.GetObjectTypeCode(entityName);

     if (entityName == "email" && entityName == "fax")
     {
          //Send the activity
          send();
     }
     else
     {
          // Attempt to save Activity and Mark it as Complete
          SaveAsCompleted();
     }

     // check to see if form is still dirty indicating that the save failed due to missing data. 
     // Bail out of function if so.
     if (Xrm.Page.data.entity.getIsDirty())
         return;

     // if regarding field populated, open new window that preserves the data in this field
     if (Xrm.Page.getAttribute("regardingobjectid").getValue() != null)
     {
          var regardingTypeName = Xrm.Page.getAttribute("regardingobjectid").getValue()[0].entityType;
          var regardingType = XrmServiceToolkit.Common.GetObjectTypeCode(regardingTypeName)
          var regardingId = Xrm.Page.getAttribute("regardingobjectid").getValue()[0].id;
          regardingId = regardingId.substring(1,37);
          window.open("./main.aspx?etc="+entityType+"&extraqs=%3fetc%3d"+entityType+"%26pId%3d%257b"+regardingId+"%257d%26pType%3d"+regardingType+"%26pagemode%3diframe%26preloadcache%3d1378318512096&pagetype=entityrecord");
     }
     else if (entityName != null) {
          //Open new phone call activity form
          Xrm.Utility.openEntityForm(entityName);
     }
}

Save and publish the resource. 

XRMServiceToolkit

The XRMServiceToolkit is required in order to dynamically obtain the object type codes for the activity.  You can download the entire toolkit from CodePlex (http://xrmservicetoolkit.codeplex.com).  We implemented our solution using version 2.0.0, which requires json2 and jquery1.9.0 or higher.  Once I downloaded the toolkit code, I created and published new web resources for each of these three Jscript libraries (xrmservicetoolkit, json2 and jquery1.9.0).  We could have avoided having to use this toolkit by hard-coding the object type codes for the OOTB activities, but then the solution wouldn’t have been extensible to custom activities so we opted for using the toolkit.  For each of the activities that need the new button, you will need to open their Main forms and add these new web resources under the Events tab on the Form Properties.

Adding buttons to the ribbon

First off, you will need a ribbon editor to add the new buttons onto the ribbon.  I prefer the Visual Ribbon Editor for its simplicity and ease of use, but any one will do.  You will need new icons for the buttons.  I built my own by combining SaveAsComplete/Send icon with the “Save and New” icon.  

img1

I have included my icons in 16×16 and 32×32 in the attached zip file for your use.  After you have added these icons as web resources into CRM, then you can add the buttons to the ribbon with the ribbon editor.  For the new buttons, you will need to copy the Display and Enable rules from the OOTB Send/Complete button.    The “Display Rules” are different between email, fax, and all other activities so you will have to review the existing Send/Complete buttons.  Below are my settings for the new button on the Email entity and there are no “Enable Rules” attached to the button.

 img2

img3

img4

Once you save the changes in the ribbon editor, you are all set!  When you open an email, you should see something like this …

  img5

Here is a shot of the Phone Call …

img6

You should now have a handy button that will save your users a few mouse clicks and valuable time going forward.

As promised, here are the files … Complete & New Files

Your email address will not be published. Required fields are marked *

Phone: 312-602-4000
Email: marketing@westmonroepartners.com
222 W. Adams
Chicago, IL 60606
Show Buttons
Share On Facebook
Share On Twitter
Share on LinkedIn
Hide Buttons