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
Mick ManMick Man 

problem with SOQL

Please! can someone help me to solve my little problem because I can not do the same,
public class OdtDetailController2 
    {

       
         public list<wrapperclass> Jonintwrapper{get;set;}
         public list<wrapperclass> Odtwrapper{get;set;}
         public Ordre_Travail__c OrdreTravail {get; set;}
        Public Double TotalNombre=0;
        Public Double TotalQuantite=0;
       Public Double Total;
       
         
         public OdtDetailController2(ApexPages.StandardController controller) 
             {
                 OrdreTravail = (Ordre_Travail__c)controller.getRecord();
                 OrdreTravail = [SELECT Id, Date__c, Produit__c, Nombre__c FROM Ordre_Travail__c];
                 list<JointMatProd__c> Jointure = [SELECT id, MatierePremiere__c, Quantite__c FROM JointMatProd__c WHERE produit__c = :OrdreTravail.Produit__c];
                 
                 //list<Ordre_Travail__c> OrdreTravail = [SELECT id, name, Date__c, Produit__c, Nombre__c FROM Ordre_Travail__c WHERE produit__c = :Ordre_Travail__c.Produit__c];
                 //list<JointMatProd__c> Jointure = [SELECT id, MatierePremiere__c, Quantite__c FROM JointMatProd__c WHERE produit__c = :Ordre_Travail__c.Produit__c];
                 Odtwrapper = new list<wrapperclass>();
                 /*for(Ordre_Travail__c Odt: OrdreTravail)
                     {
                        Odtwrapper.add(new wrapperclass(Odt));*/
                         if(OrdreTravail.Nombre__c!=Null){
                             //TotalNombre+=odt.Nombre__c;
                             Odtwrapper.add(new wrapperclass(OrdreTravail));
                             TotalNombre+=OrdreTravail.Nombre__c;

                        } 
                    // }
                     
                 Jonintwrapper = new list<wrapperclass>();
                 for(JointMatProd__c Joint: Jointure)
                     {
                        if(Joint.Quantite__c==Null)
                        Joint.Quantite__c=0; 
                        Jonintwrapper.add(new wrapperclass(Joint,Double.ValueOf(Joint.Quantite__c*TotalNombre)));
                        
                     }
                       

              }
    
              public class wrapperclass
                  {
                       public Ordre_Travail__c ODT{get;set;}
                       public JointMatProd__c JOINT{get;set;}
                       Public Decimal Total{get;set;}
                       
                       public wrapperclass(Ordre_Travail__c OrdreTravail) 
                           {
                                this.ODT = (OrdreTravail);
                           }
                           
                       public wrapperclass(JointMatProd__c Joint,Decimal Total) 
                           {
                                this.JOINT = (Joint);
                                this.Total=Total;
                           }
                  }
    }

here's my problem

"Visualforce ErrorAide sur cette page
System.QueryException: List has more than 1 row for assignment to SObject 
Class.OdtDetailController2.<init>: line 16, column 1"
 
Best Answer chosen by Mick Man
MithunPMithunP
Hi Micky,

Uncomment line number 16 and update like below
 
OrdreTravail = [SELECT Id, Date__c, Produit__c, Nombre__c FROM Ordre_Travail__c Where id =: OrdreTravail.Id];



Best Regards,
Mithun.

All Answers

Suneel#8Suneel#8
Your SOQL at line 16 is fetching more than one row and you are trying to save them in one object.Hence the error.Either you should use a collection to store the values or modify SOQL such that it retrieves only one row.

OrdreTravail = [SELECT Id, Date__c, Produit__c, Nombre__c FROMOrdre_Travail__c];

Please mark this question as solved if your issue is fixed
Mick ManMick Man
thank you for your answer Sunnel#8, I would like to modify SOQL she retrieves à single line, but I is not know what to do, since I am à beginner, thank you for your understanding
MithunPMithunP
Hi Micky,

As per your code, i think you no need to add line number 16, since you are using standard controller and getting the same record in constructor. just comment line number 16 and check the functionality.

comment line no 16
//OrdreTravail = [SELECT Id, Date__c, Produit__c, Nombre__c FROM Ordre_Travail__c];

Best Regards,
Mithun
 
Mick ManMick Man
Mithun thank you but there is still a problem
Visualforce Error
System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Ordre_Travail__c.Produit__c 
Class.OdtDetailController2.<init>: line 17, column 1
 
MithunPMithunP
Hi Micky,

Uncomment line number 16 and update like below
 
OrdreTravail = [SELECT Id, Date__c, Produit__c, Nombre__c FROM Ordre_Travail__c Where id =: OrdreTravail.Id];



Best Regards,
Mithun.
This was selected as the best answer
Mick ManMick Man
thank you again, but there are still a mistake to

Visualforce Error
System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Ordre_Travail__c.Name 

 
MithunPMithunP
Hi Micky,

You need to add necessary fields in your query, just add name field in your query.

Here is updated query.
OrdreTravail = [SELECT Id, name,Date__c, Produit__c, Nombre__c FROM Ordre_Travail__c Where id =: OrdreTravail.Id];

Best Regards,
Mithun.
Mick ManMick Man
thank you very much, my problem is resolved
MithunPMithunP
I'm glad I helped

Best Regards,
Mithun.
Mick ManMick Man
and if I would like to create a test class for this class, what should I do