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
admin1.3954222418789446E12admin1.3954222418789446E12 

Apex controller issues

Hi,

I have been trying to create a way of allowing my users to edit only the price books I want them to and this has led me down the path of creating a visualforce page and custom controller

Although I have a range of programming experience, I am still relatively new to both VF and Apex.  What I have is something of a cobbled together mash-up of code found through the website, so do please feel free to point out inconsistencies.

Here is my code:
[code]
<apex:page Controller="Custom_pricebookentry_controller">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection >
                <apex:inputField value="{!pricebook_List.pricebook2Id}"/>
                <apex:inputField value="{!SearchCriteria.CurrencyIsoCode}"/>
                <apex:commandButton value="Search" action="{!filterPBEs}" reRender="pbe_section,error"/>
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockSection>

            <apex:pageBlockTable value="{!filteredPBEs}" id="pbe_section" var="pbe">
                <apex:column headerValue="Name"><apex:outputField value="{!pbe.name}"/></apex:column>
                <apex:column headerValue="Ccy"><apex:outputField value="{!pbe.currencyIsoCode}"/></apex:column>
                <apex:column headerValue="Price"><apex:outputField value="{!pbe.unitPrice}"/></apex:column>
                <apex:column headerValue="New Price"><apex:inputField value="{!pbe.unitPrice}"/></apex:column>
            </apex:pageBlockTable>
           
            <apex:pageMessages id="error"></apex:pageMessages>

        </apex:pageBlock>
    </apex:form>
</apex:page>
[/code]

and

[code]
public with sharing class Custom_pricebookentry_controller {
    public List<priceBookEntry> FilteredPBEs{get;set;}
    public priceBookEntry SearchCriteria{get;set;}
    public opportunity pricebook_list{get;set;}
    public Custom_pricebookentry_controller ()
    {
        pricebook_list = new opportunity();
        SearchCriteria = new priceBookEntry();
    }
   
    public void filterPBEs()   
    {
        FilteredPBEs = new List<priceBookEntry>();
       
        FilteredPBEs = [SELECT Name,UnitPrice
                          FROM PricebookEntry
                         WHERE Pricebook2Id =: pricebook_list.Pricebook2Id
                           AND CurrencyIsoCode =: SearchCriteria.CurrencyIsoCode];
         if(FilteredPBEs.size() == 0)
         {
            Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No records to Display'));
         }
    }
   
    public void save()   
    {
        priceBookEntry pbe= new priceBookEntry();

        try
        {
            insert pbe.unitPrice;
        }
        catch(Exception ex)
        {
          System.debug('\n\nException ='+ex.getMessage()+'\n\n');
        }

    }
}
[/code]

Right now I get an error "Error: Compile Error: DML requires SObject or SObject list type: Decimal at line 31 column 13", so presumably I have to define the variable type somehow.

Any help would be hugely appreciated.

David
Best Answer chosen by admin1.3954222418789446E12
Blake TanonBlake Tanon
insert pbe.unitPrice;
the system thinks you're trying to insert a field, instead of an object.  You're also trying to save a record with no data in it.  

I assume you're waiting to update FilteredPBEs with new values.

In that case, your save method should be like this....

public void save()  
    {
        try
        {
            update FilteredPBEs;
        }
        catch(Exception ex)
        {
          System.debug('\n\nException ='+ex+'\n\n');
        }

    }


that will take the list you have displayed on the page, along with edits from the user and update the records.

Next, I don't see where you're calling your filterPBEs method.  So the list wont show on the page, try to call it from the class's method so the list loads when the class is called.

All Answers

Blake TanonBlake Tanon
insert pbe.unitPrice;
the system thinks you're trying to insert a field, instead of an object.  You're also trying to save a record with no data in it.  

I assume you're waiting to update FilteredPBEs with new values.

In that case, your save method should be like this....

public void save()  
    {
        try
        {
            update FilteredPBEs;
        }
        catch(Exception ex)
        {
          System.debug('\n\nException ='+ex+'\n\n');
        }

    }


that will take the list you have displayed on the page, along with edits from the user and update the records.

Next, I don't see where you're calling your filterPBEs method.  So the list wont show on the page, try to call it from the class's method so the list loads when the class is called.
This was selected as the best answer
admin1.3954222418789446E12admin1.3954222418789446E12
Thanks very much for your answer Blake.  The insert / update point hit the nail on the head perfectly!

As an aside the FilterPBEs action is called from the Search button, which then reRenders the pagetable.
Blake TanonBlake Tanon
Glad I could help!