0

I have a custom visualforce page that has the Product2 standard controller and a class as extension. The objective here is to make it possible to enter the product information, and the user can click "Save and New", and the page will reload after inserting a new product with the informations that were typed in.

The issue I'm having right now is that even though I get all the fields from the visualforce page using the inputField markup, Salesforce keeps throwing me an error saying that a required field is missing. However this field is set (I can even check it being set using System.debug on the object, just right before the insertion).

Since I can check that the object has all the necessary fields using the debug feature, I would like to know if I'm missing something here, or if this is a bug?

The code is as follows:

<apex:page showHeader="true" sidebar="true" standardController="Product2" extensions="CadastroProdutosController">
<!-- Name: CriaMultiplosProdutos -->
    <apex:pageMessages/>
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection title="Novo Produto" collapsible="false" columns="2">
                <apex:inputField value="{!prod.ProductCode}" label="Código do Produto" required="true"/>
                <apex:inputField value="{!prod.Fabricante__c}" label="Fabricante" required="true"/>
                <apex:inputField value="{!prod.Modelo_do_produto__c}" label="Modelo do Produto"/>
                <apex:inputField value="{!prod.Description}" label="Descrição" required="true"/>
                <apex:inputField value="{!prod.regimeEspecial__c}" label="Regime Especial"/>
                <apex:inputField value="{!prod.Family}" label="Família de Produtos" required="true"/>
                <apex:inputField value="{!prod.Observacao__c}" label="Observação"/>
                <apex:inputField value="{!prod.Ativo__c}" label="Ativo"/>
                <apex:inputField value="{!prod.Fabricante_Codigo_do_produto__c}" label="Fabricante + Código do Produto"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons>
                <apex:commandButton action="{!mySave}" value="Salvar"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

And the controller:

public with sharing class CadastroProdutosController
{
    public ApexPages.StandardController controller;
    public Product2 prod {get;set;}

    public CadastroProdutosController(ApexPages.StandardController stdController)
    {
        this.controller = stdController;
        this.prod = new Product2();
    }

    public PageReference mySave()
    {
        try
        {
            System.debug(this.prod);
            insert this.prod;
            return new PageReference('/apex/CriaMultiplosProdutos');
        }
        catch (Exception e)
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getCause() + ' ' + e.getMessage() + ', Line: ' + e.getLineNumber()));
        }
        return null;
    }
}
4
  • 2
    Yes, you are obviously missing something or it would work. However, without seeing your code it is impossible to say.
    – Eric
    Oct 31, 2015 at 15:57
  • Edited my question with the code. Please, take a look. Oct 31, 2015 at 16:00
  • Do you have workflow rules or triggers involved? It's possible that while the field is set right before insertion, some transaction logic might be wiping out the data. I strongly suspect your issue lies further down the transaction lifecycle than what you imagine.
    – sfdcfox
    Oct 31, 2015 at 16:22
  • There is an 'after insert' trigger on Product2 object, but since it is running after the record is inserted, it is definitely not the cause of the problem. I also checked the workflow rules but didn't see anything changing the name or code of the product. Oct 31, 2015 at 16:34

1 Answer 1

2

I put your code into my sandbox and I got the following error:

Required fields are missing: [Product Name]

So you are missing the name not the product code

Adding the following and it works correctly:

<apex:inputField value="{!prod.Name}"  required="true"/>
2
  • That was it. It was a simple interpretation problem. Thank you Eric! I was so focused on the functionality that mistake the product's code for its name field in the code. Oct 31, 2015 at 16:40
  • 1
    No worries.....as you can see via the thread, we ALL get focused on the wrong issue at times (Especially me) .
    – Eric
    Oct 31, 2015 at 16:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .