Get Order Products based Order ID - salesforce

I have added a standardcontroller ("OrderItem) to my visualforce page and also an extension. I am trying to view the OrderItem details based on the Order record ID of the page. But I keep getting the error "Id value is not valid for the OrderItem standard controller".
Because I want to override the "EditProducts" button in "Order Products" with my visualforce page. I must use a standardController for "OrderItem". Which is the API name for "Order Products"
Please help. Thank you!
<apex:page standardController="OrderItem" extensions="OrderProductExtension" lightningStylesheets="true">
<apex:form>
<apex:pageBlock id="products_list" title="Order Products">
<apex:pageBlockTable value="{! Products}" var="Oi" >
<apex:column value="{! Oi.PricebookEntry.Product2.Name}">
<apex:facet name="header">
<apex:commandLink action="{! sortByName}" reRender="products_list">
<apex:outputText value="{! $ObjectType.OrderItem.Fields.Product2Id.Label}"/>
</apex:commandLink>
</apex:facet>
</apex:column>
<apex:column value="{! Oi.Quantity}">
<apex:facet name="header">
<apex:commandLink action="{! sortByQuantity}" reRender="products_list">
<apex:outputText value="{! $ObjectType.OrderItem.Fields.Quantity.Label}"/>
</apex:commandLink>
</apex:facet>
</apex:column>
<apex:column value="{! Oi.UnitPrice}">
<apex:facet name="header">
<apex:commandLink action="{! sortByUnitPrice}" reRender="products_list">
<apex:outputText value="{! $ObjectType.OrderItem.Fields.UnitPrice.Label}"/>
</apex:commandLink>
</apex:facet>
</apex:column>
<apex:column value="{! Oi.TotalPrice}">
<apex:facet name="header">
<apex:commandLink action="{! sortByTotalPrice}" reRender="products_list">
<apex:outputText value="{! $ObjectType.OrderItem.Fields.TotalPrice.Label}"/>
</apex:commandLink>
</apex:facet>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
my custom extension:
public class OrderProductExtension{
private String sortOrder = 'TotalPrice';
private String currentId;
public OrderProductExtension(ApexPages.StandardController stdController){
this.currentId = stdController.getId();
}
public List<OrderItem> getProducts(){
List<OrderItem> products = Database.query('SELECT Quantity, PricebookEntry.Product2.Name, UnitPrice, TotalPrice FROM OrderItem WHERE' +
' orderId =: currentId ORDER BY ' + sortOrder + ' ASC');
return products;
}
public void sortByName(){
this.sortOrder = 'PricebookEntry.Product2.Name';
}
public void sortByQuantity(){
this.sortOrder = 'Quantity';
}
public void sortByUnitPrice(){
this.sortOrder = 'UnitPrice';
}
public void sortbyTotalPrice(){
this.sortOrder = 'TotalPrice';
}
}

You will need to cheat a bit.
Option 1 is to make the button on Order, not Order Items. It'll display in wrong place but you should be able to hide old button and train users. Then the order id will be passed and it'll work OK. the standardController="Order" will be needed though.
Option 2 is bit messy. something like <apex:page standardController="OrderItem" recordSetVar="records" would indicate that you plan to use this page for multiple items, not just one. Maybe you heard about StandardSetController? But then you don't have the order id (well, if there are existing lines you can do it but what if there are none added yet). See if you can make the button override as type URL (/apex/MyPageName?id={!Order.Id}), not as type button.

Related

How to get the ID for the specified custom object record form visual force page to apex class?

I'm trying to do a small project and I am stuck as I couldn't pull out the ID of the specified record of a custom object from vf page to apex class the code is given below
<apex:page standardController="enquiry__c" extensions='callme' recordSetVar="items"> <!--here enquiry__c is a custom object -->
<apex:form>
<apex:pageblock>
<apex:pageblocksection>
<apex:pageBlockTable value='{!items}' var='en' width="200" >
<apex:column value='{!en.name}'/>
<apex:column value='{!en.Student_Enquiry_Name__c}'/>
<apex:column value='{!en.phone__c}'/>
<apex:column value='{!en.email__c}'/>
<apex:column value='{!en.Status__c}'/>
<apex:column headerValue="Action">
<apex:commandButton value='convert to student'/>
</apex:column>
<apex:column headerValue="Record ID">
<apex:outputText value='{!en.id}' />
</apex:column>
</apex:pageBlockTable>
<apex:commandButton value='new' action='{!newenquiry}'/>
</apex:pageblocksection>
</apex:pageblock>
</apex:form>
</apex:page>
And the output is as below
So my question is when I click on button "convert to student" beside a particular record I have to get the ID of that record into apex class and the apex code is given below
public class callme
{
public enquiry__c e {get;set;}
public id i {get;set;}
public callme(ApexPages.StandardSetController c)
{
}
public PageReference newenquiry()
{
PageReference p=new PageReference('/apex/vfTab_on_enquiry');
return p;
}
public PageReference newcourse()
{
PageReference p=new PageReference('/apex/courseInsertion');
return p;
}
}
Please help me with this, thanks in advance.
-> you can use actionFunction for this.
-> create a js function called callActionMethod and invoke this function on the click of "convert to student" button.
-> create convertStudent method in apex class.
-> create an actionfunction named callConvertStudentMethod and pass the record id to the parameter.
Apex Controller:
public class callme
{
public enquiry__c e {get;set;}
public id i {get;set;}
public callme(ApexPages.StandardSetController c)
{
e = new enquiry__c();
}
public PageReference newenquiry()
{
PageReference p=new PageReference('/apex/vfTab_on_enquiry');
return p;
}
public PageReference newcourse()
{
PageReference p=new PageReference('/apex/courseInsertion');
return p;
}
public void convertStudent(){
system.debug('record id ---->'+i);
e = [SELECT FIELDS(ALL) FROM enquiry__c WHERE ID = :i];
system.debug('student record ---->'+e);
}
}
VF Page:
<apex:page standardController="enquiry__c" extensions='callme' recordSetVar="items"> <!--here enquiry__c is a custom object -->
<apex:form>
<apex:pageblock id='resultPanel'>
<apex:pageblocksection>
<apex:pageBlockTable value='{!items}' var='en' width="200" >
<apex:column value='{!en.name}'/>
<apex:column value='{!en.Student_Enquiry_Name__c}'/>
<apex:column value='{!en.phone__c}'/>
<apex:column value='{!en.email__c}'/>
<apex:column value='{!en.Status__c}'/>
<apex:column headerValue="Action">
<apex:commandButton value='convert to student' onclick="callActionMethod({!en.id})" />
</apex:column>
<apex:column headerValue="Record ID">
<apex:outputText value='{!en.id}' />
</apex:column>
</apex:pageBlockTable>
<apex:commandButton value='new' action='{!newenquiry}'/>
</apex:pageblocksection>
</apex:pageblock>
<apex:actionFunction name="callConvertStudentMethod" action="{!convertStudent}" reRender="resultPanel" >
<apex:param name="firstParam" assignTo="{!i}" value="" />
</apex:actionFunction>
</apex:form>
<script type="text/javascript">
function callActionMethod(ele)
{
callConvertStudentMethod(ele);
}
</script>
</apex:page>

Adding new records and displaying them on visualforce page from custom object

I get an error in visualforce page now. Saying:
Error:Unknown property 'tudent__cStandardController.d'
This happens when I add the delete commandLink button to the vf page. Before I add it it doesn't put out an error but on it doesn't display records on the page.
Thanks for help in advance
Visualforce code:
<apex:page standardController="tudent__c" extensions="vidsav">
<apex:form >
<apex:outputPanel id="check">
<apex:pageBlock title="Dodaj Študenta">
<apex:pageBlockSection columns="1">
<apex:inputField value="{! tudent__c.Name }"/>
<apex:inputField value="{! tudent__c.priimek__c }"/>
<apex:inputField value="{! tudent__c.Datum_rojstva__c }"/>
<apex:inputField value="{! tudent__c.letnik__c }"/>
<apex:inputField value="{! tudent__c.Naslov__c }"/>
<apex:inputField value="{! tudent__c.naziv_fakultete__c }"/>
<apex:inputField value="{! tudent__c.tudijski_program__c }"/>
<apex:inputField value="{! tudent__c.tip_tudija__c }">
<apex:actionSupport event="onchange" rerender="check" />
</apex:inputField>
<apex:inputField value="{! tudent__c.Samopla_nik__c }" rendered="{!IF( tudent__c.tip_tudija__c == 'izredni', true, false )}" />
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save" />
<apex:commandButton value="Cancel" action="{!cancel}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:outputPanel>
<apex:pageBlock title="Študenti">
<apex:pageBlockTable value="{!studentsR}" var="s" >
<apex:commandLink action="{!deleteStudent}" onclick="if(!confirm('Are you sure?')) return false;">`enter code here`Del
<apex:param value="{!d.Id}" name="idToDel" assignTo="{!SelectedStudentId}"/>
</apex:commandLink>
<apex:column value="{!s.Name}"/>
<apex:column value="{!s.priimek__c}"/>
<apex:column value="{!s.Datum_rojstva__c}"/>
<apex:column value="{!s.letnik__c}"/>
<apex:column value="{!s.Naslov__c}"/>
<apex:column value="{!s.naziv_fakultete__c}"/>
<apex:column value="{!s.tudijski_program__c}"/>
<apex:column value="{!s.tip_tudija__c}"/>
<apex:column value="{!s.Samopla_nik__c}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
public class vidsav {
public vidsav(ApexPages.StandardController controller) {
}
public List<tudent__c> studentsR {get;set;}
public String SelectedStudentId {get;set;}
public vidsav() {
loadData();
}
public void loadData() {
studentsR = [Select id,Name,priimek__c,Datum_rojstva__c,letnik__c,Naslov__c,naziv_fakultete__c,tudijski_program__c,tip_tudija__c,Samopla_nik__c, CreatedDate from tudent__c Order By CreatedDate desc];
}
public void deleteStudent(){
studentsR = [Select id,Name,priimek__c,Datum_rojstva__c,letnik__c,Naslov__c,naziv_fakultete__c,tudijski_program__c,tip_tudija__c,Samopla_nik__c, CreatedDate from tudent__c where id = :SelectedStudentId];
if(studentsR.size() > 0 || studentsR[0].Id != ''){
delete studentsR;
}
loadData();
}
}
Here is the updated code you should use ->
1) Update your PageBlockTable to this -
<apex:pageBlockTable value="{!studentsR}" var="s" >
<apex:column value="{!s.Name}"/>
<apex:column value="{!s.priimek__c}"/>
<apex:column value="{!s.Datum_rojstva__c}"/>
<apex:column value="{!s.letnik__c}"/>
<apex:column value="{!s.Naslov__c}"/>
<apex:column value="{!s.naziv_fakultete__c}"/>
<apex:column value="{!s.tudijski_program__c}"/>
<apex:column value="{!s.tip_tudija__c}"/>
<apex:column value="{!s.Samopla_nik__c}"/>
<apex:column>
<apex:commandLink action="{!deleteStudent}" onclick="if(!confirm('Are you sure?')) return false;" value="Del">
<apex:param value="{!s.Id}" name="idToDel" assignTo="{!SelectedStudentId}"/>
</apex:commandLink>
</apex:column>
You needed to put your commandLink inside an apex:column in order for it to display correctly in the pageBlockTable and also of course the d had to be replaced with a s since your table variable is s (var="s").
2) Update your Controller to this -
public class vidsav {
public vidsav(ApexPages.StandardController controller) {
loadData();
}
public List<Account> studentsR {get;set;}
public String SelectedStudentId {get;set;}
public void loadData() {
studentsR = [Select id,Name,priimek__c,Datum_rojstva__c,letnik__c,Naslov__c,naziv_fakultete__c,tudijski_program__c,tip_tudija__c,Samopla_nik__c, CreatedDate from tudent__c Order By CreatedDate desc];
}
public void deleteStudent(){
studentsR = [Select id,Name,priimek__c,Datum_rojstva__c,letnik__c,Naslov__c,naziv_fakultete__c,tudijski_program__c,tip_tudija__c,Samopla_nik__c, CreatedDate from tudent__c where id = :SelectedStudentId];
if(studentsR.size() > 0 || studentsR[0].Id != ''){
delete studentsR;
}
loadData();
}
}
You were using two constructors and were calling loadData() from the wrong one and this is why it was not loading data. With this you will be able to delete records and reload your table.
Replace {!d.Id} with {!s.Id}
The variable used to traverse the records in the pageblocktable is s not d.

Salesforce show all cases based on criteria in visualforce

All,
I am attempting to show all cases that meet certain criteria in salesforce using visual force page, then I would like to use that data in a gantt chart. I do know much about coding, but trying based on user manuals. This visualforce page comes back without any case information.
<apex:page standardController="Case" >
<apex:form >
<apex:pageBlock >
<apex:pageMessages />
<apex:pageBlockButtons>
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockButtons>
<apex:PageBlockTable value="{!Case}" var="c">
<apex:column value="{!c.Account}"/>
<apex:column value="{!c.Number}"/>
<apex:column value="{!c.Owner}"/>
<apex:column headerValue="Install Date">
<apex:inputField value="{!a.Planned_Install_Date__c}"/>
</apex:column>
</apex:PageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
You need an apex controller class to get data from Salesforce to the visual force page. Something like this:
Visual Page:
<apex:page standardController="Case" controller="MyCaseController">
<apex:form >
<apex:pageBlock >
<apex:pageMessages />
<apex:pageBlockButtons>
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockButtons>
<apex:PageBlockTable value="{!Cases}" var="c">
<apex:column value="{!c.Accountid}"/>
<apex:column value="{!c.CaseNumber}"/>
<apex:column value="{!c.OwnerId }"/>
</apex:PageBlockTable>
</apex:pageBlock>
</apex:form>
Controller:
public class MyCaseController {
public list<Case> cases {get;set;}
public MyCaseController(){
cases = [select id, accountid, CaseNumber, OwnerId from case];
}
}
You should be able to accommodate the code to your needs.

Rendered doesn't work

If I try to rendered the code below it doesn't work.
If I show me the size of the list I will get the value 1, but nothing is happen.
Where is the mistake?
I tried it also with != NULL .isempty etc., the same problem.
<apex:pageblock title="Unternehmen Detail" id="pbAccDL" rendered="{!If(AccDList2.size > 0,true,false)}>
<apex:pageblocktable value="{!AccDList2}" var="AccD" rendered="{!IF(AccDList2.size != 0, true, false)">
<apex:column style="font-size:16pt; font-weight: bold" headerValue="" value="{!AccD.Name}"/>
</apex:pageblocktable>
<apex:pageblocktable value="{!AccDList2}" var="AccD" columnswidth="50%, 25%, 25%">
<apex:column value="{!AccD.BillingStreet}"/>
<apex:column value="{!AccD.BillingPostalCode}"/>
<apex:column value="{!AccD.BillingCity}"/>
</apex:pageblocktable>
Public List <Account> getAccDList2() {
List <Account> AccD = [SELECT Id, Name, RecordTypeId, Status__c, Kunde_seit__c, Billingstreet, BillingPostalCode, BillingCity FROM Account WHERE Id = :SelectedAccountId];
RETURN AccD;
}
Public pageReference getAccDList() {
getAccDList2();
//RETURN NULL;
RETURN ApexPages.CurrentPage();
}
I don't understand the problem, because I use the same function for an other pageblock and works fine.
<apex:pageblock title="Account" id="pbAcc" rendered="{!IF(AccList2.size != NULL,true,false)}">
<apex:pageblockButtons location="top">
<apex:commandButton value="page 1" rerender="pbAcc" action="{!FirstPage}" disabled="{!prev}"/>
<apex:commandButton value="prev page" rerender="pbAcc" action="{!previous}" disabled="{!prev}"/>
<apex:commandButton value="next page" rerender="pbAcc" action="{!next}" disabled="{!nxt}"/>
<apex:commandButton value="last page" rerender="pbAcc" action="{!LastPage}" disabled="{!nxt}"/>
</apex:pageblockButtons>
<apex:pageblocktable value="{!AccList2}" var="Acc" columnswidth="5%, 70%, 25%">
<apex:column headervalue="LINK">
<apex:outputLink target="_blank" value="/{!Acc.Id}">Details</apex:outputLink>
</apex:column>
<apex:column headervalue="Account">
<apex:outputField value="{!Acc.Name}" />
<apex:actionSupport event="onclick" action="{!getOppList}" rerender="pbOpp, pbAccDL, pbAccDR, pbOppD">
<apex:param assignTo="{!SelectedAccountId}" value="{!Acc.Id}" name="SelectedAccountId"/>
</apex:actionSupport>
</apex:column>
<apex:column headervalue="City">
<apex:outputField value="{!Acc.BillingCity}"/>
</apex:column>
</apex:pageblocktable>
Could someone help me please.
Thanks,
peX
Note that if the outer container (apex:pageBlock in your case) wasn't rendered at page load time you wouldn't be able to rerender inner elements later. To make sure this is not the problem remove rendered attribute from outer apex:pageblock. One more thing to note in your second example you are writing AccList2.size != NULL which is never the case (if AccList2 is a list) you should check with AccList2.size > 0

Salesforce Updating Checkbox value on change event

Here hidden_field__c is a checkbox.
When in the VFP if user changes the checkbox to true in the database it still shows as false and vice-versa
Can someone please point out what's missing in my code.?
This is my code.
-----------controller --------------
public class dataTableCon {
List<Account> accounts;
public List<Account> getAccounts() {
if(accounts == null) accounts = [select name, owner.name,hidden_field__c from account limit 10];
return accounts;
}
}
---------VFP-------------
<apex:page controller="dataTableCon" id="page">
<apex:form >
<apex:pageBlock id="theBlock">
<apex:dataTable value="{!accounts}" var="account" id="theTable" rowClasses="odd,even" styleClass="tableClass">
<apex:column >
<apex:facet name="header">Private</apex:facet>
<apex:inputCheckbox value="{!account.hidden_field__c}" >
<apex:actionSupport event="onchange" rerender="theBlock"/>
</apex:inputCheckbox>
</apex:column>
<apex:column >
<apex:facet name="header">Name</apex:facet>
<apex:outputText value="{!account.name}" >
</apex:column>
<apex:column >
<apex:facet name="header">Owner</apex:facet>
<apex:outputText value="{!account.owner.name}" >
</apex:column>
</apex:dataTable>
</apex:pageBlock>
</apex:form>
</apex:page>
You need to have some mechanism that actually saves the changes. Try adding an <apex:commandButton> inside your <apex:form>, and then have that button call an action that saves.
Apex:
public PageReference save()
{
update accounts;
}
Visualforce:
<apex:commandButton value="Save" action="{!save}"/>

Resources