web
You’re offline. This is a read only version of the page.
close
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 905

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

I have the same question (0)
  • Asmaa Bouhmidi Profile Picture
    173 on at

    Hello,

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

  • Suggested answer
    Asmaa Bouhmidi Profile Picture
    173 on at

    an could you try to the invert result to false

    2022_2D00_04_2D00_19_5F00_22h38_5F00_04.png

  • EBMRay Profile Picture
    905 on at

    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
    173 on at

    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
    905 on at

    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
    173 on at

    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
    905 on at

    Asmaa Bouhmidi

    Thank you so much, it worked perfectly!

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 45 Most Valuable Professional

#2
iampranjal Profile Picture

iampranjal 36

#3
Satyam Prakash Profile Picture

Satyam Prakash 31

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans