Hide tab in any entity form in Microsoft Dynamics CRM 4 using Javascript in onLoad event of the form.

In many cases, you might want to remove one of the tabs of an entity form in Microsoft Dynamics CRM 4. This can be specifically helpful in two cases:

1- If you want to hide and show tabs depending on a picklist value selection such as contact type: Hide Work details if contact is of type internal contact for example

2- If you want to hide the Administration and Notes tab from an entity form while these tabs are locked and cannot be removed from the entity form.

To hide a tab in an entity form, you can use the following Javascript sample script:

//---------------------------Hide a tab by passing tab name to the function
HideTab('Administration');
//replace "Administration" with your tab name
function HideTab (tabText)
{
 var tab = FindTab(tabText);
 if (tab)
 {
  tab.style.display = "none";
 }
}
function FindTab(tabText)
{
 var tabBar = document.getElementById("crmTabBar");
 if (tabBar)
 {
  var tabs = tabBar.childNodes;  

  for (var i = 0, len = tabs.length; i < len; i++)
  {
   var currentTab = tabs[i];
   if (currentTab.innerText === tabText)
   {
    return currentTab;
   }
  }
 }
}
//-----------------end of script--------

Maxjerin has added a new code suggestion. Maxjerin’s code hides a tab based on the onChange event of a field on the form. If the primary contact “name” field is maxjerin, the tab is displayed. Otherwise the tab is hidden.

I have updated the post to include Maxjerin’s code – Thanks Maxjerin:

Here is the code that I used to HIDE/UNHIDE a tab when I wanted to show it for only one particular user and hide for others. The code includes some alert messages just for debugging purpose which you might find useful to understand the code. You may remove them once done.

This section goes into the onChange section of the Primary Contact lookup field

if (crmForm.all.primarycontactid.DataValue == null )
{
var strTabStatus = TabStatus(‘Profile’);
if ( strTabStatus == ”)
{
alert(“Primary Contact none, hiding the tab”);
HideTab(‘Profile’);
}
}
else if (crmForm.all.primarycontactid.DataValue[0].name == ‘maxjerin’)
{
alert(“Primary Contact p f, unhiding the tab”);
UnHideTab(‘Profile’);
//replace “Administration” with your tab name
}
else if (crmForm.all.primarycontactid.DataValue[0].name != ‘maxjerin’)
{
var strTabStatus = TabStatus(‘Profile’);
if (strTabStatus == ” )
{
alert(“Primary Contact other than maxjerin, Hiding the tab”);
HideTab(‘Profile’);
}
}
function HideTab (tabText)
{
var tab = FindTab(tabText);
if (tab)
{
tab.style.display = “none”;
}
}
function UnHideTab (tabText)
{
var tab = FindTab(tabText);
if (tab)
{
tab.style.display = “”;
}
}
function FindTab(tabText)
{
var tabBar = document.getElementById(“crmTabBar”);
if (tabBar)
{
var tabs = tabBar.childNodes;
for (var i = 0, len = tabs.length; i < len; i++)
{
var currentTab = tabs[i];
if (currentTab.innerText === tabText)
{
return currentTab;
}
}
}
}
function TabStatus(tabText)
{
var tabBar = document.getElementById(“crmTabBar”);
if (tabBar)
{
var tabs = tabBar.childNodes;
for (var i = 0, len = tabs.length; i < len; i++)
{
var currentTab = tabs[i];
if (currentTab.innerText === tabText)
{
if (currentTab.style.display == ‘none’)
{
alert(“tab already hidden, TAB NAME: ” + currentTab.innerText );
return “none”;
}
else
{
alert(“tab is visible, TAB NAME: ” + currentTab.innerText );
return “”;
}
}
}
}
}
//-----------------------------------------------------------------------
Please comment below if you have any feedback or anything to add.
Thanks

Hide, Display, Resize and Rename left navigation links, CRM fields and attributes using Javascript for Microsoft Dyanmics CRM 4.

Scripts for Resizing, Hiding and renaming left navigation links, fields and attributes in Microsoft Dynamics CRM 4 using Javascript. This post is just a collection of some Javascript code for various common scripts that is frequently used in CRM form events, CRM entity events, etc. These script blocks can be used in OnLoad, OnSave events for CRM forms and OnChange events for CRM fields.

//------- Resize CRM form in the onload event
window.resizeTo(screen.availWidth * 0.85, screen.availHeight * 0.85);
//-------Rename left menu link / Left Navigation. Example: Contacts
var navItem = document.getElementById(’navContacts’);
navItem.innerHTML = navItem.innerHTML.replace(’>Contacts’,'>Students’);
//------Hide left menu link / Navigation. Example: Workflow
var navLeftItem = document.getElementById(’navAsyncOperations’);
navLeftItem.style.display = ‘none’;
//-----Hide Left navigation menu item link of a CRM form based on
// a value in a picklist on the same form.
//Get contacts left navigation menu item element.
var navLeftItem = document.getElementById(’navContacts’);
//if picklist (customer type code) value is equal to 1 (1st item in picklist) then
//hide left menu item (contacts link is used as an example), otherwise, show it.
if(crmForm.all.customertypecode.DataValue == 1)
{
 navLeftItem.style.display = ‘none’;
}
else
{
 navLeftItem.style.display = ‘inline’;
}
 //-----Hide a CRM field on a form. example: the "name" field.
crmForm.all.name_c.style.display = 'inline';
crmForm.all.name_d.style.display = 'inline';
//-----Hide a CRM field on a form (example: the "name" field)
//based on the selected value in a picklist
if(crmForm.all.customertypecode.DataValue == 1)
{
         crmForm.all.name_c.style.display = 'inline';
         crmForm.all.name_d.style.display = 'inline';
}
else
{
         crmForm.all.name_c.style.display = 'none';
         crmForm.all.name_d.style.display = 'none';
}  ////Add this code to the OnLoad event of the form and
////to the on change event of the picklist attribute (field) as well
//---------Function to hide CRM field in a form. 
//Function accept fieldname as a parameter and
//boolean parameter to remove (hide) entire row on form
HideField('name'); //replace "name" with your field/attribute name
 
function HideField( fieldName, removeEntireRow )
{
 // Always hide the elements, even if we will be hiding the whole row.
 // This allows us to show another field in this row later without this
 // one showing up.
 var elem = crmForm.all[fieldName + "_c"];
 if( elem != null ) elem.style.display = "none";
 elem = crmForm.all[fieldName + "_d"];
 if( elem != null ) elem.style.display = "none";     
 
 if (removeEntireRow)
 {
  var elem = crmForm.all[fieldName + "_d"];
  if( elem != null ) elem.parentElement.style.display = "none";
 }
}

 This post was written quickly from memory and some code blocks here and there, so you might find some minor spelling mistakes. Please comment below if you find any issues with the script blocks or if you want me to extend this post to include additional script.

Script to Hide Add Existing Record button and Add New Record button using Javascript in Microsoft Dyanmics CRM

If you want to hide the two top left  buttons in the assicated view of any entity,  Add Existing Record and Add New Record Buttons, you can use the following javascript code i the onLoad Event of the Microsofty Dyanmics CRM 4 Form.  This post relies on and is an extension to the existing useful post by Dave Hawes.

 //This javascript should be added to the onLoad event for the form of the entity where you want to hide buttons for the associated views of other entities

//-------Hide Add Existing Record Button Only----Method 1----
HideAssociatedViewButton('new_account_new_project', 'Add existing Project to this record');
function HideAssociatedViewButton(loadAreaId, buttonTitle)
{
    var navElement = document.getElementById('nav_' + loadAreaId);
    if (navElement != null)
    {
        navElement.onclick = function LoadAreaOverride()
        {
            // Call the original method to launch the navigation link
            loadArea(loadAreaId);
            var associatedViewIFrame = document.getElementById(loadAreaId + 'Frame');
            if (associatedViewIFrame != null)
            {
                associatedViewIFrame.onreadystatechange = function HideTitledButton()
                {
                    if (associatedViewIFrame.readyState == 'complete')
                    {
                        var iFrame = frames[window.event.srcElement.id];
                        var liElements = iFrame.document.getElementsByTagName('li');
                        for (var i = 0; i < liElements.length; i++)
                        {
                            if (liElements[i].getAttribute('title') == buttonTitle)
                            {
                                liElements[i].style.display = 'none'; //this will hide and align properly
                                liElements[i].style.visibility = 'hidden'; // this is needed when re-sizing cause button to re-appear.
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
}

//—End of Hide Add existing button Only

//——————Hide Add New Record Button and Add Existing Button ———

//Hide Both Buttons-----------
HideAssociatedViewButtons('new_account_new_project', ['Add existing Project to this record', 'Add a new Project to this record']); //Where "Project" is a custom entity.
//OR Hide Add Existing Button Only-----Method 2-------
HideAssociatedViewButtons('new_account_new_project', ['Add existing Project to this record']);  //Where "Project" is a custom entity.

function HideAssociatedViewButtons(loadAreaId, buttonTitles){
    var navElement = document.getElementById('nav_' + loadAreaId);
   if (navElement != null)    {
        navElement.onclick = function LoadAreaOverride()        {
            // Call the original CRM method to launch the navigation link and create area iFrame
            loadArea(loadAreaId);
            HideViewButtons(document.getElementById(loadAreaId + 'Frame'), buttonTitles);
        }
    }
}

function HideViewButtons(Iframe, buttonTitles) {
    if (Iframe != null ) {
        Iframe.onreadystatechange = function HideTitledButtons() {
            if (Iframe.readyState == 'complete') {
                var iFrame = frames[window.event.srcElement.id];
                var liElements = iFrame.document.getElementsByTagName('li');

                for (var j = 0; j < buttonTitles.length; j++) {
                    for (var i = 0; i < liElements.length; i++) {
                        if (liElements[i].getAttribute('title') == buttonTitles[j]) {
                             liElements[i].style.display = 'none'; //this will hide and align properly
                             liElements[i].style.visibility = 'hidden'; // this is needed when re-sizing cause button to re-appear.
                            break;
                        }
                    }
                }
            }
        }
    }
} 

Credit goes to Dave Hawes Blog post mentioned above.

 

SCRIBE: Return the GUID of an entity record in Microsoft Dynamics CRM using a lookup field value.

I am working on an Integration project between Microsoft Dynamics CRM and SAP. I am using Scribe as the Integration tool/application.

The source in this case is SAP. The target is Microsoft Dynamics CRM. I am using Microsoft Dynamics CRM Scribe Adaptor.

I’m not an expert in Scribe (as yet) and I wanted to do a simple step. I wanted to get the GUID of a record in Microsoft CRM entity using a lookup field value.

The whole Scribe step is just an update to the Account form. I have the Contact name and I need to get the Contact GUID to input this GUID value (pointer to the Contact) into the Account form primary contact field. This is an N:1 relationship between Account and Contact.

So if the primary Contact name of an Account record is John Smith, the question was: how can I get the GUID of the John Smith contact to update the Account record.

Thanks to my colleague John Ball, the answer is to use the simple DBLookup function (formula).

To do that: Select the target Scribe CRM adaptor field. In my case this is custom_account_primarycontactid. Select it and then click formula. In the formula window look for the DBLooukup function. You can use the DBLookupCached function for an enhanced performance if your values are not changing before or after in any of the steps.

The formula should be something like that:

DBLOOKUPCached(S2 , “T”, “contactentity”, “contactname”, “custom_account_primarycontactid” )

 

That should be it. It’s quite simple.

Renaming Left Navigation Menu items (leftnav) of an Entity in Microsoft Dynamics CRM 4

I will try to make this as simple and straight to the point as possible.

This post is to explain how to to change the name and rename one or more of the left menu items – leftnav – items of an Entity, for example the Account entity or the Contact entity or any other Custom entity. In summary, this should be done using a script in the onload event of the entity form and apparently can NOT be done using the Isv.Config file.

So for example, when you open an account record, on the left hand side of the Account form you will have the left navigation list of entities which are: Activites, History, Sub-Accounts, Contacts, Relationshiips, Workflows, Opportunities, Leads, Quotes, Orders, Invoices, cases, contracts, marketing lists and campaigns.

To rename any of these entities or any other custom entity has a 1:N relationship with the Account entity, you will need to put some javascript code (or script really) in the OnLoad event of the Account Form. This code will look for the name of the left navigation item (link to an entity) and rename it to whatever you want. You can not do this in ISV.CONFIG as you can only add new links to ISV.CONFIG but you can not edit already existing left menu items in this file as they don’t exist and are not displayed.

So, in the OnLoad event of the entity that you want to rename its left navigation menu items, you will need to put the fulling code (or something like it):

var navItem = document.getElementById(‘leftNavMenuItemID’);
navItem.innerHTML = navItem.innerHTML.replace(‘>CurrentLinkValue’,’>NewLinkValue’);

This code will look for the menu item that has an id (‘leftNavMenuItemID’) in your chosen entity form and replace its current link name to a new link name.

So for example:

var navItem = document.getElementById(‘navContactsMenuItem’);
navItem.innerHTML = navItem.innerHTML.replace(‘>Contacts’,’>Employees’);

If you input this code in the OnLoad event of the Account entity Form, this code will look for the “Contact” entity link in the left navigation and rename it from Contacts to Employees.

You can apply the same to any other entity either it is a customisable (system) or custom entity. You will need to use a HTML tool such as Microsoft’s Internet Explorer Developer Toolbar to find out the ID of the navigation item that you want to rename (‘navContactsMenuItem’).

Please let me know if you have a question or if you want me to clarify more.

Thanks,

Mohamed Ibrahim