Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Hide/Show button based on two different lookup values

(0) ShareShare
ReportReport
Posted on by 901

Hello everyone,

I created a button using the RibbonWorkbench tool on the Work order table.

I would like to show/hide the button based on the below simple criteria:

If the work order status = "Scheduled" and the Work Order Type = "Visit" and the Primary Incident Type = "Visit" the button should be visible else it will not be visible on the ribbon.
FormCond.png

It seems that to achieve this requirement, I need to inject a JS as a CustomRule and JS action to the command for this button as the setup that I did below:
WorkbenchRule.pngEnableRuleWorkbench.png

The JS code that I did is the below is working and returning data as expected as I have tested it in OnLoad event of the form as below:

BtnWOVisVal.pngBtnJSResult.png

function getFormatedName(query){
    if(query.getValue()!== null)return query.getValue()[0].name.toLowerCase();
    else return null
}

function RibbonShowHideButton(executionContext) {
    debugger;
    let formContext = executionContext.getFormContext();
    let status = formContext.getAttribute("msdyn_systemstatus").getValue();
    console.log(status);
    if(status === "690970001" || 690970001){
        let work_type = formContext.getAttribute("msdyn_workordertype");
        console.log(work_type);
        let incident_type = formContext.getAttribute("msdyn_primaryincidenttype");
        console.log(incident_type);
        let agreement = formContext.getAttribute("msdyn_agreement");
        let service =formContext.getAttribute("msdyn_serviceaccount").getValue();
        let flag = false;

         
        if(flag === false){
            if(getFormatedName(work_type) === "visit" && getFormatedName(incident_type) === "visit"){
                console.log("COndition entered");
                return true;
            }
        }
    }
}
function refreshRibbon(executionContext){
	var formContext = executionContext.getFormContext();
	formContext.ui.refreshRibbon();
	
	
}

However, after I published and opened a record that satisfies the above condition, the button is always hidden and not visible at all.

I tried to debug the button by using the command checker but it does not show the details needed.
COmmandBarTrouble.png

Could you please advise why the button is always hidden and not showing?
Would highly appreciate it if you can provide an example.

Thank you,
EBMRay

  • EBMRay Profile Picture
    901 on at
    RE: Hide/Show button based on two different lookup values

    Asmaa Bouhmidi

    Thank you so much, it worked perfectly!

  • Suggested answer
    Asmaa Bouhmidi Profile Picture
    141 on at
    RE: Hide/Show button based on two different lookup values

    do not forgot to publish the solution

    1452.ShowHide.gif

    2022_2D00_04_2D00_20_5F00_10h04_5F00_46.png2022_2D00_04_2D00_20_5F00_10h05_5F00_01.png2022_2D00_04_2D00_20_5F00_10h05_5F00_07.png

  • EBMRay Profile Picture
    901 on at
    RE: Hide/Show button based on two different lookup values

    Hi Asmaa Bouhmidi ,

    Thank you for replicating the same scenario.

    Could you please provide screenshots of the ribbon command settings?

    I changed the code to the below but it is still not showing at all:

    function RibbonShowHideButton(executionContext) {
        debugger;
        let formContext = executionContext;
        let status = formContext.getAttribute("msdyn_systemstatus").getValue();
        let buttonVisible = false;
        console.log(status);
        if(status === "690970001" || 690970001){
            let work_type = formContext.getAttribute("msdyn_workordertype");
            console.log(work_type);
            let incident_type = formContext.getAttribute("msdyn_primaryincidenttype");
            console.log(incident_type);
            let agreement = formContext.getAttribute("msdyn_agreement");
            let service =formContext.getAttribute("msdyn_serviceaccount").getValue();
            let flag = false;
    
             
            if(flag === false){
                if(getFormatedName(work_type) === "visit" && getFormatedName(incident_type) === "visit"){
                    console.log("COndition entered");
                    buttonVisible = true;
                }
            }
        }
    return buttonVisible;
    }
    
    function getFormatedName(query){
        if(query.getValue()!== null)return query.getValue()[0].name.toLowerCase();
        else return null
    }
    
    /*function refreshRibbon(executionContext){
    	var formContext = executionContext.getFormContext();
    	formContext.ui.refreshRibbon();
    	
    	
    }*/

    Awaiting your response.

    Best regards,
    EBMRay

  • Suggested answer
    Asmaa Bouhmidi Profile Picture
    141 on at
    RE: Hide/Show button based on two different lookup values

    hello the problem is in you code try the following

    function showHide(executionContext)
    {
    let formContext = executionContext;
    let status = formContext.getAttribute("msdyn_systemstatus").getValue();
    let buttonVisible = false;
    if(status === "690970001" || status === 690970001){
    let work_type = formContext.getAttribute("msdyn_workordertype");
    let incident_type = formContext.getAttribute("msdyn_primaryincidenttype");
    console.log(incident_type);
    let agreement = formContext.getAttribute("msdyn_agreement");
    let service =formContext.getAttribute("msdyn_serviceaccount").getValue();
    let flag = false;


    if(flag === false){
    if(getFormatedName(work_type) === "visit" && getFormatedName(incident_type) === "visit"){
    buttonVisible = true;
    }
    }
    }

    return buttonVisible;
    }

    it worked for me 

    ShowHide.gif

    Please mark my answer ad verified if it worked for you

  • EBMRay Profile Picture
    901 on at
    RE: Hide/Show button based on two different lookup values

    Hey Asmaa Bouhmidi ,

    Thank you for your reply.

    I already did that configuration as you mentioned below:

    Command-COnfiguration.pngInvertResult.png
    I was missing to assign false for the InvertResult. However, after I published the button stays hidden knowing that the record I am inspecting already satisfies the condition.

    It is really strange why it is not showing.

    Could you please try to replicate the same scenario?

    Awaiting your response.

    Best regards,
    EBMRay

  • Suggested answer
    Asmaa Bouhmidi Profile Picture
    141 on at
    RE: Hide/Show button based on two different lookup values

    an could you try to the invert result to false

    2022_2D00_04_2D00_19_5F00_22h38_5F00_04.png

  • Asmaa Bouhmidi Profile Picture
    141 on at
    RE: Hide/Show button based on two different lookup values

    Hello,

    can you make sure you have added the command to the button and the action and enable rules to the command

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

Ramesh Kumar – Community Spotlight

We are honored to recognize Ramesh Kumar as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Adis Profile Picture

Adis 136 Super User 2025 Season 1

#2
Sohail Ahmed Profile Picture

Sohail Ahmed 81

#3
Jonas "Jones" Melgaard Profile Picture

Jonas "Jones" Melgaard 77 Super User 2025 Season 1

Product updates

Dynamics 365 release plans