Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Unanswered

Javascript to show custom values based on another field

Posted on by 2
Hi
 
I want to show a field and some values in a field called Sub Status1 based on the values in the Status. I have the follow script, but currently all it does is hide the field. It will not show any it or any values when the Status is pending, or expired. I have copied the code from another table which has the same behaviour that our vendor created. I have configured the Events area on the form and field. I am not a code writer and is anyone able to see if there is something wrong with the code i am using
 
if (typeof (Uni) == "undefined") {
    Uni = {
        __namespace: true
    };
};
if (typeof (Uni.Waikato) == "undefined") {
    Uni.Waikato = {
        __namespace: true
    };
};
if (typeof (Uni.Waikato.Es) == "undefined") {
    Uni.Waikato.Es = {
        __namespace: true
    };
};
if (typeof (Uni.Waikato.Es.Uni_agreement) == "undefined") {
    Uni.Waikato.Es.Uni_agreement = {
        __namespace: true
    };
};

Uni.Waikato.Es.Uni_agreement.subStatussCache = null;
//On Load
Uni.Waikato.Es.Uni_agreement.GetSubStatus = function (executionContext) {
    if (arguments === null || arguments.length === 0 ||
        typeof arguments[0].getFormContext !== "function") {
        Xrm.Navigation.openAlertDialog({ text: "executionContext has not been passed into function:" + arguments.callee.name });
        return;
    }
    var formContext = executionContext.getFormContext();
    var subStatus = formContext.getControl("uow_substatus1");
    if (subStatus != null) {
        Uni.Waikato.Es.Uni_agreement.subStatussCache = subStatus.getOptions();
    }
}
Uni.Waikato.Es.Uni_agreement.LimitsubStatus = function (executionContext) {
    if (arguments === null || arguments.length === 0 ||
        typeof arguments[0].getFormContext !== "function") {
        Xrm.Navigation.openAlertDialog({ text: "executionContext has not been passed into function:" + arguments.callee.name });
        return;
    }
    var formContext = executionContext.getFormContext();
    var agreementStatus = formContext.getAttribute("statuscode");
    var subStatus = formContext.getAttribute("uow_substatus1");
    if (agreementStatus != null && subStatus != null) {
        if (agreementStatus.getValue() != null) {
            var subStatusControl = formContext.getControl("uow_substatus1");
            var optionsToAdd;
            switch (agreementStatus.getValue()) {
                case 942210002:  //Pending
                    optionsToAdd = [942210000, 942210002]; //Negotiation, Signing
                    break;
                 case 942210004:  //Expired
                    optionsToAdd = [942210001, 942210003]; //Action Required, Not Renewing
                    break;
                default:
                    break;
            }
            //Add options
            if (optionsToAdd != null && Uni.Waikato.Es.Uni_agreement.subStatussCache != null) {
                subStatusControl.clearOptions();
                for (var i = 0; i < optionsToAdd.length; i++) {
                    var newOption = Uni.Waikato.Es.Uni_agreement.subStatussCache.find(element => element.value == optionsToAdd[i]);
                    if (newOption != null) {
                        subStatusControl.addOption(newOption);
                    }
                }
                subStatusControl.setVisible(true);
            } else {
                formContext.getControl("uow_substatus1").setVisible(false);
            }
        } else {
            formContext.getControl("uow_substatus1").setVisible(false);
        }
    }
}
  • Suggested answer
    Ben356 Profile Picture
    Ben356 8 on at
    Javascript to show custom values based on another field
    Hello,
     
    It’s a bit complex to advise on the code shared without a clear picture on the logical names and data types of the fields involved.
     
    Given that you are not so much in code, the expected functionality can also be achieved by using business rules which only entail dragging and dropping components, they are a fast way to set field values, hide fields on particular field states.
     
    Kindly review the following document for your consideration.
    https://learn.microsoft.com/en-us/dynamics365/customerengagement/on-premises/customize/create-business-rules-recommendations-apply-logic-form?view=op-9-1

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans