Hide/Show Button based on the Dataset Field within a Collection (Dynamic Page)

Hello,

I am getting an error message with custom css on button.

I have created two buttons on a page using custom code to hide one button if the dataset field in my collection is set to “SOLD” and to show the other button if the dataset field in the same collection is set to “BUY” or “RENT”. Both buttons are linked to a pop-up form.

It doesn’t work. This is the error message:

UserError: datasetApi ‘getCurrentItem’ operation failed
Caused by DatasetError: Operation (getCurrentItem) is not allowed because the field used to build this page’s URL is empty

Here is the code:

$w.onReady(function () {

// Wait for the dataset to be ready

$w(‘#dataset1’).onReady(() => {

// Get the current item from the dataset

const item = $w(‘#dataset1’).getCurrentItem();

// Check if the Availability field is “SOLD”

if (item && item.Availability && item.Availability.toUpperCase() === “SOLD”) {

$w(‘#button39’).hide();

} else {

$w(‘#button39’).show();

}

// Check if the Availability field is “BUY” or “RENT”

if (item && item.Availability && (item.Availability.toUpperCase() === “BUY” || item.Availability.toUpperCase() === “RENT”)) {

$w(‘#button86’).hide();

} else {

$w(‘#button86’).show();

}

});

});

UserError: datasetApi ‘getCurrentItem’ operation failed
often happens on dynamic pages viewed in Preview mode but has no effect on the live site. If you’re getting this error on the live site, check to make sure that it is actually a dynamic page.

Try this:

$w.onReady(function () {
  $w('#dataset1').onReady(() => {
    let item = $w('#dataset1').getCurrentItem();
    if (item && item.Availability) {
      const status = item.Availability.toUpperCase();

      if (status === "SOLD") {
        $w('#button39').hide();
      } else {
        $w('#button39').show();
      }

      if (status === "BUY" || status === "RENT") {
        $w('#button86').hide(); 
      } else {
        $w('#button86').show();
      }
    }
  });
});

Hello,

Thanks for your reply. I published the site, and it didn’t work.

I deleted my code and pasted your code, which is much simpler. However, that too didn’t work.

The page is definitely a Dynamic Page from a collection with all of our listings.

Do you know what could be the issue to fix it?

I appreciate your help.

Mary

Dynamic pages are built from a URL field (slug) in your collection. I would check to make sure this field has values for all items. If one is blank you will get that error.

you should confirm that the status (item.Availability) is actually being passed in the code by adding the lineconsole.log("Availability is: ", status") after status is declared. If you’re not seeing BUY or SOLD in the logs, check the correct spelling of the field key - is it Availability or availability? If status is correct then check the logic to make sure the correct button is hiding/showing.

THANKS ALL. Resolved and working :slight_smile:

There was a typo in the dataset.

Thanks for your help.