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
Natalie Jane HodsonNatalie Jane Hodson 

Pull Current Record ID from Opportunity to pre-populate Mass Custom Object Record Creation

Hi,

I have a list button on a related list called Resource Allocation against my Opportunity object. I have created a mass create record visualforce page that is related to the button. I am pulling my Master Detail Relationship onto my Table, and want to pre-populate it with the Current Record ID of the Opportunity record I selected my button from.

Piecing together elements I found online I was able to create an extension to get the Opp ID, however when I use the button I recieve the following error.
Invalid conversion from runtime type Resource_Allocation__c to Opportunity 
Not sure how I recieve this as there was no error when saving the apex class or visualforce page. I may have just written jibberish as I am new to Apex.

Any Help would be appreciated on this!

<<<<<<<<Related apex below>>>>>>>

Visualforce Page
<apex:page standardcontroller="Resource_Allocation__c" recordSetVar="Resource Allocations" tabstyle="Resource_Allocation__c" extensions="ManageListController,OppExtension" >
<apex:form >
   <apex:pageBlock title="Bulk Resource Allocation Create">
      <apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable">
         <apex:column headerValue="Ident">
            <apex:outputText value="{!wrapper.ident}"/>
         </apex:column>
        <apex:column headervalue="Opportunity">
        <apex:inputField id="OppsID" value="{!wrapper.RA.Opportunity__c}"/>
        </apex:column>
        <apex:column headerValue="Resource Name or Role">
         <apex:inputField value="{!wrapper.RA.Resource_Name_or_Role__c}"/>
                 </apex:column>
        <apex:column headerValue="Start">
            <apex:inputField value="{!wrapper.RA.Start_Date__c}"/>
        </apex:column>
         <apex:column headerValue="End">
            <apex:inputField value="{!wrapper.RA.End_Date__c}"/>
         </apex:column>
         <apex:column headerValue="Est Pay">
            <apex:inputField value="{!wrapper.RA.Resource_Est_Pay__c}"/>
         </apex:column>
         <apex:column headerValue="Est Charge">
            <apex:inputField value="{!wrapper.RA.Resource_Est_Charge__c}"/>
         </apex:column>
        <apex:column headerValue="Max Days Contracted">
        <apex:inputfield value="{!wrapper.RA.Max_Days_Contracted__c}"/>
               </apex:column>
         <apex:column headervalue="Daily Expense Allowance">
               <apex:inputField value=" {!wrapper.RA.Daily_Expense_Allowance__c}"/>
               </apex:column>
         <apex:column headerValue="Action">
            <apex:commandButton value="Delete" action="{!delWrapper}" rerender="wtable">
               <apex:param name="toDelIdent" value="{!wrapper.ident}" assignTo="{!toDelIdent}"/>
            </apex:commandButton>
         </apex:column>
      </apex:pageBlockTable>
      <apex:commandButton value="Add Row" action="{!addRows}" rerender="wtable">
         <apex:param name="addCount" value="1" assignTo="{!addCount}"/>
      </apex:commandButton>
      <apex:commandButton value="Add 5 Rows" action="{!addRows}" rerender="wtable">
         <apex:param name="addCount" value="5" assignTo="{!addCount}"/>
      </apex:commandButton>
      <apex:commandButton value="Save" action="{!save}"/>
   </apex:pageBlock>
 </apex:form>
</apex:page>

Extension 1
public class ManageListController 
{
   private final Resource_Allocation__c RA;
     // The extension constructor initializes the private member
        // variable RA by using the getRecord method from the standard
        // controller.
        public ManageListController(ApexPages.StandardSetController stdController) {
            this.RA = (Resource_Allocation__c)stdController.getRecord();
            wrappers=new List<ResourceAllocationWrapper>();
        }

 public List<ResourceAllocationWrapper> wrappers {get; set;}
 public static Integer toDelIdent {get; set;}
 public static Integer addCount {get; set;}
 private Integer nextIdent=0;
  
 public ManageListController()
 {
  wrappers=new List<ResourceAllocationWrapper>();
  for (Integer idx=0; idx<1; idx++)
  {
   wrappers.add(new ResourceAllocationWrapper(nextIdent++));
  }
 }
  
 public void delWrapper()
 {
  Integer toDelPos=-1;
  for (Integer idx=0; idx<wrappers.size(); idx++)
  {
   if (wrappers[idx].ident==toDelIdent)
   {
    toDelPos=idx;
   }
  }
   
  if (-1!=toDelPos)
  {
   wrappers.remove(toDelPos);
  }
 }
  
 public void addRows()
 {
  for (Integer idx=0; idx<addCount; idx++)
  {
   wrappers.add(new ResourceAllocationWrapper(nextIdent++));
  }
 }
  
 public PageReference save()
 {
  List<Resource_Allocation__c> RA=new List<Resource_Allocation__c>();
  for (ResourceAllocationWrapper wrap : wrappers)
  {
   RA.add(wrap.RA);
  }
   
  insert RA;
   
  return new PageReference('/' + Schema.getGlobalDescribe().get('Opportunity').getDescribe().getKeyPrefix() + '/o');
 }
  
 public class ResourceAllocationWrapper
 {
  public Resource_Allocation__c RA {get; private set;}
  public Integer ident {get; private set;}
   
  public ResourceAllocationWrapper(Integer inIdent)
  {
   ident=inIdent;
   RA=new Resource_Allocation__c(Name='RA' + ident);
  }
 }
}

 
Extension2
public class OppExtension {
 private final Opportunity Opps;
     // The extension constructor initializes the private member
        // variable Opps by using the getRecord method from the standard
        // controller.
        public OppExtension(ApexPages.StandardSetController stdController) {
            this.Opps = (Opportunity)stdController.getRecord();
                    }

     
        public Opportunity currentRecord{get; set;}
         
        public OppExtension(ApexPages.StandardController controller) {
            currentRecord = [SELECT Id, Name, Amount FROM Opportunity WHERE Id =:ApexPages.currentPage().getParameters().get('OppsId')];
        }
     
    }
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Natalie

You are using the concept of standard controller but in your Extension 2 you are trying to convert Resource Allocation Object instance to Opportunity instance which is not possible.
These errors dont arise while compiling because the class doesn't know for which VF page would it act as a standard controller.

Cheers!!!