Dynamic Selection Lists - Rework

Hints, Tips and Tricks

Technical Hints Tips and Tricks that cover customization and development using Sage CRM. API usage and coding are covered.

Dynamic Selection Lists - Rework

  • Comments 1
  • Likes

This post is a revamp post on how to manage linked selection lists (drop downs) in Sage CRM. This was posted before on the CRM community here - https://community.sagecrm.com/partner_community/b/hints_tips_and_tricks/archive/2008/01/14/dynamic-selection-lists.aspx and there is a nice post on Greytrix here - http://www.greytrix.com/blogs/sagecrm/2014/09/11/linked-selection-fields-through-client-side-scripting/. I used some ideas from there and made the code more streamline and easily readable. 

What this customization will do is it will give you the capability to link selection lists to one another without much need for changing the onchange script every time you need to link a field to another. This will also help if you need to link more than 2 fields to each other. In my example I will be linking 3 fields, but you can extend this as far as you like, by just adding one line of code.

First let's look at the setup in CRM for the selection lists we will be using

The first thing you need to know is that the code in the selection list is the important part here. The translation (what the user sees on screen) does not matter in this case. We need to match the codes to all the related selection lists. I started by using an alphabetical code like 'A' for my first selection list, first option. Every option on my list will then be an alphabetical letter. So following that it will be B, C, D... and so on. This however does not mean it will be restricted to 26 selections, as when I get to Z, I can just make a new one with AA, BB, CC... So this list is basically endless, until you get to ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ. 

Example of my selection list. The field is called case_field1 with selections 1 - 3. At the bottom you will see what it would look like adding a 4th selection

Now lets look at my linked field, case_field2

From that you can see that the list names follow, however this is only for illustration purposes as the codes are the ones that do the work. When looking at the codes one can see that the items/selections you want to match with the first list (case_field1) will have the same code as that item followed by a number, this can be any number but for simplicity I labelled the number starting at 1. The second item linked to the same list will be number two. So in this example it will be A1, A2, A3... These items will all link to 'Selection 1' in list 1. Other items will be B1, B2, those link to 'Selection 2' on list 1/case_field1.

The pattern you will see here is:

Field1 = [Field1code] = Field1Code
Field2 = [Field1Code]NUMBER = Field2Code
Field3 = [Field2Code]NUMBER = Field3Code
Field4 = [Field3Code]NUMBER

An example of this will be:

Field1 = A
Field2 = A1
Field3 = A11
Field4 = A111

or (these will all be codes and not translations)

Field1 Field2 Field3
A A1 A11
A12
A13
A2 A21
A22
A23
A3 A31
A32
A33
B B1 B11
B12
B2 B21
B22
B3 B31
B32

So now that we have the setup out of the way, let's look at the code:

<script language="javascript">
crm.ready(function(){
FilterSelection("#case_field1","#case_field2");
FilterSelection("#case_field2","#case_field3");
//add as many as you want here

});
function FilterSelection(main, linked)
{
var list = $(linked).html();
$(main).change(function(e) {
var selected = $(main).val();
$(linked).html(list);
if (selected == "--None--") return;
$(linked).children("option:not([value^='"+selected+"'])").remove();
$(linked).append("<option value='' selected=true>--None--</option>");
});
}
</script>

NB - The above code sample should be placed in the Custom Content section of the appropriate page.

We have a crm.ready() function here that fires when the page is loaded and then runs the FilterSelection() function. This will run how many times you want it to if you just add more lines to the ready function. In the first call - FilterSelection("#case_field1","#case_field2"); we are telling the function that our main field is case_field1 and the second field that needs to filter based on the main field is case_field2. I add these to the parameters of the function FilterSelection(), but I also have to add them in double quotes and preceding with a # character. This is because I am using JQuery base to find my selections and then modify them. If we were using the crmjs api then we can't use methods like append. The function FilterSelection runs every time it is called with the main field and linked field parameters. It then checks what values are in the linked field and attaches a 'change' event onto the main field, so that every time the main field changes values it will fire the code linked to it.

In this case the code does the following:

1. Get the selected value
2. If the value is '--None--', then skip the code
3. Find all the values in the linked list and if they DO NOT start with a value from the main list, then remove those options from the list
4. Add a '--None--' option to the list again after it has been removed.

It is as simple as that. Now what you will have is something like this:

Comments