You need to sign in to do that
Don't have an account?
Junction Object Class
Hey All,
I am having trouble with the below class and am hoping to get some help. I have a Junction Object that is connecting two custom Objects, Order__c and Product_Bundle__c and I am creating a Visualforce Page that will allow the User to create the Junction Objects by adding Quantity to the records and saving the page. The class is designed to:
1. Query the Product_Bundle__c Custom Object for available Product Bundles and assocate them behind the scenes with the current Order__c
2. Display them on a VF page with the inputField Quantity__c exposed
3. If the User enters a quantity and clicks "Save", the system will create those Junction Objects associating the Product Bundles to the Order
I cannot seem to get my PageBlockTable to use the list in my Controller:
Error: Unknown property 'Order_Item_Junction__cStandardController.lstoitem' |
Any help on the below code would be great!
Class
public class oitemController { public List<Product_Bundle__c> lstbundle = new List<Product_Bundle__c>(); public List<Order_Item_Junction__c> lstoitem = new List<Order_Item_Junction__c>(); public string orderid; public boolean proceed {get;set;} public boolean setproceed() { return true; } public oitemController(ApexPages.StandardController controller) { string flag = ''; //Order ID orderid = ApexPages.currentPage().getparameters().get('oid'); //Record Type string orecordtype = ApexPages.currentPage().getparameters().get('rectype'); //Use this variable to test the code coverage of try-catch block within constructor and set to 'true' String calledFromTestClass = ApexPages.currentPage().getparameters().get('calledFromTestClass'); if(orecordtype == 'New Order') { flag = 'new'; //Fetch all Product Bundles where Order_Availability__c = New Order lstbundle = [Select Order_Availability__c, Name, Id From Product_Bundle__c where Order_Availability__c =:'New Order' and Completed__c =: TRUE order by Name]; } else if(orecordtype == 'Return Order') { flag = 'return'; //Fetch all Product Bundles where Order_Availability__c = Return Oder lstbundle = [Select Order_Availability__c, Name, Id From Product_Bundle__c where Order_Availability__c =:'Return Order' and Completed__c =: TRUE order by Name]; } List<Order_Item_Junction__c> lstoitem = new List<Order_Item_Junction__c>(); if(calledFromTestClass == 'true') { lstoitem = null; } try{ for(integer i=0;i<lstbundle.size();i++) { Order_Item_Junction__c oitem = new Order_Item_Junction__c(); oitem.Product_Bundle__c = lstbundle[i].Id; oitem.Order__c = orderid; lstoitem.add(oitem); } }catch(Exception ex){ // Adding exeption message onto the page ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Error:'+ ex.getMessage())); System.debug(ex.getMessage()); } } }
Page
<apex:page id="orderpage1" standardController="Order_Item_Junction__c" extensions="oitemController" sidebar="false" showheader="false" > <script type="text/javascript"> function refreshparent() { window.opener.location.reload(); window.close(); } </script> <apex:form id="form1"> <apex:pageBlock title="Add Order Items" id="order"> <apex:pageBlockButtons location="top"> <apex:commandButton status="processingimg" value="Save" action="{!save}" rerender="order" oncomplete="refreshparent()" /> <apex:commandButton onclick="javascript:window.close();" value="Cancel" /> <apex:actionstatus id="processingimg" > <apex:facet name="start"> <apex:image id="theImage" value="{!$Resource.ProcessingImage}" /> </apex:facet> </apex:actionstatus> </apex:pageBlockButtons> <apex:pageBlockSection id="pbsbundle" title="Available Product Bundles" rendered="true" columns="1"> <apex:pageBlockTable id="pbtbundles" value="{!lstoitem}" var="l" width="100%"> <apex:column headervalue="Product Bundle Name" value="{!l.Product_Bundle__c}" width="80%"/> </apex:pageBlockTable> </apex:pageBlockSection> <apex:pageBlockButtons location="bottom"> <apex:commandButton status="processingimg" value="Save" action="{!save}" rerender="order" oncomplete="refreshparent()"/> <apex:commandButton onclick="javascript:window.close();" value="Cancel" /> <apex:actionstatus id="processingimg1" > <apex:facet name="start"> <apex:image id="theImage1" value="{!$Resource.ProcessingImage}" /> </apex:facet> </apex:actionstatus> </apex:pageBlockButtons> </apex:pageBlock> </apex:form> </apex:page>
Hey Bob,
Figured it all out. Below is my final code. Thanks for all your help!
Class:
Page:
All Answers
In order to use the list on the page it needs to be a controller property, which is defined as public and has a public getter and setter. You don't appear to have the getter and setter part of this. You'll need to change:
To:
and then initialise it in your constructor:
Bob! So simple...thanks for the extra eyes. I have updated it but now cannot get my related Order to pull in and when I try to save the records from the page, it does not save. Any more pointers?
Class
Page
As you are using the standard controller save method, that will save the single junction object being identified through the id parameter - is that the behaviour that you are looking for?
No, looking to save the list of items that have quantity filled in.
In that case you'll need to override the save method in your extension controller and iterate the list, saving each element that you need to.
Hi Bob,
So I have been trying to take a swing at doing this and I cannot get my code to fire. Can you take a look and help me out?
Class
Page
Hey Bob,
Figured it all out. Below is my final code. Thanks for all your help!
Class:
Page:
Glad to hear you got there.
Thanks for response.
You are using data table and your code works fine for that case.
I have a form with input fields and related list at the bottom. I am opening a popup window for adding/editing related item (visual force page). After adding/editing the related list, I just want to refresh the related list section on the main window.
I tried using the oncomplete="refreshandclose()", but that is not waiting if there is any error in popup form. i.e., Required field not entered. Its just closing the popup window and reloading the main window. Reloading the main window also causes another problem i.e., if I have any unfinished changed, those are gone.
Thanks
I am really thankful to you for posting such a great solution. It helped me totally while I was working with nearly equal to your scenario with two junction objects.
Now, I am stuck as test class with one line only and that is:
How can I make this condition true in my Test Class. It shows that if statement is covered during test, but condition is not 'true' so it doesn't cover "lstSEndorsers = null". If I can cover this line then I can get my code coverage for 100%.
Your help will be really appreciated.
Hi spatelce
Well, I hope this answers your question-
Typically in some cases you have you "fudge" between controllers and test classes -- the most typical case is with HTTP callouts.
If you have to artificially manipulate a test contition between controller and test, it's easiest to have test class set your controller variable and be done with it.
For instance,
public class MyController {
public Boolean calledFromTestClass;
MyController () {
calledFromTestClass = false;
}
setIsTest(Boolean val) {
calledFromTestClass = val;
}
}
@isTest
Test.StartTest();
[---]
MyController mc = new MyController();
mc.setistest(true);
[---]
Hope this helps,