Dynamically change SearchSQL for an SSA field client-side

I the article "Almost everything you wanted to know about Search Select Advanced Fields (A round up about lookups)" https://community.sagecrm.com/partner_community/b/hints_tips_and_tricks/archive/2015/06/23/almost-everything-you-wanted-to-know-about-search-select-advanced-fields-a-round-up-about-lookups.aspx there is a link to "Dynamically change SearchSQL for an SSA field client-side" https://community.sagecrm.com/partner_community/b/hints_tips_and_tricks/archive/2010/01/25/dynamically-change-searchsql-for-an-ssa-field-client-side.aspx. However the link seems to be broken.

Does anyone have a working link to the article?

Thanks

  • 0

    I don't have a link to the article but I have a function that does what you want (I believe this code, or something similar at least, was what was in that article):

    function SetSSASearchSQL (strFieldname, searchSQL) {

    if (!window['NavUrl' + strFieldname]) return; // if the function does not exist we must not be in edit mode

    // replace SearchSql in the NavUrl function

    eval('window[\'NavUrl\'+strFieldname]=' + window['NavUrl' + strFieldname].toString().replace(/&SearchSql=[^&]*/i, '&SearchSql=' + escape(searchSQL)).replace('NavUrl' + strFieldname, '') + ';');

    // replace SearchSql in the ShowPopup function

    eval('window[\'ShowPopupOptionsWindow\'+strFieldname]=' + window['ShowPopupOptionsWindow' + strFieldname].toString().replace(/&SearchSql=[^&]*/i, '&SearchSql=' + escape(searchSQL)).replace('ShowPopupOptionsWindow' + strFieldname, '') + ';');

    }

  • 0

    Don't have a link, so no idea if my home-brew solution is anything like what that article prescribed, but here's how I do it:

    Assume we have a Search Select Advanced field called 'Branch Address' (full name opli_branchaddress). There's another one called 'Company Address', and I want to filter the Branch Address by the CompanyAddressID. This is the function I call when the Company Address is selected:

    function setSearchSqlForBranchAddress(compAddrID) {

    var eSrc = encodeURIComponent( 'companyaddressid=' + compAddrID);

    var fnStr = window.NavUrlopli_branchaddress.toString();

    fnStr = fnStr.replace(/&SearchSql=[^&]*/i, '&SearchSql=' + eSrc);

    fnStr = fnStr.replace('NavUrlopli_branchaddress', '');

    eval('window.NavUrlopli_branchaddress = ' + fnStr);

    }

  • 0

    Brilliant thanks John and Chris.

    I did use the method from the article ~ 3 years ago but I don't have any of that code available. Was a little surprised when the article wasn't available.

  • 0

    Here is my version, say I have custom field on Opportunity and i want to look up Person but restrict it to the current company (I know you can already do this, but this is a simple example)

    var myrecord = CRM.GetContextInfo("company","comp_companyid");

    SearchSQL = "pers_companyid=" + myrecord;

  • 0

    I think that approach is server side only and not dynamic unless you force a page refresh. The other two options can dynamically change the SQL client side based on, for example, a change to another field.

  • 0

    I confess. It was me that unpublished the article. It was from 2010 and used a technique suitable only for antique versions of IE.

  • 0

    A warning to everyone - this no longer works after the below patches

    • Sage CRM 2021 R2.5
    • Sage CRM 2022 R2.4
    • Sage CRM 2023 R2.2

    I expect this will no longer work in 2024 too.

    The required "SearchSQL" parameter has been removed as it can be abused to perform SQL injection attacks, so filtering dynamically client side is now impossible.

    https://help.sagecrm.com/on_premise/en/2023R2/2023R2.2/Content/Patches/2023R2.2/Patch-ReadMe.htm
    CRM-2133

    This removal has/will break a number of customisations we have made for customers and will also affect current versions of Qnect 200 (.e.g. Account selection on quotes/orders).

  • 0 in reply to John Kingsbury

    Which version of Qnect? As we have customers on 2023 R2.2 and all is working OK with Qnect. 

  • 0 in reply to Matthew Shaw

    All versions. If you create a quote/order, you will see that it will display all accounts in the accounts list.

  • 0 in reply to John Kingsbury

    I stand corrected, the highest we have gone is 2023 R2.1 

    If things break if we go to v2024 then we can't upgrade customers. 

  • 0 in reply to Matthew Shaw

    I have added my voice to it and raised it with Sage support

  • 0 in reply to Matthew Shaw

    I believe we now have a solution.

    The SearchSQL parameter can still be used, however if there is no "SearchSQL = 'abc'" in the CreateScript of the field, it will not be present in the URL.

    So we have modified the code to add the parameter if it is missing.

    We will be doing a bit more testing, then will get some proper patches out.

    In the meantime, in WWWRoot/js/Custom/z99_99qnectsystem.js, replace the SetSSASearchSQL method with the following two methods:

        SetSSASearchSQL: function (strFieldname, searchSQL) {
            if (!window['NavUrl' + strFieldname]) return; // if the function does not exist we must not be in edit mode
            // replace SearchSql in the NavUrl function
            var inlineFuncString = window['NavUrl' + strFieldname].toString();
            eval('window[\'NavUrl\'+strFieldname]=' + this.ModifySearchSQLInJs(inlineFuncString, searchSQL).replace('NavUrl' + strFieldname, '') + ';');
    
            // replace SearchSql in the ShowPopup function
            var popupFuncString = window['ShowPopupOptionsWindow' + strFieldname].toString();
            eval('window[\'ShowPopupOptionsWindow\'+strFieldname]=' + this.ModifySearchSQLInJs(popupFuncString, searchSQL).replace('ShowPopupOptionsWindow' + strFieldname, '') + ';');
        },
        ModifySearchSQLInJs: function (js, searchSQL) {
            if (js.indexOf("&SearchSql=") < 0) {
                //SearchSQL is not in the URL, we need to add it.
                //Best way is to find another parameter we know exists and add the SearchSQL parameter to the end.
                var searchTableRegExp = /&SearchTable=([^&]*)/i;
                var searchTable = searchTableRegExp.exec(js)[1];
                return js.replace(/&SearchTable=[^&]*/i, '&SearchTable=' + searchTable + '&SearchSql=' + escape(searchSQL));
            }
            else {
                return js.replace(/&SearchSql=[^&]*/i, '&SearchSql=' + escape(searchSQL));
            }
        },

  • 0 in reply to John Kingsbury

    Hi John

    I have done as instructed but my demo system still doesn't work correctly (cleared browser cache as well) 



  • 0 in reply to Matthew Shaw

    You are missing the final few closing braces

  • 0 in reply to John Kingsbury

    I would be dangerous if I had my head screwed on correctly.