+ Start a Discussion
Mick ManMick Man 

visualforce

I would like to add a record in a custom object products and the same time a record in a custom object jointure__c, 
Here is my controller
public class NouveauProduitExtensions {

    public NouveauProduitExtensions(ApexPages.StandardController controller) {
    }
    
    public PageReference cancel() {
        return Page.Nouveau_produit;
    }
    public JointMatProd__c Jointure {get; set;} 

    public Produit__c prod { get; set; }

    public NouveauProduitExtensions() {
        prod = new Produit__c();
        
        Jointure = new JointMatProd__c();
    }
    
    public PageReference showAddMatierePage() {
        return Page.Produit_Add_Matiere;
    }
    
    public PageReference Save() {
        insert prod;
        insert JointList;
        PageReference acctPage = new ApexPages.StandardController(prod).view();

        acctPage.setRedirect(true);

        return acctPage;
    }

     public List<cMatiere> matiereList {get; set;}
     public List<JointMatProd__c> JointList {get; set;}
     public List<MatierePremiere__c> selectedMatieres{get;set;}
     public Integer value {get;set;} 

     public List<cMatiere> getMatieres() 

     {
      if(matiereList == null) {
       matiereList = new List<cMatiere>();
       for(MatierePremiere__c c : [select Id, Name, Unite__c from MatierePremiere__c])
    
       {

       matiereList.add(new cMatiere(c));
       }
      }
      return matiereList;
     }  
    
public PageReference processSelected()

 {
  
   
    selectedMatieres = new List<MatierePremiere__c>();
    JointList = new List<JointMatProd__c>();
    
    for (cMatiere cMat : getMatieres()) {
        if(cMat.selected == true) {
            selectedMatieres.add(cMat.mat);
        }
    }
   value = selectedMatieres.size();
   System.debug('printing listcontc'+selectedMatieres.size());
    
    for (MatierePremiere__c mat : selectedMatieres)
     {
        JointList.add(new jointMatProd__c(Produit__c = prod.id, MatierePremiere__c = mat.id, Quantite__c=0));
    }

   return page.Nouveau_produit;

 }

public class cMatiere

{
 public MatierePremiere__c mat {get; set;}
 public Boolean selected {get; set;}

 public cMatiere(MatierePremiere__c c) {
            mat = c;
            selected = false;
        }
  }


}

but if I completed my registration with the button save, an error is produced as
"System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields have not been filled: [Produit]: [Produit]"
can someone help me
 
James LoghryJames Loghry
Simple enough fix.  It sounds like your custom object has a field called "Produit", or perhaps it's the name field of the custom object, because I don't see a __c appended.  You'll need to specify this field before you insert the produit record.  Either have the user enter the Produit field in Visualforce, or specify the field in your constructor, based on your requirements, and you should be good to go.