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
Eduardo DeslandesEduardo Deslandes 

insert OpportunityLineItem with custom controller

Hi, I trying to create a visualforce page to insert OpportunityLineItems, but it's not working, when I click "Salvar" (save) nothing happens, anyone could help me please?

page:

<apex:page controller="ProdOpController">
    <apex:form >
        <apex:pageBlock title="Novo Produto da Oportunidade">
            <apex:pageBlockSection title="Informações do Produto">
                <apex:inputField value="{!prodop.OpportunityId}"/>
                <apex:inputField value="{!prodop.Quantity}"/>
                <apex:inputField value="{!prodop.UnitPrice}"/>
                <apex:selectList multiselect="false" size="1" value="{!prodop.Product2Id}">
                    <apex:selectOptions value="{!prodOptions}"/>
                </apex:selectList>
            </apex:pageBlockSection>
        <apex:pageBlockButtons >
            <apex:commandButton action="{!salvar}" value="Salvar"/>
        </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

controller:
 
public class ProdOpController {
    
    public OpportunityLineItem prodop{ get; private set; }
    
    public ProdOPController(){
        prodop = new OpportunityLineItem();
        System.debug('Entrei no construtor');
    }
    
    public PageReference salvar(){
        System.debug('Entrei em salvar');
        try{
            insert prodop;
        }
        catch(System.DMLException e){
        	System.debug('insert prodop falhou');
            System.debug(e);
            ApexPages.addMessages(e);
            return null;
        }
        PageReference redirectSuccess = new ApexPages.StandardController(prodop).view();
        return (redirectSuccess); 
    }

    public List<SelectOption> getProdOptions(){
        List<PricebookEntry> prods = Database.query(
        	'SELECT Id, name FROM PricebookEntry'
        );
        List<SelectOption> options = new List<SelectOption>();
        for(PricebookEntry p : prods){
            options.add(new SelectOption(p.Id, p.Name));
        }
        System.debug('listei as opcoes de produtos');
        return options;
    }
}

 
Best Answer chosen by Eduardo Deslandes
SonamSonam (Salesforce Developers) 
Hi,

You have written the page and controller correctly, the only thing missing here is the PriceBookEntry ID which you had to assign to the Lineitem instance.Check the below code for the update required:

Controller code:

public class ProdOpController {
    
    public OpportunityLineItem prodop{ get; set; }
    
    public ProdOPController(){
        prodop = new OpportunityLineItem();
        System.debug('Entrei no construtor');
    }
    
    public PageReference save(){
        system.debug('---->222 '+prodop.Product2Id);
        //PriceBookEntry priceBookList = [SELECT Id FROM PriceBookEntry WHERE Product2Id =: prodop.Product2Id and PriceBook2.isStandard=true LIMIT 1];

        prodop.PricebookEntryId = prodop.Product2Id; //'01u28000006fO2V';
        System.debug('Entrei em salvar---> '+prodop);
        
        try{
            insert prodop;
        }
        catch(System.DMLException e){
            System.debug('insert prodop falhou');
            System.debug(e);
            ApexPages.addMessages(e);
            return null;
        }
        PageReference redirectSuccess = new ApexPages.StandardController(prodop).view();
        return (redirectSuccess); 
    }

    public List<SelectOption> getProdOptions(){
        List<PricebookEntry> prods = Database.query(
            'SELECT Id, name FROM PricebookEntry'
        );
        List<SelectOption> options = new List<SelectOption>();
        for(PricebookEntry p : prods){
            options.add(new SelectOption(p.Id, p.Name));
        }
        System.debug('listei as opcoes de produtos');
        return options;
    }
}

All Answers

SonamSonam (Salesforce Developers) 
Hi,

You have written the page and controller correctly, the only thing missing here is the PriceBookEntry ID which you had to assign to the Lineitem instance.Check the below code for the update required:

Controller code:

public class ProdOpController {
    
    public OpportunityLineItem prodop{ get; set; }
    
    public ProdOPController(){
        prodop = new OpportunityLineItem();
        System.debug('Entrei no construtor');
    }
    
    public PageReference save(){
        system.debug('---->222 '+prodop.Product2Id);
        //PriceBookEntry priceBookList = [SELECT Id FROM PriceBookEntry WHERE Product2Id =: prodop.Product2Id and PriceBook2.isStandard=true LIMIT 1];

        prodop.PricebookEntryId = prodop.Product2Id; //'01u28000006fO2V';
        System.debug('Entrei em salvar---> '+prodop);
        
        try{
            insert prodop;
        }
        catch(System.DMLException e){
            System.debug('insert prodop falhou');
            System.debug(e);
            ApexPages.addMessages(e);
            return null;
        }
        PageReference redirectSuccess = new ApexPages.StandardController(prodop).view();
        return (redirectSuccess); 
    }

    public List<SelectOption> getProdOptions(){
        List<PricebookEntry> prods = Database.query(
            'SELECT Id, name FROM PricebookEntry'
        );
        List<SelectOption> options = new List<SelectOption>();
        for(PricebookEntry p : prods){
            options.add(new SelectOption(p.Id, p.Name));
        }
        System.debug('listei as opcoes de produtos');
        return options;
    }
}
This was selected as the best answer
Eduardo DeslandesEduardo Deslandes

Thanks Sonam,

I managed to make it work by changing the  

<apex:selectList multiselect="false" size="1" value="{!prodop.Product2Id}">

by

<apex:selectList multiselect="false" size="1" value="{!prodop.PricebookEntryId}">

too, but that is confused because the pricebookentry field is not showed in the OpportunityLineItem schema, anyway now I know this issue :)