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

Verified

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

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

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
Ravi Kashyap responded on 19 Aug 2018 11:00 PM
Suggested Answer

Hi,

I tried the same implementation (though for oob entity and field) and this seem to work as expected. i.e. On opportunity I filter the Contact lookup which is added to the OOB BPF. The contact lookup filtered bsed on my condition. Then I update the BPG, moved the field to second stage. The filter work as expected there.

Then I added the field on stage 1 as well as stage 2. The filter stopped on stage 2. This is because when you add the control more than once, the id changes.

Also, I have noticed several time that if the filter applies on load and if you are applying the filter again then sometime it doesn't work. In these scenarios, we need to remove the previously added filter. docs.microsoft.com/.../removepresearch

Hope this helps.

Reply
rthompson responded on 19 Aug 2018 11:17 PM
My Badges

Hi Ravi,

Were you using

Xrm.Page.getControl("header_process_new_reasonid").addPreSearch(addLookupFilter);

or

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

or

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

Reply
Ravi Kashyap responded on 19 Aug 2018 11:29 PM
Verified Answer

Hi,

I have used all. "Xrm.Page.getControl("header_process_new_reasonid").addPreSearch(addLookupFilter);" is the actual javascript to filter the records whereas "addOnStageChange" & "addOnStageSelected" are the methods to attach the method when the stage is selected & stage is changed.

Here is what I have -

============================

function OpportunityOnLoad() {

   addLookupFilter();

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

   Xrm.Page.data.process.addOnStageChange(addLookupFilter);

}

function addLookupFilter() {

   var account = Xrm.Page.getAttribute("parentaccountid").getValue();

   if (account == null) {

       if (Xrm.Page.getControl("header_process_parentcontactid") !== null) {            

           Xrm.Page.getControl("header_process_parentcontactid").addPreSearch(filter);

       }

   }

   else {

       if (Xrm.Page.getControl("header_process_parentcontactid") !== null) {

           Xrm.Page.getControl("header_process_parentcontactid").addPreSearch(filter);

       }

   }

}

function filter() {

   var fetchXml = "<filter type='and'> <condition attribute='firstname' operator='like' value='s%' /></filter>";

   Xrm.Page.getControl("header_process_parentcontactid").addCustomFilter(fetchXml);

}

============================

Reply
rthompson responded on 19 Aug 2018 11:46 PM
My Badges

Thanks Ravi,

I will rebuild everything and try again.

Thanks very very much for taking the time to look into this.

I really appreciate you doing this.

Thanks again.

Reply
Ravi Kashyap responded on 19 Aug 2018 11:29 PM
Verified Answer

Hi,

I have used all. "Xrm.Page.getControl("header_process_new_reasonid").addPreSearch(addLookupFilter);" is the actual javascript to filter the records whereas "addOnStageChange" & "addOnStageSelected" are the methods to attach the method when the stage is selected & stage is changed.

Here is what I have -

============================

function OpportunityOnLoad() {

   addLookupFilter();

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

   Xrm.Page.data.process.addOnStageChange(addLookupFilter);

}

function addLookupFilter() {

   var account = Xrm.Page.getAttribute("parentaccountid").getValue();

   if (account == null) {

       if (Xrm.Page.getControl("header_process_parentcontactid") !== null) {            

           Xrm.Page.getControl("header_process_parentcontactid").addPreSearch(filter);

       }

   }

   else {

       if (Xrm.Page.getControl("header_process_parentcontactid") !== null) {

           Xrm.Page.getControl("header_process_parentcontactid").addPreSearch(filter);

       }

   }

}

function filter() {

   var fetchXml = "<filter type='and'> <condition attribute='firstname' operator='like' value='s%' /></filter>";

   Xrm.Page.getControl("header_process_parentcontactid").addCustomFilter(fetchXml);

}

============================

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
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
Ravi Kashyap responded on 19 Aug 2018 11:00 PM
Suggested Answer

Hi,

I tried the same implementation (though for oob entity and field) and this seem to work as expected. i.e. On opportunity I filter the Contact lookup which is added to the OOB BPF. The contact lookup filtered bsed on my condition. Then I update the BPG, moved the field to second stage. The filter work as expected there.

Then I added the field on stage 1 as well as stage 2. The filter stopped on stage 2. This is because when you add the control more than once, the id changes.

Also, I have noticed several time that if the filter applies on load and if you are applying the filter again then sometime it doesn't work. In these scenarios, we need to remove the previously added filter. docs.microsoft.com/.../removepresearch

Hope this helps.

Reply

SBX - Two Col Forum

SBX - Migrated JS