Function inside AddPreSearch is not being called

I am trying to learn Microsoft Dynamics 365, In my app, I have a lookup field subject and a choice field casetype, which has 2 options and on 1 option, I want to limit the lookup to have only 2 fields, and so for other choice the other two fields should be there.

The main problem is that the function I use inside addPreSearch it never goes inside that.

I tried:

var subjectControl = formContext.getControl("subjectid");
    if (subjectControl) {
      console.log("Hello 1");
      subjectControl.addPreSearch(() => applySubjectFilter(formContext));
}

function applySubjectFilter(executionContext) {
    console.log("Custom Filter Function triggered0");
    var formContext = executionContext.getFormContext();
    console.log("Custom Filter Function triggered1");
}

And another approach I tried:

subjectControl.addPreSearch(applySubjectFilter);

function applySubjectFilter(executionContext)

I never get inside the addPreSearch but subjectid is there everything is there I am even getting inside the if block and having Hello1.

I even tried to use this code:

function subjectLimitOnCase(executionContext) {
    var formContext = executionContext.getFormContext();

    var casetype = formContext.getAttribute("casetypecode").getValue();
    console.log("Case Type is: ", casetype);

    if (casetype != null) {
        console.log("Hello 0");
        var subjectControl = formContext.getControl("subjectid");
  
        if (subjectControl) {
            console.log("Hello 1");

            // Create a custom view with a FetchXML filter
            AttachSubjectCustomView(formContext, casetype);
        } else {
            console.log("Subject lookup control is not available on the form.");
        }

        console.log("Hello 2");
    }
}

function AttachSubjectCustomView(formContext, casetype) {
    if (casetype == null) return;

    // Define the FetchXML query based on casetype
    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
        "<entity name='subject'>" +
        "<attribute name='title' />" +
        "<attribute name='subjectid' />" +
        "<order attribute='title' descending='false' />";

    if (casetype == 500000002) { // Case Type: Service Request
        fetchXml += "<filter type='or'>" +
            "<condition attribute='title' operator='eq' value='Increase Usage Limit' />" +
            "<condition attribute='title' operator='eq' value='Request Secondary Card' />" +
            "</filter>";
    } else if (casetype == 500000001) { // Case Type: Complaint
        fetchXml += "<filter type='or'>" +
            "<condition attribute='title' operator='eq' value='Amount Not Imbursed' />" +
            "<condition attribute='title' operator='eq' value='Card Charges Applied Incorrectly' />" +
            "</filter>";
    }

    fetchXml += "</entity></fetch>";

    // Define the layout XML
    var layoutXml = "<grid name='resultset' object='1' jump='subjectid' select='1' icon='1' preview='1'>" +
        "<row name='result' id='subjectid'>" +
        "<cell name='title' width='150' />" +
        "</row></grid>";

    // Generate a unique viewId
    var viewId = "{00000000-0000-0000-0000-000000000001}"; // Replace with a unique GUID
    var viewDisplayName = "Filtered Subjects Based on Case Type";

    // Apply the custom view to the subject lookup control
    formContext.getControl("subjectid").addCustomView(viewId, "subject", viewDisplayName, fetchXml, layoutXml, true);

    console.log("Custom view applied to Subject Lookup");
}

I have even added on these events also

enter image description here

enter image description here

Please tell me what can I do in my case.

For sake of clarity I am also attaching columns properties also:

Case Type:

enter image description here

Subject:

enter image description here

Upvotes: 0

Views: 66

Answers (0)

Related Questions