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
SV MSV M 

Adding Button to Visualforce Page

Hi, I would like add buttons at each row of visuallforce page and when I click the button it should give an alert which should get the Name of that Account since here I am using my standardCOntroller as Account object.

I've below code
<apex:page standardController="Account" recordSetVar="Accounts" sidebar="False">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection title="Details">
            <apex:pageBlockTable value="{!Accounts}" var="a">
                <apex:column value="{!a.name}"/>
                <apex:column value="{!a.Type}"/>
                <apex:column value="{!a.Website}"/>
                <apex:column headerValue="Details">
                    <apex:commandButton value="Details"/></apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

It gave me buttons at each row.
User-added image
Please tell me how to create custom action for the button to get the Name of the account as a alert..
Rushikesh KhandaleRushikesh Khandale
Hello,
You can use OnClick event of the command button for this purpose. You'll have to call a javascript function on onClick event. Please refer the following code.
<apex:page standardController="Account" recordSetVar="Accounts" sidebar="False">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection title="Details">
            <apex:pageBlockTable value="{!Accounts}" var="a">
                <apex:column value="{!a.name}"/>
                <apex:column value="{!a.Type}"/>
                <apex:column value="{!a.Website}"/>
                <apex:column headerValue="Details">
                    <apex:commandButton value="Details" onclick="showAlert('{!a.Name}')"/></apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
    <script>
		function showAlert(accountName){
        	alert('HELLO ' + accountName);
    	}    
    </script>
</apex:page>
Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards