function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
lovetolearnlovetolearn 

Apexcommand button onclick event

Hi, 

 

I am trying to create an "onclick" event handler for the <apex:commandbutton> tag. For some reason, it is not working properly. Here is my code: 

 

<apex:commandbutton action="{!delete}" value="Delete" immediate="true" onclick="dislayErr()"/>

function displayErr(){
   if({!Account.name != 'Test User'}){
      alert("You are not allowed to delete this credit card entry");
   }
}
                

 Please help. Thank you. 

Best Answer chosen by Admin (Salesforce Developers) 
cloudmaniacloudmania

There are many workarounds to prevent the delete action.

But as a similar solution like urs:

function displayErr(acctName)

}

if(acctName!='Test User')

      {   

 alert(".....");

      }else

      {

          DeleteRecord();

      }

}

<apex:actionfunction name="DeleteRecord" action="{!Delete}"/>

<apex:commandButton value="Delete" onclick="DisplayErr('{!Account.Name}' "/>

Where Do you pass the Account Id through the URL in Visualforce,or apex side?

 

All Answers

Starz26Starz26

try

 

<apex:commandbutton action="{!delete}" value="Delete" immediate="true" onclick="dislayErr({!Account.Name})"/>

function displayErr(acctName){
   if(acctName != 'Test User'){
      alert('You are not allowed to delete this credit card entry');
   }
}
        
lovetolearnlovetolearn

Hi, 

 

This worked partially. Apologies, I didn't explain myself very well on the first post. The edits worked, the message displayed. However, when i tried to pass a record ID through the URL, the alert did not display. How do I get the alert to display when I pass a record ID through? 

 

In addition, how do I stop the delete function from executing if the alert displays?

 

Thanks for your help. 

asish1989asish1989

Hi Lovetolearn
You have to give Id of Test User In the Url.....
 try this by designing a controller method

public class x {

 

public Id accId{get;set;}

public Account account{get;set;}

    x() {

   accId = ApexPages.currentPage().getParameters.get('Id')

  account = [select Id,Name from Account where Id=:accId];

}

public Account getAccount() {

return account;

}

}

 

<apex:commandbutton action="{!delete}" value="Delete" immediate="true" onclick="dislayErr({!Account.Name})"/>

function displayErr(acctName){
   if(acctName != 'Test User'){
      alert('You are not allowed to delete this credit card entry');
   }
}
cloudmaniacloudmania

There are many workarounds to prevent the delete action.

But as a similar solution like urs:

function displayErr(acctName)

}

if(acctName!='Test User')

      {   

 alert(".....");

      }else

      {

          DeleteRecord();

      }

}

<apex:actionfunction name="DeleteRecord" action="{!Delete}"/>

<apex:commandButton value="Delete" onclick="DisplayErr('{!Account.Name}' "/>

Where Do you pass the Account Id through the URL in Visualforce,or apex side?

 

This was selected as the best answer
lovetolearnlovetolearn

Thanks everyone for all the help. I appreciate it.