Microsoft Gold Certified Partner
Skip Navigation LinksMagnetism Blog > Nathan Eccles Blog

Nathan Eccles Blog

  • Populating Lookup Fields with JavaScript in CRM 4.0

    In a previous blog post I went over how to populate fields in CRM. You may notice in that blog post I did not cover how to populate lookup fields, and this simply was because at the time I didn’t know an effective way to do this. After this problem coped up a few more times I decided it was about time to find a usable solution, so I took to the internet in an attempt to learn some tricks.
    Many of you, like me, may have wondered why simply setting the lookup field to a value and moving off the field with JavaScript doesn’t force the field to lookup the value as it would if you were manually entering data. Now apparently this feature can be accessed simply by adding “_ledit” to the end of the attribute name.
    Knowing this, populating lookup fields becomes almost as easy as populating regular fields, we just have to bear in mind that data value should, when possible, exactly match the record name. A limitation of this method is that if the record you are trying to look up is “Test” but there is a record named “Test 2” or “Tester” it will not work correctly. This is because when the lookup field searches for “Test” it finds more than 1 result, and therefore is uncertain of which record you want.
    This method is however excellent for populating a field such as Price List, which is often the same, and typically won’t have any repeats in names.
    Following is the code you require to populate a lookup field;

    If the lookup field is on the default tab;

              // Check to make sure there isn’t already a value in the lookup field.
    if (crmForm.all.attribute.DataValue == null) {
              // Change focus to the ledit section of the lookup field.
        crmForm.all.attribute_ledit.focus();
              // Input the text value of the name of the record you wish to populate in the lookup field.
        crmForm.all.attribute_ledit.value = "Your Text Here";
    }

    Rename attribute to the name of the lookup field you are populating and change Your Text Here to the name of the record you wish to look up.

    Now unfortunately the focus() command doesn’t work unless the correct tab is open. This means that if the lookup field you wish to populate is not on the main tab you require a few more lines of code for the population to work correctly.

    If the lookup field is NOT on the default tab;

              // Make sure there is no data already entered in the lookup field you wish to populate.
    if (crmForm.all.attribute.DataValue == null) {
              // Navigate to the appropriate tab.
        crmForm.all.tab1Tab.click();
              // Change focus to the ledit section of the lookup field.
        crmForm.all.attribute_ledit.focus();
              // Input the text value of the name of the record you wish to populate in the lookup field.
        crmForm.all.attribute_ledit.value = "Your Text Here";
              // Wait 1 millisecond then change to back to the primary tab. This Timeout is required for this action to work correctly.
    setTimeout("crmForm.all.tab0Tab.click()",1);
    }

    Rename attribute to the name of the lookup field you are populating, 1 to the tab number the attribute is located on and Your Text Here to the name of the record you wish to look up.
    NOTE: 0 is the first tab (default tab), 1 is the second tab, 2 is the third tab etc.

    Using the Timeout is a workaround as I couldn’t get the JavaScript to fire the last command to bring the focus back to the default tab without it. If anyone has a solution for this I would love to hear it!

    Full story

    Comments (0)

  • Retrieving Data from Lookup Fields

    If you’ve ever tried using JavaScript with lookup values in CRM I’m sure you will have found that they are quite a lot different from the other types of attributes. With only a small amount of JavaScript code you can retrieve the following values from any lookup value.

    Below is the code I used to get those 4 values:

    Here’s an explanation of the code:

    // Defines the Lookup field Value.
    var lookupItem = crmForm.all.mag_productid.DataValue;

    // Check to make sure the Lookup field contains data (if there is no data and you attempt to populate the value fields you will get an error message).
    if (lookupItem != null) {
    // Populate the field “Text Value” with the text value of the lookup field. (The name of the record you have selected)
        crmForm.all.mag_textvalue.DataValue = lookupItem[0].name;
    // Populate the field “Entity Type” with the schema name of the lookup field. (The type of record displayed e.g. “product”)
        crmForm.all.mag_entitytype.DataValue = lookupItem[0].typename;
    // Populate the field “GUID” with the GUID value of the lookup field. (The unique ID given to the record you have selected)
        crmForm.all.mag_guid.DataValue = lookupItem[0].id;
    // Populate the field “Entity Type Code” with the entity type code of the lookup field. (The unique number value given to the entity)
        crmForm.all.mag_entitytypecode.DataValue = lookupItem[0].type;
    }

    To use this code:

    1. Open the entity which contains the lookup field you wish to pull these data values from.
    2. Create 4 new nvarchar attributes to capture the 4 values shown above.
    3. Place the 4 attributes on your form and put the following code in the onLoad form event, and onChange event of the lookup field ensure the Event is enabled box is checked;

      var lookupItem = crmForm.all.lookupfield.DataValue;

      if (lookupItem != null) {
          crmForm.all.textvalue.DataValue = lookupItem[0].name;
          crmForm.all.entitytype.DataValue = lookupItem[0].typename;
          crmForm.all.guid.DataValue = lookupItem[0].id;
          crmForm.all.entitytypecode.DataValue = lookupItem[0].type;
      }

    4. Replace lookupfield with the attribute name of the lookup field you are using.
    5. Replace textvalue with the attribute name of the field you wish to populate with the attribute name.
    6. Repeat this for entitytype, guid, and entitytypecode.
    7. Preview your changes to ensure that they are working correctly and then publish your entity.

    Now it’s unlikely that this code would be used as is, but hopefully with a little imagination and creativity you will be able to utilise it to make your CRM system more dynamic and smooth flowing.
    If you are looking for more JavaScript code please look through the other Magnetism blog posts, or post here and we’ll see if we can help you out.

    Full story

    Comments (0)

  • Hiding Fields Based on User Selection

    Recently I came across the problem of one entity needing the choice between two look-up fields; Product and Custom Product. Now I could have just left both the fields there and let the user work out which one to use, but because I believe in ease of use I decided to look into some client-side scripting (JScript).
    The first step to take was to create the two relationships (lookups), and an additional bit field called “Type”.

    1. Navigate to Settings, Customization, Customize Entities.
    2. Open the entity you wish to edit.
    3. Create two N:1 Relationships, one to each of the entities you wish to link to.
    4. Create a new Attribute with the type of bit which will indicate to users which lookup field they require.
      Note: Renaming “Yes” and “No” will make it easier for your users to identify the type of record they wish to look up. For example I used “Product” and “Custom Product”.
    5. Publish the entity.
    6. Click on Forms and Views and open the Form view.
    7. Add the three attributes you have just created onto the form (I would suggest formatting as shown below).

    Now we need to make only the appropriate field appear based on the bit selection.

    1. For each of the three attributes;
      1. Select the attribute and click Change Properties on the right.
      2. Go to the Name tab.
      3. Note down their Name (NOT Display Name).
    2. On the main form edit page click on Form Properties, ensure OnLoad is selected, then click Edit.
    3. Check the box Event in enabled. and copy in the following code:

      if (crmForm.all.defining_value.DataValue == true) {
          crmForm.all.lookup1_c.style.display = "inline";
          crmForm.all.lookup1_d.style.display = "inline";
          crmForm.all.lookup2_c.style.display = "none";
          crmForm.all.lookup2_d.style.display = "none";
      }
      else {
          crmForm.all.lookup1_c.style.display = "none";
          crmForm.all.lookup1_d.style.display = "none";
          crmForm.all.lookup2_c.style.display = "inline";
          crmForm.all.lookup2_d.style.display = "inline";
      }
    4. Replace defining_value with the name of the bit attribute, lookup1 with the name of the first lookup field on the form, and lookup2 with the name of the second lookup field on the form.
      Note: The “_c” is defining the display name of the attribute while “_d” is defining the attribute field.
    5. Copy the code you just entered.
    6. Click OK twice.
    7. Select the bit attribute and click Change Properties.
    8. Click the Events tab and then Edit.
    9. Check the box Event is enabled. and paste the code you copied in step 5.
    10. Click OK twice.
    11. Preview your form and make sure the code is working as expected.
      Note: If you have not published the entity since creating the bit attribute, it will not display properly.
    12. Save and Close the form.

    My problem after I had that working was that whichever field was displayed needed to be a required field. If I just set them both as required fields then I will never be able to save a record as I will always get an error message telling me I need to fill in all required fields (the hidden one).
    To get around this I added a little more JScript code which makes the hidden field read-only. This works because required fields which are read-only do not require an entry, and therefore do not display the error message.

    Note: The below steps are only needed if the fields you wish to hide are Required Fields.

    1. Click N:1 Relationships and open the relationships you created earlier.
    2. In the Relationship Attribute area change the Requirement Level to “Business Required”.
    3. Save and Close the relationships.
    4. Open the OnLoad event for the entity and replace the previous code with the following code:

      if (crmForm.all.defining_value.DataValue == true) {
          crmForm.all.lookup1_c.style.display = "inline";
          crmForm.all.lookup1_d.style.display = "inline";
          crmForm.all.lookup1.Disabled = false;
          crmForm.all.lookup2_c.style.display = "none";
          crmForm.all.lookup2_d.style.display = "none";
          crmForm.all.lookup2.Disabled = true;
      }
      else {
          crmForm.all.lookup1_c.style.display = "none";
          crmForm.all.lookup1_d.style.display = "none";
          crmForm.all.lookup1.Disabled = true;
          crmForm.all.lookup2_c.style.display = "inline";
          crmForm.all.lookup2_d.style.display = "inline";
          crmForm.all.lookup2.Disabled = false;
      }
    5. Replace defining_value with the name of the bit attribute, lookup1 with the name of the first lookup field on the form, and lookup2 with the name of the second lookup field on the form.
    6. Copy the code into the OnChange event of the bit attribute, replacing the previous code.
    7. Preview the form to ensure everything looks correct.
    8. Save and Close the form.
    9. Publish the entity.
    10. Create one of each type of lookup to ensure you are not getting an error message about the hidden field.

    Remember to always check usability of the system when editing this code as any mistakes could stop you from being able to edit the entity.
    Enjoy!

      Full story

      Comments (0)

    1. CRM Keyboard Shortcuts

      A few months back I stumbled across a rather useful trick for navigating Dynamics CRM 4.0. I’m sure many of you already use keyboard shortcuts like CTRL + S and ALT + F4 in programs such as Word and Excel, but are you aware that there are actually keyboard shortcuts for the Dynamics CRM web client?

      Below is a list of what I believe to be the 10 most helpful and under-used keyboard shortcuts for Microsoft Dynamics CRM 4.0 as shown on the Microsoft Website.

      Keys What They Do
      CTRL + S or SHIFT + F12  Saves the record
      ALT + S Saves and closes the record
      CTRL + SHIFT + S Saves the record and opens a new one
      ALT + F4 Closes the active record
      CTRL + D Deletes the record
      ESC (Escape) Cancels the edit and closes the record
      CTRL + SHIFT + F Expand / Collapse the Form Assistant
      TAB Moves forward through the record’s fields
      SHIFT + TAB Moves backward through the record’s fields
      CTRL + A Selects all text within a current field or list

      All of these shortcuts can be used in the CRM for Outlook client when opening and editing records.

      However there are also shortcuts for Outlook which may prove valuable to you. I have put the top 10 most applicable for use with the CRM for Outlook client.

      Keys What They Do
      CTRL + SHIFT + I  Switches to Inbox
      CTRL + 1 Switches to Mail
      CTRL + 2 Switches to Outlook Calendar
      CTRL + 3 Switches to Outlook Contacts
      CTRL + 4 Switches to Outlook Tasks
      CTRL + SHIFT + A Creates a new Appointment
      CTRL + SHIFT + B Displays the Address Book
      CTRL + SHIFT + C Creates a new Contact
      CTRL + SHIFT + K Creates a new Task
      F7 Checks Spelling

      There are also some navigation shortcut keys for the CRM web client, although they are much less helpful than those for Outlook. These shortcut keys are simply used to expand drop down menus within CRM’s default top navigation pane.

      Keys What They Do
      ALT + N Expands the New Activity menu
      ALT + C Expands the New Record menu
      ALT + G Expands the Go To menu
      ALT + T Expands the Tools menu
      ALT + I Opens the Advanced Find window
      ALT + H Expands the Help menu

      The use of these keyboard shortcuts should considerably increase your productivity with CRM as time taken for navigation within Outlook, and editing of records within CRM should have decreased.

      I hope you enjoy your new efficiency!

      If you know of any other useful keyboard shortcuts for either the CRM web client or CRM for Outlook please post here and let us know!

      Full story

      Comments (0)

    2. Dynamics CRM 4.0 – Desktop Shortcuts

      If you’re a user which is frequently saving new records into CRM, this trick could help to save you time and effort. In this blog I will explain how to create desktop shortcuts which link to creating new records in Dynamics CRM.

      If Internet Explorer is your Default Browser

      Most businesses set Internet Explorer as their default browser, and considering Dynamics CRM 4.0 is only compatible with Internet Explorer this works well. If this is the case for you please use the following 6 steps. If your default browser is NOT Internet Explorer please refer to the next section.

      1. Open your CRM in your web browser.
      2. Navigate to the entity for which you wish to create a desktop shortcut.
      3. Click the option to create a new record.
      4. Drag the small Internet Explorer icon located directly to the left of the URL (at the top of the window) onto your desktop.

        If your CRM does not display a URL simply press “Ctrl + n” to open the same window in a regular browser window which does display the URL.
      5. Rename the shortcuts if so desired.
      6. For multiple desktop shortcuts repeat steps 1-5 for as many entities as you require.

      If Internet Explorer is NOT your Default Browser

      If you are not using Internet Explorer as your default browser it becomes slightly more difficult to create working desktop shortcuts. However the following 10 steps should aid you in this process. If there is any confusion about this process please don’t hesitate to comment.

      1. Right click on your desktop and click New > Shortcut.
      2. Enter the following text "%ProgramFiles%\Internet Explorer\iexplore.exe" (or browse to find your Internet Explorer shortcut).
      3. Give the shortcut the name of the entity you are linking it to eg “New Lead”.
      4. Open CRM and navigate to the entity you wish to create the desktop shortcut for.
      5. Click the option to create a new record.
      6. Highlight and copy the URL (located at the top of the window).
        If your CRM does not display a URL simply press “Ctrl + n” to open the same window in a regular browser window that does display the URL.
      7. Right click the desktop shortcut created in Step 1-2 and click Properties.
      8. Click the end of the Target location (after the .exe”), put a space, and paste in the URL you copied in Step 6.
      9. Apply the settings.
      10. Repeat steps 1-9 for as many entities as you require.

      Optional – Adding Icons to your Shortcuts

      As you may notice if you create multiple desktop shortcuts, it can become difficult to distinguish between which shortcuts relates to which entities. By simply adding icons to each of the shortcuts you are able to make them clearly distinguishable.

      To Add Icons to your CRM Desktop Shortcuts

      1. Right click the shortcut and click Properties.
      2. Click the “Change Icon...” button.
      3. Click “Browse...”
      4. Browse to the location of the file you wish to display as the shortcut icon.
      5. Select the appropriate icon file and click “Open”
      6. Click “OK” twice.
      7. Repeat steps 1-6 for all shortcuts you wish to put icons on.

      Our amazing graphical designer David has offered to create high quality icons for all the main entities. These will be available for download in one of his later posts.

      Full story

      Comments (0)

    3. Pre-Populating Fields in Microsoft Dynamics CRM 4.0

      Ever noticed a field you always fill out with the same information every time you create a new record? Ever wondered why you can’t just have that field pre-populate with the required text?
      There is actually a way to do this using a little trick we like to call ‘Client-Side Scripting’. The name might sound complicated, but hopefully after reading this post you’ll be able to pre-populate all the fields you want!

      Throughout this section of the blog the images will relate to the application of this process to pre-populate the Country/Region field with text “New Zealand”.

      1. Navigate to Settings, Customization, and then Customize Entities.
      2. Open the entity for which you wish to have a field pre-populated.
      3. Select Attributes and locate the field which you wish to pre-populate by looking at the Display Name. When you have found the attribute, note down its Name.
      4. Select Forms and Views and open the Form view.
      5. In the right task-bar open Form Properties.
      6. From the Event List click OnLoad and then Edit.
      7. Check the box Event is enabled and copy in the following code:

        if (crmForm.all.<attribute_name>.DataValue == null){
            crmForm.all.<attribute_name>.DataValue = "<desired text here>";
        }

        (To pre-populate multiple fields simply copy the code multiple times.)
      8. Replace the <attribute_name> with the name of the attribute identified in step 3, and <desired text here> with your desired display text.
      9. Click OK twice then on the top task bar select Preview and then Create Form.
      10. Ensure the field is being pre-populated correctly and close the preview form.
      11. Click Save and Close twice and then Publish the entity.

      Here is a worked example of what the code is defining:

      Using a similar method we are able to pre-populate a different text field based on the value of another.

      In this instance I will demonstrate this by populating the Country/Region attribute based on the City attribute.

      1. Follow the above steps finding both the name of the attribute you wish to populate, as well as the name of the attribute that defines that population.
      2. Copy the following code into the OnLoad event:

        if (crmForm.all.<attribute_name1>.DataValue == "<text to check>"){
            crmForm.all.<attribute_name2>.DataValue = "<desired text here>";
        }

      3. Change <attribute1_name> to the name of the defining attribute and <attribute2_name> to the name of the attribute you wish to populate. Change <text to check> to the text that you wish to check is in place and <desired text here> to the text you wish to populate in the related field.

        To make this work most effectively, copy and paste the code you have just entered into the OnChange event of the defining field.


      4. To do this, return to the Form Customization window and double click on the defining attribute.
      5. Select the tab Events and then click Edit.
      6. Paste in the code you entered into the OnLoad event.
      7. Ensure that the Event is enabled Checkbox is checked.
      8. Once the code is in both the OnLoad event of the form, and OnChange event of the defining attribute, preview and test your customizations, save the entity, and publish it.

      Full story

      Comments (0)

    4. Editing the Site Map – Dynamics CRM 4.0

      The Site Map is an xml file which controls the display of the main navigation in Dynamics CRM. By editing it we are able to change what the user sees in the navigation pane when they use CRM. Editing the Site Map can be a daunting task if you don’t know what you’re doing. However, it is also a useful file to edit to give your users a smoother experience and keep them out of areas they have no use for.

      By default the Dynamics CRM navigation pane looks like the one on the left. By clicking the Personalize Workplace link you are able to add the sections: Sales, Marketing, Service, and Scheduling. After doing so your navigation will look like the one on the right.

      However, using this method each user has to personalize their own workspace, and you’re still left with the un-needed tabs down the bottom. By editing the sitemap we are able to choose what is displayed in the Workplace tab for all users, and remove the tabs which are no longer needed.

      To get started, let’s export the Site Map.

      1. Navigate to Settings, Customization, and then Export Customizations.
      2. Find and export the Site Map (SiteMap.xml) and save it somewhere easy to access.
      3. Extract the file from the zip folder and open it with whichever web editing program you prefer (notepad works fine).

       

       

       

       

       

       

      The following shows a list of all the Elements you will see and use when editing the Site Map:

      Element Name Description
      Area Specifies the areas to show in the navigation pane.
      Description Contains a description in one language.
      Descriptions Contains a set of localized descriptions.
      Group Specifies a group of subareas.
      Privilege Controls whether a subarea is displayed for a given user.
      SiteMap Specifies the root node for the site map.
      SubArea Specifies the elements displayed in the left navigation pane for each area.
      Title Contains a title in one language.
      Titles Contains a set of localized titles for an area.

       

      For more information on attributes I suggest looking at the Dynamics CRM SDK.

      The first step I would advise taking when editing the Site Map is to permanently reveal any of the existing groups in the Workplace tab which you require.

      1. Find the Groups within the Workplace Area (right up the top).
      2. Find the particular group you would like to reveal..
      3. Remove the code: IsProfile=”true”

      Below is a section of a sitemap which is set to reveal the Sales area in the Workplace, but not the Marketing area.

      • To remove any entities you do not wish to be displayed simply remove the SubArea (<SubArea... ...” />).
      • To move navigation links around, cut and paste the SubAreas into their appropriate positions.
      • To put navigation links into the Workplace tab (or anywhere else) that wouldn’t normally be there, copy and paste the appropriate SubArea into the correct position.

      To hide these sub-areas you need to add a privilege tag (<Privilege... ...” />). This tag allows you to dictate who sees the navigation links based on what privileges their User Role allows. The following example shows that if the user’s role does not allow them to delete leads, then the SubArea is not displayed to them. (Multiple privileges may be required by separating the values with commas. Eg “Read,Create,Delete”)

      Note also that the privilege tag must be placed between the <SubArea> and </SubArea> tags. This means that the ...” /> from the initial SubArea should be changed to ...”>

      Here’s the list of the available Privilege values from the Dynamics CRM SDK:

      • All
      • AllowQuickCampaign
      • Append
      • AppendTo
      • Assign
      • Create
      • Delete
      • Read
      • Share

      When all SubAreas in an Area are restricted due to privileges, the whole tab will be removed from that users display.

      To apply the changes you have made to the site map:

      1. In CRM navigate to Settings, Customization, Import Customizations
      2. Browse for the site map file you have edited
      3. Click Open and then Upload (Note: If the site map is incorrectly formed you will receive the message “Either the file could not be uploaded, or this is not a valid Customization file.”)
      4. Click Import Selected Customizations

      If you are looking to make changes to the sitemap layout you can also look into using the Microsoft Dynamics CRM Demonstration Tool which offers a graphical user interface to edit the sitemap.

      Full story

      Comments (0)

    5. Dynamics CRM 4.0 Applications Exam

      On Thursday, after many hours of study and revision, I sat the Microsoft Dynamics CRM 4.0 Applications exam. Initially I was a little frightened of sitting the exam, but I’d booked it in and there was no backing out. For the 4 days prior to my exam my evenings were filled with revision of CRM concepts, sales, marketing, service management and service scheduling. All these thoughts in my head must have paid off however as achieved a rather respectable pass grade in the exam. Now confident that my revision is working I will shortly be moving on to complete the Customization exam, the Installation exam, and finally, the Extending CRM exam. Looks like my next few weeks will be filled with forms and views, SQL servers and active directories, and all the wonders Extending CRM has in store for me! In summation I would conclude that although the Applications exam may seem daunting, if you get stuck into your study, and think through exam questions logically, the 70% required to pass is a reasonably attainable goal.

      Full story

      Comments (0)