0

I have two custom objects, Package__c and Product__c, I'm trying to create a form that will take input for fields of both objects on a single visualforce page and Save the data accordingly. Here is my Visualforce page -

    <apex:page controller="RedirectedController">
    <apex:form > 

    <apex:pageBlock >
        
        <apex:pageBlockSection columns="2">
                
            <apex:inputField label="Name" value="{!objectpkg.Name}"/>
            <apex:inputField label="Account" value="{!objectpkg.Account__c}"/>
            <apex:inputField label="Type" value="{!objectpkg.Package_Type__c}"/>
            <apex:inputField label="Cost Of Goods" value="{!objectpkg.Cost_of_Goods_Sold__c}"/>
            <apex:inputField label="Quantity Of Goods" value="{!objectpkg.Quantity_of_Goods_Sold__c}"/>
            <apex:inputField label="Ready to Ship" value="{!objectpkg.Ready_to_Ship__c}"/>
                
        </apex:pageBlockSection>
        
        <apex:pageBlockButtons >
            <apex:commandButton action="{!saveAll}" value="Save"/>
            <apex:commandButton value="Cancel"/>
        </apex:pageBlockButtons>

    </apex:pageBlock>

   <apex:inputField label="Family" value="{!objectprod.Product_Family__c}"/>
</apex:form> 
</apex:page>

Apex Code

public with sharing class RedirectedController 
{
    public List<Package__c> objectpkg {get; set;}
    public List<Product__c> objectprod {get; set;}
    public Package__c pkg {get; set;}
    public Product__c prod {get; set;}
    public RedirectedController() 
    {
        List<Package__c> objectpkg = new List<Package__c>();
        List<Product__c> objectprod = new List<Product__c>();
        objectpkg = [SELECT Account__c, Cost_of_Goods_Sold__c, Name, Package_Type__c, Quantity_of_Goods_Sold__c, Ready_to_Ship__c FROM Package__c];
        objectprod =[SELECT Product_Family__c From Product__c];

    }

    public void saveAll()
    {
        insert objectpkg;
        insert objectprod;
    }

}

I get the error as "Could not resolve the entity from apex:inputField value binding '{!objectpkg.Name}'. apex:inputField can only be used with SObjects, or objects that are Visualforce field component resolvable."

What am I doing wrong?

0

Presumably, you are expecting only a single Product__c and Package__c for the VF form

so, use singleton properties in the controller and resolve the queries to a single record

public with sharing class RedirectedController 
{
    public Package__c objectpkg {get; set;}
    public Product__c objectprod {get; set;}

   public Package__c pkg {get; set;}
   public Product__c prod {get; set;} 

    public RedirectedController() 
    {

        objectpkg = [SELECT Account__c, Cost_of_Goods_Sold__c, Name, Package_Type__c, Quantity_of_Goods_Sold__c, Ready_to_Ship__c 
                      FROM Package__c][0];
        objectprod =[SELECT Product_Family__c From Product__c][0];

    }

    public void saveAll()
    {
        insert objectpkg;
        insert objectprod;
    }

}

It is not clear from your VF form where the values of variables Product__c and Package__c come from -- these are sobjects and have to be queried for from some ID field made available to the controller in a URL parameter

1

objectprod is a List<Product__c> so you cannot get the field from a list.

You need to iterate and then use it.

4
  • Could you post the Code ? I'm new to Salesforce so it would be helpful – An Amateur Dec 4 '20 at 14:32
  • You can google it, tons of example out there. Google it and you will learn faster. For eg: developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/… – Nagendra Singh Dec 4 '20 at 14:33
  • I tried <apex:repeat value="{!objectpkg}" var="o"> <apex:inputField label="Name" value="{!o.Name}"/> <apex:inputField label="Account" value="{!o.Account__c}"/> </apex:repeat> and so on, now I'm not getting any errors but the inputfields are not showing up on the page – An Amateur Dec 4 '20 at 14:57
  • Account__c is a Id so it will act as a lookup. but it will show in the UI. There might be some error in your code. – Nagendra Singh Dec 4 '20 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.