Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

127
Visualizações
EnableRule in ribbon cannot get boolean value from async function

I want to return true or false from the function showHideAddNewButton. I have an EnableRule in a ribbon button, which calls a custom rule that calls this function showHideAddNewButton. On passing either true, which will show the button, or false, which will hide the button.

I have to access statuscode (Status Reason) and statecode (Status) fields on the entity. I have created a query using the Xrm.WebApi.retrieveMultipleRecords, but cannot get it to return a flag. I want the retrieveMultipleRecords method to only execute on and never be called again but 'return true' below gets executed setting my button to true always.

function showHideAddNewCsrsRecalculation(primaryControl){
    var fileNumber = primaryControl.getAttribute("ssg_filenumber").getValue();
    
        Xrm.WebApi.retrieveMultipleRecords("rrg_csrsfile", "?$select=statuscode,statecode,rr_filenumber&$filter=rr_filenumber eq '" + fileNumber + "'").then(
            function success(result) {
                for (var i = 0; i < result.entities.length; i++) {
                    var statusCode = result.entities[i].statecode; 
                    var statusReasonCode = result.entities[i].statuscode; 
                    
                    //if draft make button invisible
                    if (statusReasonCode == 8676725)
                       return false;
                    //if submitted make button invisible
                     if (statusReasonCode == 8676726)
                       return false; 
    
                    //if inactive make button invisible
                     if (statusCode == 1)
                       return false;
                }           
            },
            function (error) {
                console.log(error.message);
                // handle error conditions
            }
        );
        
    //if draft make button invisible
    //if (primaryControl.getAttribute("statuscode").getValue() == 867670025)
    //   return false; 
    //if submitted make button invisible
    //if (primaryControl.getAttribute("statuscode").getValue() == 867670026)
    //  return false; 
    
    //if inactive make button invisible
    //if (primaryControl.getAttribute("statecode") != 'undefined' && primaryControl.getAttribute("statecode").getValue() == 1)
    //  return false;
    
    //other options make button visible
      return true; --> This keeps getting called as a result my button is always visible
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Function Xrm.WebApi.retrieveMultipleRecords returns a promise, not an actual boolean value. The function is executed asynchronously, so immediately after the call to this function the next line is executed and that line always returns true.

In fact it is not possible to make an asynchronous call synchronous. Instead we can follow another approach by following these steps:

  1. Do the query in the form's onload function and store the result in a variable.
  2. Refresh the ribbon.
  3. Create a ribbon button handler returning the variable's value.
let isRecalculationButtonVisible = false;

function onLoad(context) {
    const formContext = context.getFormContext();

    const filter = "$filter=rr_filenumber eq '"
        + formContext.getAttribute("ssg_filenumber").getValue()
        + "' and (statecode eq 1 or statuscode eq 8676725 or statuscode eq 8676726)";

    Xrm.WebApi.retrieveMultipleRecords("rrg_csrsfile", "?$select=rrg_csrsfileid&$top=1&" + filter)
        .then(function (result) {
            isRecalculationButtonVisible = result.entities.length === 0;
        })
        .catch(function (error) {
            console.log(error.message);
        })
        .finally(() => {
            formContext.ui.refreshRibbon(false);
        });
}

function showHideAddNewCsrsRecalculation() {
    return isRecalculationButtonVisible;
}

As you probably already noticed I added a few improvements.

  1. All your button needs to know is if there are any records meeting specific conditions, so there is no need to actually retrieve them. Therefore these conditions can simply be placed in the query's filter. I also added a $top=1, because the number of records meeting the conditions is not relevant here. As a consequence the only check that needs to be done is whether a record is returned or not.
  2. As explained, retrieveMultipleRecords returns a promise. The recommended error handling for promises is adding a catch function at the end of the chain.
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda