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
Amit Singh.ax1042Amit Singh.ax1042 

How to use Command Button in Page Block Table???

Hi Friends,
I have created an object Purchase Order...
in Purchase Order Following are the Fields...
Name
Invoice Created (Picklist-: Yes,No,Partally)
Status (Picklist-: Sent,Open)
Amount (Currency)


Another Object Is-:
Invoice
Fields of Invoice are

Purchase Order (look up- PO is master)
Date (Date)
Amount (Currency)


i have override the Purchase order tab using visualforce page...
(i am using Page Block table in my Vf Page)

I Donot want to create Invoice From Invoice Tab...
i want to use command button in my Purchase Order Page Block Table...
so I will be able to create Invoice for Each PO...

Suppose i am creating any Invoice By click Create Button Next to any PO record...
i should get navigate to create Invoice Page (Where Purchase Order will be automatically filled)
( i am able to do this)

but I want that, Suppose Purchase Order Record (For which I want to Create Invoice),if its status field is Open then if I click on create Invoice button next to Purchase Oreder then I should Not allow to land to CreateNewInvoice Visualforce page... I should get message like "You can not create Invoice For this Purchase Order because its Status is Open"...

and if for any Purchase Oreder its status is Sent then after clicking create Invoice Button next to PO record I should land to Create New Invoice Visualforce...


How It can be Possible???

<apex:page controller="DisplayPOunderjobtabextension">

  <apex:form >
   <apex:pageBlock >
     
     
    
    
     <apex:commandButton action="{!CreatePO}" value="New Purchase Order" id="btn1"/> 
    
    <br/>
    <apex:pageBlockTable value="{!purchaseorder}" var="po">
    
    <apex:column headerValue="Action">

         
          <apex:commandButton value="Create Invoice" action="/apex/CreateInvoiceFromPO?id={!po.id}"/>
         </apex:column> 
    
    
      <apex:column headerValue="Name">
       <apex:outputLink tabindex="tab1" value="/apex/PODetailpage?id={!po.Id}">{!po.Name}</apex:outputLink>
      </apex:column>
      
      <apex:column headerValue="Invoice Created">
      <apex:outputField value="{!po.Invoice_Created__c}"/> 
      </apex:column>
            
       <apex:column headerValue="Status">
       <apex:outputField value="{!po.Status__c}"/>
      </apex:column>
      
      <apex:column headerValue="Total Amount">
       <apex:outputField value="{!po.Amount__c}"/>
      </apex:column>
      
    
    </apex:pageBlockTable>
    

   
  
    <apex:pageBlockSection rendered="{!purchaseorder.Size == 0}">
     <apex:pageBlockSectionItem >
      No records to display.
     
     </apex:pageBlockSectionItem>
    </apex:pageBlockSection>
    
   </apex:pageBlock>
   
  </apex:form>
</apex:page>




Apex Class


public class DisplayPOunderjobtabextension
{
public List<Purchase_Order__c> purchaseorder{get;set;} 
//public List<Purchase_Order__c> purchaseorder1{get;set;} 
public DisplayPOunderjobtabextension()
{
String theID = ApexPages.CurrentPage().getParameters().get('id');
purchaseorder = [Select id,Name,Due_Date__c,Issue_Date__c,job__c,Status__c,Total_Amount__c,Vendor__c,Approved_By_Staff__c,Invoice_Created__c from Purchase_Order__c where Job__c =: theID Order BY Name];
}

 public PageReference CreatePO()
    {
       String jobid;
       String theId=ApexPages.CurrentPage().getParameters().get('id');
       Job__c job = [Select Id from Job__c where id =: theID];
       jobid=job.Id;
       
      PageReference p = Page.CreateNewPurchaseOrder;
       p.setredirect(true);
     p.getParameters().put('jobid',theID); 
     return p;
       return null;
    }


}

 

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

This can be done using rendered Attribute, like show different Command buttons one with action as redirect and other just to alert that invoice exists.

eg:

<apex:column headerValue="Action">
    <apex:commandButton value="Create Invoice" action="/apex/CreateInvoiceFromPO?id={!po.id}" rendered="{!po.Status__c = 'Sent'}"/>
    <apex:commandButton value="Invoice Exists" onclick="alert('You can not create Invoice For this Purchase Order because its Status is Open');" rendered="{!po.Status__c = 'Open'}"/>
</apex:column>

hope it helps.

All Answers

Rehan DawtRehan Dawt

Hi Amit,

 

You can use

 

<apex:column headerValue="Column Name You want to Display">
     <apex:commandButton value="Button Name" action="{!FunctionName}" /> 
</apex:column>

 

Thanks.

Rehan Dawt

Amit Singh.ax1042Amit Singh.ax1042

But Rehan, I want that this action should be called on certain condition...

again go through my post!!! 

 

 

Regards,

Amit

Rahul SharmaRahul Sharma

This can be done using rendered Attribute, like show different Command buttons one with action as redirect and other just to alert that invoice exists.

eg:

<apex:column headerValue="Action">
    <apex:commandButton value="Create Invoice" action="/apex/CreateInvoiceFromPO?id={!po.id}" rendered="{!po.Status__c = 'Sent'}"/>
    <apex:commandButton value="Invoice Exists" onclick="alert('You can not create Invoice For this Purchase Order because its Status is Open');" rendered="{!po.Status__c = 'Open'}"/>
</apex:column>

hope it helps.

This was selected as the best answer
Amit Singh.ax1042Amit Singh.ax1042

Thanks Rahul,it works...