Not filter Products when change Account or contact

I’ve a field at Lead table named Product (crf2e_service) , And at product table there are 3 bool fields named Individual, Private, Governorate,

Contact is individual, and Account contain a classification field (Private, or Governorate)

At Creating lead i want when user choose Existing contact filter product according to individual type only, and when user choose account filter according to classification

The JS code i write works only at the first time i choose contact or account, when i remove or choose another type not clearing filter or change it

I but onload method at Form OnLoad Event Handler
And applyProductFilter at Existing Contact, And Existing Account OnChange Event Handler

var isProductFilterApplied = false;

function onLoad(executionContext) {
    var formContext = executionContext.getFormContext();
    var formType = formContext.ui.getFormType();

    if (formType !== 1) return; // Only apply for new records

    console.log("onLoad executed, form type: " + formType);

    // Add OnChange event handlers for parentcontactid and parentaccountid
    var parentContactField = formContext.getAttribute("parentcontactid");
    var parentAccountField = formContext.getAttribute("parentaccountid");

    if (parentContactField) {
        parentContactField.addOnChange(function(context) {
            applyProductFilter(context);
        });
    }

    if (parentAccountField) {
        parentAccountField.addOnChange(function(context) {
            applyProductFilter(context);
        });
    }

    // Apply filter on load
    applyProductFilter(executionContext);
}

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

    var parentContactField = formContext.getAttribute("parentcontactid");
    var parentAccountField = formContext.getAttribute("parentaccountid");

    var parentContact = parentContactField ? parentContactField.getValue() : null;
    var parentAccount = parentAccountField ? parentAccountField.getValue() : null;

    console.log("applyProductFilter executed");

    clearProductFilter(formContext);

    if (parentAccount) {
        var accountId = parentAccount[0].id.replace('{', '').replace('}', '');
        console.log("Parent Account ID: " + accountId);
        filterProductsByAccount(formContext, accountId);
    } else if (parentContact) {
        var contactId = parentContact[0].id.replace('{', '').replace('}', '');
        console.log("Parent Contact ID: " + contactId);
        filterProductsByContact(formContext, contactId);
    } else {
        console.log("No parent contact or account selected");
    }
}

function clearProductFilter(formContext) {
    var productField = formContext.getControl("crf2e_service");

    if (productField) {
        console.log("Clearing product filter");
        productField.removePreSearch(productFilterHandler);
        productField.addPreSearch(function () {
            productField.addCustomFilter("<filter type='and'></filter>");
        });
        isProductFilterApplied = false;
    }
}

function productFilterHandler() {
    // Dummy function to use as a reference for adding/removing the handler
}

function filterProductsByContact(formContext, contactId) {
    Xrm.WebApi.retrieveRecord("contact", contactId, "?$select=contactid").then(
        function success(result) {
            console.log("filterProductsByContact executed");
            var filter = "<filter type='and'><condition attribute='crf2e_isindividual' operator='eq' value='true' /></filter>";
            applyProductLookupFilter(formContext, filter);
        },
        function (error) {
            console.log("Error retrieving contact: " + error.message);
        }
    );
}

function filterProductsByAccount(formContext, accountId) {
    Xrm.WebApi.retrieveRecord("account", accountId, "?$select=ownershipcode").then(
        function success(result) {
            console.log("filterProductsByAccount executed");
            var classification = result.ownershipcode;
            var filter = "";

            if (classification === 2) { // Assuming 2 is the value for Private
                filter = "<filter type='and'><condition attribute='crf2e_isprivate' operator='eq' value='true' /></filter>";
            } else if (classification === 1) { // Assuming 1 is the value for Government
                filter = "<filter type='and'><condition attribute='crf2e_isgovernment' operator='eq' value='true' /></filter>";
            }

            applyProductLookupFilter(formContext, filter);
        },
        function (error) {
            console.log("Error retrieving account: " + error.message);
        }
    );
}

function applyProductLookupFilter(formContext, filter) {
    var productField = formContext.getControl("crf2e_service");

    if (productField) {
        console.log("Applying product lookup filter: " + filter);
        productField.addPreSearch(function () {
            productField.addCustomFilter(filter);
        });

        isProductFilterApplied = true;
    }
}

At Creating lead i want when user choose Existing contact filter product according to individual type only, and when user choose account filter according to classification

The JS code i write works only at the first time i choose contact or account, when i remove or choose another type not clearing filter or change it

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật