You need to sign in to do that
Don't have an account?
Claiborne
Data Grid
I have a simple visual force data grid that is trying to collect data on a single column.
This is the visual force page:
This is the controller:
For whatever, reason, the data is never written back.
This used to work with a pageBlockList, but this tag is no longer available.
Also, the pageBlockTable seems to ignore all column width information.
Ideas ?
This is the visual force page:
Code:
<apex:page controller="AssignSlabs" tabstyle="Orders__c"> <apex:form id="assignSlabs"> <apex:pageBlock mode="edit"> <p>This is a list of all available {!orderline.slab_color__r.Name} slabs for {!orderline.order__r.Name}. This order line requires {!quantityRemaining} more slabs to complete the order of {!orderQuantity} slabs.</p> <p/> <br/> <apex:pageblockSection id="Slabs"> <apex:pageblocktable align="center" value="{!slabs}" var="slab" title="Slabs" width="100%"> <apex:column headerValue="Slab" width="15%"><apex:outputLink value="/{!slab.ID}">{!slab.Name}</apex:outputlink></apex:column> <apex:column headerValue="Status" width="15%" value="{!slab.Status__c}"/> <apex:column headerValue="Quality Rating" width="20%" value="{!slab.Quality_Rating__c}"/> <apex:column headerValue="Finished Date" width="10%" value="{!slab.Finished_Date__c}"/> <apex:column headerValue="Comments" width="30%" value="{!slab.Comments__c}"/> <apex:column headerValue="Assign Slab—" width="10%"><apex:inputfield value="{!slab.Assigned__c}"/></apex:column> </apex:pageblockTable> </apex:pageblockSection> <p/> <apex:pageBlockSection id="Status"> <apex:commandButton action="{!assign}" value="Assign Slabs" rerender="Status" id="assignSlabsButton"/> <apex:commandButton action="{!cancel}" value="Cancel" id="cancelButton"/> <apex:outputLabel value="{!errormessage}"></apex:outputLabel> </apex:pageblockSection> </apex:pageBlock> </apex:form> </apex:page>
Code:
global class AssignSlabs { List<Slab__c> slabs; Order_Line__c orderline; Integer orderQuantity; Integer quantityRemaining; String errormessage; String testMessage; List<String> headers; public AssignSlabs() { slabs = getSlabs(); orderline = getOrderLine(); quantityRemaining = getQuantityRemaining(); orderQuantity = getOrderQuantity(); headers = getHeaders(); } public List<Slab__c> getSlabs() { List <Slab__c> slabs = [ select Name, comments__c, slab_color__c, status__c, finished_date__c, assigned__c, order_line__c, quality_rating__c from slab__c where slab_color__c = :ApexPages.currentPage().getParameters().get('slabcolor') AND Order_Line__c = NULL order by slab_color__c, finished_date__c]; return slabs; } public Order_Line__c getOrderLine() { Order_Line__c orderline = [ select id, slab_color__r.Name, Order__r.Name, quantity__c, assigned__c from order_line__c where id = :ApexPages.currentPage().getParameters().get('olid')]; return orderLine; } public Integer getQuantityRemaining() { Integer QuantityRemaining = 0; orderline.assigned__c = [select count() from slab__c where order_line__c = :ApexPages.currentPage().getParameters().get('olid')]; quantityRemaining = orderLine.quantity__c.intValue() - orderline.assigned__c.intValue(); return quantityRemaining; } public Integer getOrderQuantity() { Integer OrderQuantity = 0; orderQuantity = orderline.quantity__c.intValue(); return orderQuantity; } public string[] getheaders() { return new string [] {'Slab','Status','Quality Rating','Finished Date', 'Comments', 'Assign Slab —'} ; } public String geterrorMessage() { return errormessage; } public PageReference assign(){ Integer entries = slabs.size(); Integer assigned = 0; // integer must be initialized or it will be null Boolean isError = false; PageReference orderlinepage = new PageReference('/' + orderLine.id); PageReference assignSlabs = new PageReference('/apex/AssignSlabs'); for (Integer e = 0; e < entries; e++){ if (slabs[e].assigned__c) { slabs[e].order_line__c = orderline.ID; assigned++; } } if (assigned <= quantityremaining) { // update slab records try { update slabs; } catch(System.DMLException e) { isError = true; ApexPages.addMessages(e); } // update assigned count in orderline record orderline.assigned__c = [select count() from slab__c where order_line__c = :ApexPages.currentPage().getParameters().get('olid')]; try { update orderline; } catch(System.DMLException e) { isError = true; ApexPages.addMessages(e); } return (isError) – assignSlabs:orderlinepage; } else { errormessage = 'You have selected too many slabs for assignment. Your selections would assign ' + assigned + ' slabs to this order, which only requires ' + quantityRemaining + ' slabs.'; return assignSlabs; } } public PageReference cancel() { PageReference orderlinepage = new PageReference('/' + orderline.id); return orderlinepage; } }
This used to work with a pageBlockList, but this tag is no longer available.
Also, the pageBlockTable seems to ignore all column width information.
Ideas ?
after adding debug statements , then running the system log window with apex code category set to debug, you should see messages that will allow you to understand why the data is now not writing to the database.
Thanks for replying.
I put the debug statements in. There is a snippet below:
What appears to be happening is that when the method assign is called from the VisualForce page, it is retrieving the slabs list from storage rather than passing the slabs object from the VisualForce page.
I think I am missing something here, but I have no idea what.
You're basically running all of your queries right away and saving them off. Then your page is going to call all of your getters and re-run the queries.
Then in your update function you're using what you saved off in your constructor, before any buttons were pressed, so you're not going to have any updated values.
I think what you want to do is instead of calling your getters in your constructor, turn all of your getters into lazy loads, like
I'm not totally sure but I think this might help you because you won't be performing the query more than once, to ensure you're using the same data in your action method that your page was using.
Try that with all of your getters and see if it works. It is better for your page performance anyway since you won't be running queries you have already run.
I tried to turn my getters into lazy getters, but I had to rename the variable to get anything out.
But still nothing is getting to the save function (called assign).
I have dumbed down the function trying to get anything to work.
Here is the class:
Here it the page:
This code displays the values OK, but when I try to "assign" it gives me
Attempt to de-reference a null object.
Short version (which may or may not work)
-------------------
change line 2 from:
The idea is that you check for null in your local class variable, and then assign. You don't need to create a new variable in your method.
I hope this gives you a better idea of what's going wrong.
Thank you both for the suggestions, but neither one has any impact on the result.
The code is still not working.
When I use the test - if (slabs == null), it always fails, even when slabs is null, so I get no slabs. For whatever reason, "lazy loading" does not work.
Any other ideas?
I'm sure there's something very obvious going wrong, it just has not jumped out at us yet.
This did not work -
This worked:
So, as Tennyson said, " . . . Their's not to reason why . . . " but it works.
And in your second code snippet there's still an issue because you are never assigning a value to slabs, which means that it's always going to be null, which means that it's always going to run your query. It might work, but it's not efficient.
This (your first example) is wrong:
This is right:
This (your second example) is causing your query to get re-run because you never assign slabs:
I highly suggest the second block of code that I posted. Not just because it's cleaner or even faster (because for all I know it's negligible to you whether or not your query gets run two times or ten), but also because it really only makes sense for it to get run once. If in between your queries someone changes the slabs out from under you, your data might not do what you expect it to.