+ Start a Discussion
acrozieracrozier 

Creating a custom button for a related list

Hello,

 

I have a two custom objects with a master detail relationship with Job__c being the master and AIIncentive__c the detail.  The AIIncentive__c object is displayed as a related list on my Job__c object. I created a custom edit page to be used off of the job page so that when a user goes to add a new line or edit other lines they will see a list of AIIncentive__c  data related to the Job__c object they are on.  

 

The problem that I am having is that I need the buttons that appear at the top of the related list to redirect the user to this custom edit page instead of the default New AIIncentive page.  I set the edit buttons to redirect to this page and that works, but when I set the New button to go to this page I get the following error.

 

Visualforce Error


System.QueryException: List has no rows for assignment to SObject
Class.Incent.: line 11, column 1

 

I can't seem to add a custom button to the top of the related list either, it will only let me add the New button, which will not redirect correctly.

 

VF page

<apex:page standardController="AIIncentive__c" extensions="Incent">
<apex:form >
<apex:pageBlock title="Edit Incentives">
		<apex:pageBlockButtons >   
         <apex:commandButton value="Save" action="{!save}" reRender="incentpanel"/> 
         <apex:commandButton value="New" action="{!NewIncentive}" reRender="incentpanel"/>
		</apex:pageBlockButtons>
             
      
     	<apex:pageblockSection >
         <apex:dataTable value="{!job}" var="j" cellspacing="5">
             <apex:column >
                 <strong>Account:</strong> &nbsp; <apex:outputField value="{!j.Account__c}"/>
             </apex:column><br />
             <apex:column >
                 <strong>Job Number:</strong> &nbsp; <apex:outputField value="{!AIIncentive__c.Job__c}"/>
             </apex:column>
         </apex:datatable><br />
        </apex:pageblockSection>
     
 
   
       
     <apex:pageblockTable value="{!Incents}" var="incent" id="incentpanel">
             <apex:column headerValue="QTY">
                 <apex:inputField value="{!incent.QTY__c}"/>
             </apex:column>
             <apex:column headerValue="Description">
                 <apex:inputField value="{!incent.Description__c}"/>
             </apex:column>
             <apex:column headerValue="Honoraria__c">
                 <apex:inputField value="{!incent.Honoraria__c}"/>
             </apex:column>
             <apex:column headerValue="Deleted">
                 <apex:inputField Value="{!incent.Deleted__c}"/>
             </apex:column>
         </apex:pageblockTable>

         
     
       
       
    </apex:pageBlock>

</apex:form> 
</apex:page>

 Extension

public with sharing class Incent {
public list<AIIncentive__c> Incents {get; set;}
public list<job__c> job {get; set;}
public id jobid;
public string NewIncentive {get; set;}
public PageReference refresh = ApexPages.currentPage();


    public Incent(ApexPages.StandardController controller) {
     id incentId= apexpages.currentpage().getParameters().get('id');
        jobid=[select id, Job__c from AIIncentive__c where id=:incentId].Job__c;

        Incents = [Select QTY__c, Description__c,  Honoraria__c, Job__c, Deleted__c  From AIIncentive__c  where Job__c = :jobid]; 
        job = [SELECT Account__c FROM Job__c WHERE id= :jobid];    
        }


    
    public PageReference NewIncentive(){
    	
    	    	    	
    	AIIncentive__c obj = new AIIncentive__c();
          	obj.QTY__c = 0;
     		obj.Description__c = '';
     		obj.Honoraria__c = 0;
     		obj.Job__c = jobid;

	 update Incents;
	 		 
     insert obj;
     
        refresh.setRedirect(true);
     
   	 return refresh;
     
    }
    
        public PageReference save() {
        	        	
        update Incents;
        
        	refresh.setRedirect(true);
        	        
        return refresh;
    }
    
    
 //   public PageReference Button(){

 //    NewIncentive();
    
   



//      return true;
     
 //    }
    }