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
Wes BarnesWes Barnes 

Update Custom Object with Opportunity and Account IDs within Controller...

All --

 

This is my first time using VF and creating a controller (shocker) and I have hit a roadblock with a project I am working on.  I have a page that is executed from the opportunity that pulls the current opportuninty and account ID to display some pertinent information from the account and opportunity as well as providing a list of available product inventory.  I have built the page and controller by reviewing the docs and a ton of forum post regarding wrapper classes and the use of checkboxes in a VF form page.  I have most of the controller built but I do not understand how to pull it all together and insert the account and opp IDs into my customer inventory object.  The goal is to loop through the list of invetory items that are selected and have the current Account and Opp IDs updated in the Inventory object.  Here is the controller:

 

 

public class InventoryController { Account account; Opportunity opportunity; Inventory__c inventory; public Account getAccount() { return [select id, name, fice_code__c, resellers_name__c, type from Account where id = :ApexPages.currentPage().getParameters().get('accid')]; } public Opportunity getOpportunity() { return [select id, name, amount, type, closedate from Opportunity where id = :ApexPages.currentPage().getParameters().get('oppid')]; } public List<cInventory> inventoryList {Get; Set;} public List<cInventory> getInventory(){ if (inventoryList == null){ inventoryList = new List<cInventory>(); for(Inventory__c c : [select Id, name, inventory_type__c, status__c, MAC_Address__c, Hard_Drive_Serial__c, FPIO_serial__c, ExIO_Card_Serial_optional__c, Appliance_Ateme_Serial__c, Build_date__c, account__r.id, account__r.name from Inventory__c where status__c = 'available' and (inventory_type__c = 'sales' or inventory_type__c = 'trial') ORDER BY Build_date__c ASC limit 1000]){ inventoryList.add(new cInventory(c)); } } return inventoryList; } public PageReference processSelected(){ List<Inventory__c> selectedInventory = new List<Inventory__c>(); for(cInventory cCon : getInventory()){ if(cCon.selected == true){ selectedInventory.add(cCon.con); } } PageReference oppPage = new PageReference ('/' + ApexPages.currentPage().getParameters().get('oppid')); oppPage.setRedirect(true); return oppPage; } public class cInventory{ public Inventory__c con {get; set;} public Boolean selected {get; set;} public cInventory(Inventory__c c){ con = c; selected = false; } } }

 

 and the VF page:

 

 

<apex:page Controller="InventoryController" tabStyle="Inventory__c"> <apex:form id="theForm"> <apex:pageBlock title="Account Information"> <apex:pageBlockSection Title="Account Section"> <apex:pageBlockTable value="{!account}" var="account"> <apex:column value="{!account.id}"/> <apex:column value="{!account.Name}"/> <apex:column value="{!account.FICE_Code__c}"/> <apex:column value="{!account.Resellers_Name__c}"/> <apex:column value="{!account.type}"/> </apex:pageBlockTable> </apex:pageBlockSection> <apex:pageBlockSection title="Opportunity Section"> <apex:pageBlockTable value="{!opportunity}" var="opp"> <apex:column value="{!opp.id}"/> <apex:column value="{!opp.Name}"/> <apex:column value="{!opp.type}"/> <apex:column value="{!opp.amount}"/> <apex:column value="{!opp.closedate}"/> </apex:pageBlockTable> </apex:pageBlockSection> </apex:pageBlock> <apex:pageBlock title="Inventory Information"> <apex:pageBlockButtons > <apex:commandButton value="Process Selected" action="{!processSelected}" rerender="table"/> </apex:pageBlockButtons> <apex:pageBlockSection title="Select Inventory"> <apex:dataTable value="{!inventory}" var="c" styleClass="list"> <apex:column > <apex:facet name="header">Select</apex:facet> <apex:inputCheckbox value="{!c.selected}" /> </apex:column> <apex:column > <apex:facet name="header">MAC Address</apex:facet> <apex:outputText value="{!c.con.MAC_Address__c}" /> </apex:column> <apex:column > <apex:facet name="header">Appliance Serial Number</apex:facet> <apex:outputText value="{!c.con.name}" /> </apex:column> <apex:column > <apex:facet name="header">Inventory Type</apex:facet> <apex:outputText value="{!c.con.Inventory_Type__c}" /> </apex:column> <apex:column > <apex:facet name="header">Inventory Status</apex:facet> <apex:outputText value="{!c.con.Status__c}" /> </apex:column> <apex:column > <apex:facet name="header">Build Date</apex:facet> <apex:outputText value="{!Month(c.con.Build_Date__c)}/{!Day(c.con.Build_Date__c)}/{!YEAR(c.con.Build_Date__c)}" /> </apex:column> <apex:column > <apex:facet name="header">Account Name</apex:facet> <apex:outputText value="{!c.con.account__r.name}" /> </apex:column> </apex:dataTable> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>

 

 I am looking for a way to execute the update on the Inventory__c custom object and appreciate any sample code or references that folks can provide to help with this.

 

Thanks in advance and again sorry for being a noob.

 

Wes

 

 

 

prabhutumprabhutum

Wes,

 

You may want to try the following in your code... 

 

 

// Update the Account and Opportunity ids on Inventory item before adding to the list

for(cInventory cCon : getInventory()){
            if(cCon.selected == true){

                cCon.AccountId = getAccount();

                cCon.OpportunityId = getOpportunity();
                selectedInventory.add(cCon.con);
               
            }
        }
// Update the Inventroy List...

Update selectedInventory[];

 

I haven't tested the code and apologize if this doesn't work as I am also new to Apex and VF. :o)

 

Goodluck!

Pr@sh...

 

Wes BarnesWes Barnes

Well that is a good start but I am not sure that will work.  I think that because of the relationship to my custom object that I have to use account__r and opportunity__r or something like that.  I am just not sure how to get the accid and oppid passed to my customer object using the update method.

 

Still working on it.

 

Wes 

prabhutumprabhutum

Wes,

 

I think you are almost there... I reread your issue and I think I have more understanding of what you are trying to achieve now. Could you list the definition of cInventory object here to fully understand what API names have been used for reference fields from Inventory, Opportunity, and Account objects?

 

I envision them to be something like

 

cInventory

------------

Id

Inventory__c -- reference field from Inventory object

Account__c -- reference field from Account object

Opportunity__c -- reference field from Opportunityobject

......

 

You can populate these using your getAccount() and getOpp() methods and then INSERT them in cInventory object. If this does not help then I am on wrong track and providing more information could help.

 

cheers,

Pr@sh...

 

 

Wes BarnesWes Barnes

Pr@sh --

 

Thanks for the response.  Well you are right on, those objects are absolutely correct.  Inventory__C is my custom object and Account__c and Opportunity__c are the reference fields in the Inventory__c object that I am trying to set with the update call.  Utlimately, I am trying to loop through a list and see what checkboxes have been selected and populate the Inventory__c object with the IDs from the account and the opportunity.  Below is a better code snippet of what I think I should be doing but I am stuck again.

 

 

public PageReference addInventory(){ List<Inventory__c> selectedInventory = new List<Inventory__c>(); // Update the Account and Opportunity ids on Inventory item before adding to the list for(cInventory cCon : getInventory()){ if(cCon.selected == true) { selectedInventory.add(cCon.con); cCon.con.account__c = getAccount(); cCon.con.opportunity__c = getOpportunity(); } } update inventory; PageReference oppPage = new PageReference ('/' + ApexPages.currentPage().getParameters().get('oppid')); oppPage.setRedirect(true); return oppPage; }

 

 

 

 

 Thanks in advance for your help again.

 

 

prabhutumprabhutum

Wes,

 

1. Do you have Inventory__c and cInventory two different objects? If only one Inventory__c then your FOR statement would fail as you are trying to define cCon as cInventory data type.

 

2. If Account and Opportunity fields are defined as Master-Detail on cInventory object you should use the assignment as

 cCon.account__c = getAccount(); //Removed ".con". Similarly for opportunity

 

3. I think you are trying to INSERT into cInventory object based on the selection of Inventory objects. If so,

INSERT cInventory; //Not inventory

 

4. You may define cInventory as an array and have one INSERT statement take care of all the inserts or have INSERT statement within FOR block right after assignments.

 

cheers,

Pr@sh...

 

Wes BarnesWes Barnes

Pr@sh --

 

I am sorry for not replying until now and did not want to leave you hanging.  I will be looking at this again tomorrow and will report back.  Thanks for your insight.  More to come.

 

Wes 

Wes BarnesWes Barnes

Pr@sh --

 

I wanted to follow up with you on this and am sorry about the delay in getting back to you.  I have made some progress and did a re-write on the original class and VF page.  I have attached the latest code which stem from the following posting:

 

http://wiki.developerforce.com/index.php/Checkbox_in_DataTable

 

My Controller 

 

public class InventoryController { public Account getAccount() { return [select id, name, fice_code__c, resellers_name__c, type from Account where id = :ApexPages.currentPage().getParameters().get('accid')]; } public Opportunity getOpportunity() { return [select id, name, amount, type, closedate from Opportunity where id = :ApexPages.currentPage().getParameters().get('oppid')]; } List<inventorywrapper> inventoryList = new List<inventorywrapper>(); List<Inventory__c> selectedInventory = new List<Inventory__c>(); public List<inventorywrapper> getInventory() { for(Inventory__c i : [select Id, name, inventory_type__c, status__c, MAC_Address__c, Hard_Drive_Serial__c, FPIO_serial__c, ExIO_Card_Serial_optional__c, Appliance_Ateme_Serial__c, Build_date__c, opportunity__c, opportunity__r.id, opportunity__r.name, account__c, account__r.name from Inventory__c where status__c = 'available' and (inventory_type__c = 'sales' or inventory_type__c = 'trial') ORDER BY Build_date__c ASC]) inventoryList.add(new inventorywrapper(i)); return inventoryList; } public PageReference getSelected() { selectedInventory.clear(); for(inventorywrapper invwrapper : inventoryList) if(invwrapper.selected == true) selectedInventory.add(invwrapper.inv); return null; } public List<Inventory__c> getSelectedInventory() { if(selectedInventory.size()>0) return selectedInventory; else return null; } public PageReference Save() { for(Inventory__c saveInventory : selectedInventory) { saveInventory.Account__r = getAccount(); saveInventory.Opportunity__r = getOpportunity(); } update selectedInventory; PageReference OppPage = new PageReference ('/' + ApexPages.currentPage().getParameters().get('oppid')); return OppPage; } public PageReference MailMerge() { PageReference MailMerge = new PageReference ('/mail/mmchoose.jsp?id=' + ApexPages.currentPage().getParameters().get('oppid') + '&1=' + '{!opp.Name}'); return MailMerge; } public class inventorywrapper { public Inventory__c inv {get; set;} public Boolean selected {get; set;} public inventorywrapper(Inventory__c i) { inv = i; selected = false; } } }

 

 VF Page:

 

 

<apex:page Controller="InventoryController" tabStyle="Inventory__c"> <apex:form id="theForm"> <apex:pageBlock title="Account Information"> <apex:pageBlockSection Title="Account Section"> <apex:pageBlockTable value="{!account}" var="account"> <apex:column value="{!account.id}"/> <apex:column value="{!account.Name}"/> <apex:column value="{!account.FICE_Code__c}"/> <apex:column value="{!account.Resellers_Name__c}"/> <apex:column value="{!account.type}"/> </apex:pageBlockTable> </apex:pageBlockSection> <apex:pageBlockSection title="Opportunity Section"> <apex:pageBlockTable value="{!opportunity}" var="opp"> <apex:column value="{!opp.id}"/> <apex:column value="{!opp.Name}"/> <apex:column value="{!opp.type}"/> <apex:column value="{!opp.amount}"/> <apex:column value="{!opp.closedate}"/> </apex:pageBlockTable> </apex:pageBlockSection> </apex:pageBlock> <apex:pageBlock title="Inventory Information"> <apex:pageBlockButtons > <apex:commandButton value="Save" action="{!Save}" rerender="table"/> <apex:commandButton value="Create Sales Documents" action="{!MailMerge}" rerender="table"/> </apex:pageBlockButtons> <apex:pageBlockSection title="Select Inventory"> <apex:dataTable value="{!inventory}" var="i" styleClass="list"> <apex:column > <apex:facet name="header">Select</apex:facet> <apex:inputCheckbox value="{!i.selected}" /> <apex:actionSupport event="onclick" action="{!getSelected}" rerender="Selected_Inv"/> </apex:column> <apex:column > <apex:facet name="header">MAC Address</apex:facet> <apex:outputText value="{!i.inv.MAC_Address__c}" /> </apex:column> <apex:column > <apex:facet name="header">Appliance Serial Number</apex:facet> <apex:outputText value="{!i.inv.name}" /> </apex:column> <apex:column > <apex:facet name="header">Inventory Type</apex:facet> <apex:outputText value="{!i.inv.Inventory_Type__c}" /> </apex:column> <apex:column > <apex:facet name="header">Inventory Status</apex:facet> <apex:outputText value="{!i.inv.Status__c}" /> </apex:column> <apex:column > <apex:facet name="header">Build Date</apex:facet> <apex:outputText value="{!Month(i.inv.Build_Date__c)}/{!Day(i.inv.Build_Date__c)}/{!YEAR(i.inv.Build_Date__c)}" /> </apex:column> <apex:column > <apex:facet name="header">Account Name</apex:facet> <apex:outputText value="{!i.inv.account__r.name}" /> </apex:column> </apex:dataTable> </apex:pageBlockSection> <apex:pageBlockSection Title="Selected Inventory" id="Selected_Inv"> <apex:dataTable value="{!SelectedInventory}" var="s" columnswidth="50px,50px" cellpadding="4" border="1"> <apex:column > <apex:facet name="header">MAC Address</apex:facet> <apex:outputText value="{!s.MAC_Address__c}" /> </apex:column> <apex:column > <apex:facet name="header">Appliance Serial Number</apex:facet> <apex:outputText value="{!s.name}" /> </apex:column> <apex:column > <apex:facet name="header">Inventory Type</apex:facet> <apex:outputText value="{!s.Inventory_Type__c}" /> </apex:column> <apex:column > <apex:facet name="header">Inventory Status</apex:facet> <apex:outputText value="{!s.Status__c}" /> </apex:column> <apex:column > <apex:facet name="header">Build Date</apex:facet> <apex:outputText value="{!Month(s.Build_Date__c)}/{!Day(s.Build_Date__c)}/{!YEAR(s.Build_Date__c)}" /> </apex:column> <apex:column > <apex:facet name="header">Account Name</apex:facet> <apex:outputText value="{!s.account__r.name}" /> </apex:column> <apex:column > <apex:facet name="header">account ID</apex:facet> <apex:outputText value="{!s.account__r.id}" /> </apex:column> <apex:column > <apex:facet name="header">Opportunity ID</apex:facet> <apex:outputText value="{!s.opportunity__r.ID}" /> </apex:column> <apex:column > <apex:facet name="header">Opportunity Name</apex:facet> <apex:outputText value="{!s.opportunity__r.name}" /> </apex:column> </apex:dataTable> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>

 

Where I am having the problem is on the update.  When I execute the Save command, the account ID and opportunity IDs are not committed to the database.  I know that when I check the checkbox that the proper ID is being placed in the VF page as I can refresh it and see the values populating the page.  Am I missing a setter call or something when the update is executed.  I am pretty sure that this should be an update and not an insert as I already have the records created and need to update with the new account ID and opportunity ids.

 

I am sorry to have shifted gears on this but I like this implementation better.

 

Thanks in advance for your help.

 

King Regards,


Wes

 

 

DpalDpal
Did you solve this?  I am having the same problem using very similiar code.  When I try to update the relationship IDs are not committed.  No errors, just nothing saved.
Wes BarnesWes Barnes

Dpal --

 

Sorry no updates and I have moved on with another project.  Unfortunately, this took a backseat to other priorities.   I am pretty sure that I was missing a setter method in there but I was not sure how to get the setter to take.  

 

Sorry I could not of more assistance.

 

Wes