Login / Register  search  syndication

          CRM Developer

Ryan Farley on CRM Development for SalesLogix, SageCRM, MSCRM, .NET - and all things geek

Hiding and Showing Fields at Runtime

Something that makes any web application a great one IMO is making it behave like a windows app as much as possible. Making a web application behave the way you'd expect a windows app to behave is what will get you a huge buy-in from end-users. The MSCRM client is no different. I think it is important to take every opportunity to make things happen the same way you would in a windows application.

For example, why show all fields on a form, even when some are only relevant when certain conditions exist? Hide them if the user doesn't need them and show them when your condition is satisfied. Let's say you have a picklist and a textbox. The picklist (schema name: new_category) represents a type of category for the entity. If the user selects “Non-Billable” from the list you want them to have the ability to enter a reason (schema name: new_nonbillreason) why the category is non-billable. There's no need to cause confusion for the user by showing them the new_nonbillreason field, unless they've selected “Non-Billable” as the category, right? Easy enough task. You'll need to add code for the form's OnLoad event, as well as for the OnChange event for the new_category picklist field.

The best way to hide a field, is not to hide the field itself, but hide the td cells that they are in. The fields label is just text, not in a label or span or something you can get to, so hiding the field only will leave the caption visible. However, you can hide the td cell that the caption is in. Each field will have two td cells. One that the caption is in, and another that the field's control is in. The caption's td cell will be named the same as the schema name, but will have a “_c” at the end. The field itself will be in a td cell that is named the same as the schema name but ending with a “_d”. So, for our example, we'll need to show/hide two td cells, one named “new_nonbillreason_c” and “new_nonbillreason_d”. Make sense? Here's the code for the OnChange event for the “new_category” picklist field.

// get the text value of the category field 
var categorytype;
categorytype = crmForm.new_category.SelectedText;

if (categorytype != 'Non-Billable')
{
    // hide the nonbillreason field & caption cells 
    crmForm.all.new_nonbillreason_c.style.display = 'none';
    crmForm.all.new_nonbillreason_d.style.display = 'none';
}
else
{
    // show the nonbillreason field & caption cells 
    crmForm.all.new_nonbillreason_c.style.display = '';
    crmForm.all.new_nonbillreason_d.style.display = '';
}

 

A few things to note here. Remember that we are hiding td cells. So when we set the “display” style attribute to none we will see that the table will collapse where the field is if it is the only field on that row. If you wanted to have everything stay in the same spot (and not have the collapsing effect for the table) then you can change the code so that you toggle the “visibility” style attribute instead. Then, technically the td cells are still there, just not showing. The code would look like this:

if (condition)
{
    // hide the nonbillreason field & caption cells 
    crmForm.all.new_nonbillreason_c.style.visibility = 'hidden';
    crmForm.all.new_nonbillreason_d.style.visibility = 'hidden';
}
else
{
    // show the nonbillreason field & caption cells 
    crmForm.all.new_nonbillreason_c.style.visibility = 'visible';
    crmForm.all.new_nonbillreason_d.style.visibility = 'visible';
}

 

That's it. Don't forget you'll want to add the same code for the form's OnLoad event. Unless the category fields default value is “Non-Billable“ you'll want that hidden from the start. However, if it's an update then you should first check the value and then set the nonbillreason's visibility based on the current value of category.

var CREATETYPE = 1;                
var UPDATETYPE = 2;

switch (crmForm.FormType)
{
    case CREATETYPE:
        // for creates, initialize the field to hidden
        crmForm.all.new_nonbillablereason_c.style.visibility = 'hidden';
        crmForm.all.new_nonbillablereason_d.style.visibility = 'hidden';
        break;

    case UPDATETYPE:
        // for updates, first check the value of category and conditionally hide
        var categorytype;
        categorytype = crmForm.new_category.SelectedText;
        
        if (categorytype != 'Non-Billable')
        {
            crmForm.all.new_nonbillablereason_c.style.visibility = 'hidden';
            crmForm.all.new_nonbillablereason_d.style.visibility = 'hidden';
        }
        break;
}

 

One thing to point out though, if the field you are hiding is “Business Required” then it will still be checked for a value when the user attempts to close the form - which means they won't be able to close the form since the field isn't visible for them to enter a value into. In these cases where the field must be required, you'll also need to toggle that at runtime as well. We'll look at that next time.

What's This?

About Ryan Farley

   Ryan Farley is the Director of Development for Customer FX Corporation and the creator of slxdeveloper.com.


Related Content
   Ping a Remote Server Using VBScript
At times you might build a customization in the SalesLogix LAN client that relies on networked resources.
Posted on May 19, 2008 by Ryan Farley to CRM Developer
 
   KnowledgeSync
Business Activity Monitoring (BAM) has become the way that many companies are generating real return from
Content from Partner Products Section
 
   ReadOnly and Disabled Fields
When you need to set fields disabled or readonly at runtime, you need to keep in mind the type of field y
Posted on Mar 06, 2006 by Ryan Farley to CRM Developer
 
   Working with CRM 3.0 Sample Chapter Available
Mike Snyder and Jim Steger of Sonoma Partners have announced the availability of the sample chaper on Wor
Posted on Feb 28, 2006 by Ryan Farley to CRM Developer
 
   Easier Navigation with Browser Tabs
A common complaint I hear those who are more used to the modality of Windows-based CRM applications is th
Posted on Feb 24, 2006 by Ryan Farley to CRM Developer
 
Comments

 

CRM Developer said:

Along the same lines as showing/hiding form fields and labels at runtime, it is often useful to change the text of field labels based on other events. This is an easy enough task which isn't really all that different than showing or hiding the label.
February 16, 2006 6:47 PM
 

Karlo Swart said:

Simply did not work in the test setup. I received no JS errors and alerts functioned prior and after attempting to hide a field. No idea why it didn't work.
May 22, 2006 9:13 AM
 

Jonas Deibe said:

Note! Dont forget to remove field(s) in print mode aswell. Its easy to forget that...
June 20, 2006 7:01 AM
 

Richa said:

Can we change the field position at runtime.
August 1, 2007 2:39 AM
 

banhinc said:

All of the client-side code will use DHTML & JScript. You cannot do anything without using some capabilities of DHTML. No all of DHTML is supported by CRM. Try using..
object.style.left
object.style.top
September 7, 2007 4:52 PM
 

Sahan said:

Hi ,

I am having trouble in getting a Auto Number to generate in the leads page . I have added one more field as Lead ID and I want an Auto Number to geneate. Logically what should happen is to get the last record of the database and then add one to it . Is there a java script that can be used for this or is there a reference that I can do to the attributes like  "crmForm.all"

Thanks

Sahan

June 18, 2008 8:30 PM

Leave a Comment

(required)  
(optional)
(required)  
Add
All contents Copyright © 2009 Customer FX Corporation
Customer FX Corporation
2324 University Avenue West, Suite 115
Saint Paul, Minnesota 55114
Tel: 800.728.5783
 Follow us on Twitter @CustomerFX






Login / Register