SBX - Search With Button

SBX - Forum Post Title

Business Process flow - change lookup filter base on a parameter on Change events

Microsoft Dynamics CRM Forum

rthompson asked a question on 10 Aug 2018 2:21 PM
My Badges

Question Status

Suggested Answer

Hi,

I have a business process flow that seem to only work if the field (Reason) that I need to do the FILTER lookup on exist in the first stage.

When I remove the Reason field from the first stage to the second stage the FILTER no longer works.

The below logic is working on the first stage.

if (Xrm.Page.getControl("header_process_new_reasonid") != null) {
 
    Xrm.Page.getControl("header_process_new_reasonid").removePreSearch(addLookupFilter);
 
    Xrm.Page.getControl("header_process_new_reasonid").addPreSearch(addLookupFilter);
}

I thought if I added the below logic to trigger on a stage change using OnLoad or OnSave still no luck.

Xrm.Page.data.process.addOnStageChange(stageChange);
Xrm.Page.data.process.addOnStageSelected(stageChange);

function stageChange() {
    Xrm.Page.getControl("header_process_new_reasonid").addPreSearch(addLookupFilter);
}

Does javascript logic only work on the first stage.

Any ideal on what I am doing wrong.

Thanks
Reply
rthompson responded on 13 Aug 2018 12:23 AM
My Badges

Hi Goutam,

I have added a try catch to all 3 areas.  OnFormLoad,  StageChange and addLookupFilter.

I am not getting any error messages.  It's confusing to me because this logic will work on the first stage.  Anything after the first stage addLookupFilter no longer works.

Reply
Suggested Answer

Hi Thompson ,

Try to add an alert beginning of  addLookupFilter() function check whether you are getting alert of not .

Also if you are not getting any alert in my earlier post "  alert("Filter apply successfully in BPF field");" which means BPF field is getting null and thats why its not adding filter criteria.

The code is correct and I don't see any error in the code .

Reply
rthompson responded on 13 Aug 2018 9:13 AM
My Badges

Hi Goutam,

I removed a lot of the code and made if very basic.  I am still having the same problem with the addLookupFilter not firing on second stage.  However the form "Reason" does work with the logic.

Could there be a problem with adding 2 hook events?

OnFormLoad

{

Xrm.Page.data.process.addOnStageSelected(stageChange);
Xrm.Page.data.process.addOnStageChange(stageChange);
}

//Stage Change
function stageChange() {
    try {

        if (Xrm.Page.getControl("new_reasonid") != null) {
            Xrm.Page.getControl("new_reasonid").addPreSearch(
            function () {
                addLookupFilter();
            });
        }

        if (Xrm.Page.getControl("header_process_new_reasonid") != null && Xrm.Page.getControl("header_process_new_reasonid") != undefined) {
            console.log("help me");
            Xrm.Page.getControl("header_process_new_reasonid").addPreSearch(
            function () {
                addLookupFilter();
            });
        }
    } catch (e) {
        //throw error
        throw new Error(e.message);
    }
}

//This function is called from the field to do the filterLookup
function addLookupFilter() {
    alert("Is this working after stage change");

}
Reply
Ravi Kashyap responded on 13 Aug 2018 11:30 PM
My Badges

Hi,

By any chance, can you share your complete JS library?

Reply
rthompson responded on 14 Aug 2018 2:45 AM
My Badges

Hi Ravi,

Here's my js library and my form properties.

Reply
Ravi Kashyap responded on 14 Aug 2018 4:33 AM
My Badges

Sorry I meant the full code in your library. I am suspecting there is some syntax error which is why its not working.

Its okay if you are not able to share. I will try the same thing on my trial and let you know how it goes.

Reply
rthompson responded on 14 Aug 2018 9:50 AM
My Badges

Great!!!  Thanks very much Ravi.  This would be great if you can get this working.

I can only copy and paste that code JS library .  Please let me know if you want me to do that.

Thanks again for all your help.

Reply
Suggested Answer

Hi Thompson,

If my understanding is correct you are using same field in both the stage?Make sure the fields are exists in the form . However I would suggest following , you are calling same function so you dont need to addOnStageChange method.Also call  stageChangeOnSelected() method in onload event as well. Try with this 

Xrm.Page.data.process.addOnStageSelected(stageChangeOnSelected);

function stageChangeOnSelected() {
    //Assuming that the reason field exists in the Main Form
    if (Xrm.Page.getControl("new_reasonid") != null) {

        Xrm.Page.getControl("new_reasonid").addPreSearch(
        function () {
            addLookupFilter();
        });     
    }
    //For BPF field
    if (Xrm.Page.getControl("header_process_new_reasonid") != null) {       

        Xrm.Page.getControl("header_process_new_reasonid").addPreSearch(
       function () {
           addLookupFilter();
       });
    }

}


function addLookupFilter()
{
    //Implement the logic 
    // Xrm.Page.getControl("header_process_new_reasonid").addCustomFilter(fetchQuery);
    // Xrm.Page.getControl("new_reasonid").addCustomFilter(fetchQuery);


}

Reply
Ravi Kashyap responded on 11 Aug 2018 5:57 AM
My Badges
Suggested Answer

Hi,

The code looks fine, if you have added onstatechange & onstageselected then it should work. Is the condition mentioned in the filter is satisfied.

If the field is added more than once that the schema name for that field will change. For example, if you add the reason field twice in the BPF the schema name for the second field will be header_process_new_reasonid_1.

Hope this helps.

Reply
Suggested Answer

Hi Thompson,

Try with this code , make sure you added the reason field in the form itself and regeister formOnload in the onload event of the form. I have putted some alert you will receive alert when the filter is applying or you can debug your code . Below code should work  -

        // Register the function in Onload Method

        function formOnload()
        {
            Xrm.Page.data.process.addOnStageSelected(stageChange);
            Xrm.Page.data.process.addOnStageChange(stageChange);
        }



        //Stage Change
        function stageChange() {
            alert("Stage Change got fired");
            if (Xrm.Page.getControl("new_reasonid") != null) {
                Xrm.Page.getControl("new_reasonid").addPreSearch(
                function () {
                    alert("Applying filter ton Form Field");
                    addLookupFilter();
                });
            }
            if (Xrm.Page.getControl("header_process_new_reasonid") != null && Xrm.Page.getControl("header_process_new_reasonid") != undefined) {
                Xrm.Page.getControl("header_process_new_reasonid").addPreSearch(
               function () {
                   alert("Applying filter ton BPF Field");
                   addLookupFilter();
               });
            }
        }

        function addLookupFilter() {
            if (Xrm.Page.getControl("statuscode") != null && Xrm.Page.getControl("statuscode").getAttribute().getValue() != null) {
                var statusCode = Xrm.Page.getAttribute("statuscode").getSelectedOption();
                var categoryId = null;
                var categoryUiName = null;
                switch (statusCode.value.toString()) {
                    case "100000004":  //Out for Calibration
                        categoryId = "{8ED431D5-2D9C-E811-80DC-005056BA7CE0}";
                        categoryUiName = "Out for Calibration";
                        break;
                    case "100000003":  //Out for Repair
                        categoryId = "{03A8C797-2E9C-E811-80DC-005056BA7CE0}";
                        categoryUiName = "Out for Repair";
                        break;
                    case "100000002":  //Remove from Service
                        categoryId = "{6C6CC53D-2E9C-E811-80DC-005056BA7CE0}";
                        categoryUiName = "Remove from Service";
                        break;
                }

                //Prepare Condition for the filter
                fetchXml = "<filter type='and'>" +
                             "<condition attribute='new_categoryid' value='" + categoryId + "' uitype='category' uiname='" + categoryUiName + "' operator='eq'/>" +
                           "</filter>";

                //Apply Filter to the FORM lookup field
                if (Xrm.Page.getControl("new_reasonid") != null && Xrm.Page.getControl("new_reasonid") != undefined) {
                    alert("Filter apply successfully in form field");
                    Xrm.Page.getControl("new_reasonid").addCustomFilter(fetchQuery);
                }
                //Apply Filter to the BPF lookup field
                if (Xrm.Page.getControl("header_process_new_reasonid") != null && Xrm.Page.getControl("header_process_new_reasonid") != undefined) {
                    alert("Filter apply successfully in BPF field");
                    Xrm.Page.getControl("header_process_new_reasonid").addCustomFilter(fetchXml);
                }
            }
        }


Reply
Suggested Answer

Ok remove the alert and check you can get next function alert or not.

 //Stage Change
        function stageChange() {
            alert("Stage Change got fired");
            if (Xrm.Page.getControl("new_reasonid") != null) {
                Xrm.Page.getControl("new_reasonid").addPreSearch(
                function () {                
                    addLookupFilter();
                });
            }
            if (Xrm.Page.getControl("header_process_new_reasonid") != null && Xrm.Page.getControl("header_process_new_reasonid") != undefined) {
                Xrm.Page.getControl("header_process_new_reasonid").addPreSearch(
               function () {              
                   addLookupFilter();
               });
            }
        }


 

Reply
Suggested Answer

Try to debug and check which line causes the issue , and put the screenshot here .

Reply
Suggested Answer

Hi Thompson ,

Try to add an alert beginning of  addLookupFilter() function check whether you are getting alert of not .

Also if you are not getting any alert in my earlier post "  alert("Filter apply successfully in BPF field");" which means BPF field is getting null and thats why its not adding filter criteria.

The code is correct and I don't see any error in the code .

Reply

SBX - Two Col Forum

SBX - Migrated JS