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 7
  • 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
A2 A21
A3 A31
B B1 B11
B2 B21
B3 B31

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

<script language="javascript">
//add as many as you want here

function FilterSelection(main, linked)
var list = $(linked).html();
$(main).change(function(e) {
var selected = $(main).val();
if (selected == "--None--") return;
$(linked).append("<option value='' selected=true>--None--</option>");

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:

  • Now that's looking good.

  • Nicely documented- this worked great for me when a client needed to limit a Case "Category" selection list based on the selection of Case_Channelid. Thank you!

  • is there a way of getting this to work on a workflow action screen.

    i have set this up on the opportunity but the client wants to hide the change button and edit the fields using a workflow action button

  • Hi Sufyan,

    Yes for sure you can get this to work on a workflow screen. Depending on your CRM version there are some ways to do this.

    Easiest would be to add the code to your js file and have it load in on the screen you want. I would look at these two articles -

    community.sagecrm.com/.../sage-crm-7-2-script-libraries-reusing-client-side-code-between-screens.aspx and


    See the part about the "In an external .JS file in the..."

    Remember that you need to tell your code to run only on the correct workflow pages, so something like this would be useful:


    if (crm.getArg("Act") == "260" || crm.getArg("Act") == "263" || (crm.getArg("Act") == "400" && /*...add some more code here to check if the field exists*/)){

    /*...paste the code in here to run only on oppo workflow pages, ACT code might be wrong...*/}

    This code uses the CRM action query string param to check what page is loaded to determine if it should execute that code on the page. It will make the system more stable and run faster as it will not execute unnecessary code on a page that it is not required for, or you could just make the code load on all pages and then it should work everywhere, but I haven't tested this yet so not sure.

    Hope this helps.

  • Hi, How do you get around having more than say 10 values in field 2?  Based on the logic described in the article.  Thanks.

  • Hi Grega,

    The key/code for field2 is not a number, it's a letter. So when Field 1 = A, Field 2 = A1...

    When you look at the javascript you can see I pick up the starts with section of the code. So as long as field 2 starts with A, it should be fine to add any amount of numbers after it, making the selection for field 2 seemingly infinite. So something like A11 or even A11111 is also allowed. The starts with value that gets passed as the param is the trick.

    Planning field 3 values will be the issue then. So really the planning is a problem since when the code for 3 is set, you will have some issues with that then.

  • Hi Conrad, Thanks for the response.