+ Start a Discussion
Mahesh Babu 187Mahesh Babu 187 

Constructor not defined error in wrapper class

Hi Team,

I am trying to display all search results in a wrapper class, but there's this error showing up every time I save the controller:

Error: Constructor not defined: [fetchserials.wrapperfetch].<Constructor>(Serial_Number__c)

The error points to this line of code:  serialNumbers.add(new wrapperfetch(a));

APEX CLASS

Public class fetchserials{
    
    Public string OrderID {get; set;}
    Public order OrderInstance {get; set;}
    Public List<OrderItem> OrderItemList {get; set;}
    Public Set<id> ProductIdSet {get; set;} //list to add product Ids
    Public list<Serial_Number__c> SerialList {get; set;}
    Public list<Serial_Number__c> SelectedSerialList {get; set;}
    Public Map<OrderItem,list<Serial_Number__c>> OrderVSerial{get; set;} // list to add OrderItem with their Serial Numbers
    Public List<wrapperfetch> fetchseriallist {get; set;}
    public Integer Counter;
    Public List<Order_Line_Item_Serials__c> insertordersn {get; set;} // list to add the selected orders
    public List<Serial_Number__c> serialNumbers;
    public string fullname{get;set;}
    public string finalSearchValue{get;set;}
   //ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.error,'Enter the List');
// ApexPages.addMessage(myMsg);
       Public fetchserials(){
        OrderItemList = new List<OrderItem>(); // intialize
        OrderVSerial = new Map<OrderItem,List<Serial_Number__c>>();
        fetchseriallist = new List<wrapperfetch>();
        insertordersn = new List<Order_Line_Item_Serials__c>();
        SelectedSerialList = new list<Serial_Number__c>();
        OrderID = ApexPages.currentPage().getParameters().get('Id'); //Getting the id of current order
        OrderInstance = [SELECT Id FROM Order where Id=:OrderID ];
        OrderItemList=[SELECT Id,OrderId,product2Id,PricebookEntry.Product2.Name, Quantity FROM OrderItem where OrderId =:OrderID];
        loadSerials();
    }
    //adding page reference of the current page
    public PageReference step1() {
        loadSerials();
      /*  if(SerialList==null){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 
                                                           'Please select the serial number'));
        } */
        return Page.fetchserials;
    }
    
    public PageReference cancel() {
        PageReference c=new PageReference('/'+OrderID);
        return c;
    }   
    
    Public void loadSerials(){
        ProductIdSet = new set<Id>();
        For(OrderItem items :OrderItemList){
            ProductIdSet.add(items.Product2Id);// order items products add into productidset 
        }
        SerialList = [SELECT Id,Serial_Number__c,Product_Name__c ,Name,Product__c,Status__c
                      FROM Serial_Number__c where Status__c=FALSE And Product__c In:ProductIdSet limit 200 ];
        
        
        /*if(SerialList == NULL)
{
SerialList.addError('There are no values in the Serials List.'); 
}*/
        // add error exception handling
        system.debug('Serialx' +SerialList );
        For(OrderItem item :OrderItemList){
            counter = 0;
            For(Serial_Number__c sn :SerialList ){
                If( sn.Product__c == item.Product2Id && counter < item.Quantity)
                {   SelectedSerialList = new list<Serial_Number__c>();
                    fetchseriallist.add(new wrapperfetch(sn,item));
                    SelectedSerialList.add(sn);
                    counter = counter +1; }
                else{continue;}
            }
            OrderVSerial.put(item,SelectedSerialList);
            /*=====
            try{
                if(SerialList == NULL ){ ApexPages.addMessages(e); }

catch (DMLException e){
    ApexPages.addMessages(myMsg);
    // Code for sending Email
}
            ============*/
        }
    }
    
    public pagereference Search()
    {
        String finalSearchValue = '%' + fullname + '%';
        serialNumbers = [Select Id, Serial_Number__c, Product_Name__c, Status__c FROM Serial_Number__c
                    where Name like :finalSearchValue];
        fetchseriallist = new list<wrapperfetch>();  
        for(Serial_Number__c a: serialNumbers)
        {
            serialNumbers.add(new wrapperfetch(a));
        }
       return null;
   }
    
    Public class wrapperfetch{
        Public Boolean selected {get; set;}
        Public Serial_Number__c sno{get; set;}
        Public string prodname {get; set;}
        Public OrderItem Oitem{get; set;}
        Public string serialno {get; set;}
        Public Boolean status {get; set;}
        Public Id stid {get; set;}
        Public Id opid {get; set;}
        
        Public wrapperfetch(Serial_Number__c sn,OrderItem Oi){
            //this.selected=check;
            this.Oitem= Oi;
            this.opid =Oi.Id;
            this.prodname =sn.Product_Name__c;
            this.sno=sn;
            this.serialno = sn.Serial_Number__c;
            this.status=sn.status__c;
            this.stid=sn.id;
            
            }
    }
    
    Public PageReference AddSerailNumbertoOrder(){
        
        for (wrapperfetch st: fetchseriallist ){
            
            If (st.selected == true ){
                Order_Line_Item_Serials__c ordersn= new Order_Line_Item_Serials__c();
                //Order_Line_Item_Serials__c make it to true
                //make a new Order_Line_Item_Serials__c list and make it to true.
                // put that into   ordersn.Serial_Number__c=st.stid;  a new list 
                ordersn.Serial_Number__c=st.stid;
                ordersn.Order_Product__c=st.opid;
                insertordersn.add(ordersn);
                
                Serial_Number__c  serials = new Serial_Number__c();
                serials.id = st.stid;
                serials.Status__c = TRUE;
                SelectedSerialList.add(serials);
            }
            if(SelectedSerialList == NULL){
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 
                                                           'Please select the serial number'));
            }
        }
        insert  insertordersn;
        update SelectedSerialList;
        PageReference orderpage = new PageReference('/'+OrderID);
        orderpage.setRedirect(true);
        orderpage.getParameters().put('myId', OrderID);
        return orderpage;
       
    }
}
-----------------------------------

VISUALFORCE Page

<apex:page controller="fetchserials" lightningstylesheets="true" readonly="true">
    <script>
  function confirmCancel() {
      var isCancel = confirm("Are you sure you wish to cancel?");
      if (isCancel) return true;
     return false;
  }  
  </script>
<Apex:form >
<apex:pageBlock >
<apex:pageMessages id="showmsg"></apex:pageMessages>
    <apex:pageBlock>
        <apex:inputText value="{!fullname}" html-placeholder="Search" />
        <apex:commandButton value="search" action="{!Search}" />
    </apex:pageBlock>
  <apex:pageBlockTable value="{!fetchseriallist }" var="u"  style="width:100%">
    
  <apex:column headerValue="Select" > 
                        <Apex:inputcheckbox value="{!u.Selected}"  /> 
                    </apex:column>  
                    <apex:column headerValue="Serial Number" > 
                        <Apex:outputText value="{!u.serialno }"   /> 
                    </apex:column> 
                   <apex:column headerValue="Product Name" > 
                        <Apex:outputText value="{!u.prodname }"  /> 
                    </apex:column> 
                    <apex:column headerValue="Status" > 
                       <Apex:outputText value="{!u.status}"   />
                    </apex:column> 
  </apex:pageBlockTable>                  
  <apex:commandButton value="Add to Order" action="{!AddSerailNumbertoOrder}" style="margin-top: -2pt;" rerender="showmsg"/>
  <apex:commandButton action="{!cancel}" value="Cancel" onclick="return confirmCancel()" immediate="true" id="cancel"/>

</apex:pageBlock>
  </Apex:form>
</apex:page>

Please help me to solve this issue.
Best Answer chosen by Mahesh Babu 187
Mahesh Babu 187Mahesh Babu 187
Hi Anudeep,
I solved the issue.

for(Serial_Number__c s : [Select Id, Serial_Number__c, Product_Name__c, Status__c, Bin_Number__c, Location__c FROM Serial_Number__c
                                  where Name like :finalSearchValue])

I was searching in where clause Name instead of Serial_Number__c. 

The correct code for users for further reference:

APEX CODE

Public class fetchserials{
    
    Public string OrderID {get; set;}
    Public order OrderInstance {get; set;}
    Public List<OrderItem> OrderItemList {get; set;}
    Public Set<id> ProductIdSet {get; set;} //list to add product Ids
    Public list<Serial_Number__c> SerialList {get; set;}
    Public list<Serial_Number__c> SelectedSerialList {get; set;}
    Public Map<OrderItem,list<Serial_Number__c>> OrderVSerial{get; set;} // list to add OrderItem with their Serial Numbers
    Public List<wrapperfetch> fetchseriallist {get; set;}
    // public Integer Counter;
    Public List<Order_Line_Item_Serials__c> insertordersn {get; set;} // list to add the selected orders
    Public map<id,Serial_Number__c> smap = new map<id,Serial_Number__c>();
    public List<Serial_Number__c> serialNumbers;
    public string fullname{get;set;}
    public string finalSearchValue{get;set;}
    
    //ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.error,'Enter the List');
    // ApexPages.addMessage(myMsg);
    
    Public fetchserials(){
        OrderItemList = new List<OrderItem>(); // intialize
        OrderVSerial = new Map<OrderItem,List<Serial_Number__c>>();
        fetchseriallist = new List<wrapperfetch>();
        insertordersn = new List<Order_Line_Item_Serials__c>();
        SelectedSerialList = new list<Serial_Number__c>();
        SerialList = new list<Serial_Number__c>();
        OrderID = ApexPages.currentPage().getParameters().get('Id'); //Getting the id of current order
      OrderInstance = [SELECT Id FROM Order where Id=:OrderID ];
       OrderItemList=[SELECT Id,OrderId,product2Id,PricebookEntry.Product2.Name, Quantity FROM OrderItem where OrderId =:OrderID];
       loadSerials();
    }
    //adding page reference of the current page
  /*  public PageReference step1() {
        loadSerials();
          if(SerialList==null){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 
'Please select the serial number'));

        return Page.fetchserials;
    }*/
    
    public PageReference cancel() {
        PageReference c=new PageReference('/'+OrderID);
        return c;
    }   
    
    Public void loadSerials(){
        ProductIdSet = new set<Id>();
        For(OrderItem items :OrderItemList){
            ProductIdSet.add(items.Product2Id);// order items products add into productidset 
        }
        SerialList = [SELECT Id,Serial_Number__c,Product_Name__c ,Name,Product__c,Status__c,Bin_Number__c,Location__c
                      FROM Serial_Number__c where Status__c=FALSE And Product__c In:ProductIdSet limit 200 ];
        system.debug('Serialx' +SerialList );
        
        /*if(SerialList == NULL)
{
SerialList.addError('There are no values in the Serials List.'); 
}*/
        // add error exception handling
        system.debug('Serialx' +SerialList );
        For(OrderItem item :OrderItemList){
            //counter = 0;
            For(Serial_Number__c sn :SerialList ){
                If( sn.Product__c == item.Product2Id) // && counter < item.Quantity)
                {   SelectedSerialList = new list<Serial_Number__c>();
                 //     mahesh   fetchseriallist.add(new wrapperfetch(sn,item));
                 fetchseriallist.add(new wrapperfetch(sn));
                 SelectedSerialList.add(sn);
                 //counter = counter +1; 
                }
                else{continue;}
            }
            OrderVSerial.put(item,SelectedSerialList);
            /*=====
try{
if(SerialList == NULL ){ ApexPages.addMessages(e); }

catch (DMLException e){
ApexPages.addMessages(myMsg);
// Code for sending Email
}
============*/
        }
    }
    
    public void search(){
        finalSearchValue = '%' + fullname + '%';
       System.debug(finalSearchValue);
        fetchseriallist = new list<wrapperfetch>();
        List<Serial_Number__c> serialList =  [Select Id, Serial_Number__c, Product_Name__c, Status__c, Bin_Number__c, Location__c FROM Serial_Number__c
                                  where Serial_Number__c like :finalSearchValue];
            System.debug(serialList.size());
        for(Serial_Number__c s : serialList){
                                      System.debug(s);
                                      fetchseriallist.add(new wrapperfetch(s));    
                                      System.debug(s);
                                  }
        
    }
    
    Public class wrapperfetch{
        Public Boolean selected {get; set;}
        Public Serial_Number__c sno{get; set;}
        Public string prodname {get; set;}
        Public OrderItem Oitem{get; set;}
        Public string serialno {get; set;}
        Public Boolean status {get; set;}
        Public String locatn{get;set;}
        Public String binNo{get;set;}
        Public Id stid {get; set;}
        Public Id opid {get; set;}
        
        //    Public wrapperfetch(Serial_Number__c sn,OrderItem Oi){
        Public wrapperfetch(Serial_Number__c sn){  
            //this.selected=check;
            //    this.Oitem= Oi;
            //    this.opid =Oi.Id;
            this.prodname =sn.Product_Name__c;
            this.sno=sn;
            this.locatn=sn.Location__c;
            this.binNo=sn.Bin_Number__c;
            this.serialno = sn.Serial_Number__c;
            this.status=sn.status__c;
            this.stid=sn.id;
            
        }
    }
    
    Public PageReference AddSerailNumbertoOrder(){
        
        for (wrapperfetch st: fetchseriallist ){
            
            If (st.selected == true ){
                Order_Line_Item_Serials__c ordersn= new Order_Line_Item_Serials__c();
                //Order_Line_Item_Serials__c make it to true
                //make a new Order_Line_Item_Serials__c list and make it to true.
                // put that into   ordersn.Serial_Number__c=st.stid;  a new list 
                ordersn.Serial_Number__c=st.stid;
                ordersn.Order_Product__c=st.opid;
                insertordersn.add(ordersn);
                
                Serial_Number__c  serials = new Serial_Number__c();
                serials.id = st.stid;
                serials.Status__c = TRUE;
                serials.Location__c=st.locatn;
                serials.Bin_Number__c = st.binNo;
                SelectedSerialList.add(serials);
                smap.put(serials.id,serials );
                
            }
            
        }
        if (insertordersn.size() > 0){    
                insert insertordersn;    
              //  update smap.values();    
            }
        return Page.fetchserials;    
    }       
}

VISUALFORCE PAGE

<apex:page controller="fetchserials" lightningstylesheets="true">
    <script>
    function confirmCancel() {
        var isCancel = confirm("Are you sure you wish to cancel?");
        if (isCancel) return true;
        return false;
    }  
    </script>
    <Apex:form >
        <apex:pageBlock >
            
            <apex:pageMessages id="showmsg"></apex:pageMessages>
            <apex:pageBlock >
                <apex:inputText value="{!fullname}" html-placeholder="Search" />
                <apex:commandButton value="search" action="{!Search}" />
            </apex:pageBlock>
            <apex:pageBlockTable value="{!fetchseriallist }" var="u"  style="width:100%">
                
                <apex:column headerValue="Select" > 
                    <Apex:inputcheckbox value="{!u.Selected}"  /> 
                </apex:column>  
                <apex:column headerValue="Serial Number" > 
                    <Apex:outputText value="{!u.serialno }"   /> 
                </apex:column> 
                <apex:column headerValue="Product Name" > 
                    <Apex:outputText value="{!u.prodname }"  /> 
                </apex:column> 
                <apex:column headerValue="Status" > 
                    <Apex:outputText value="{!u.status}"   />
                </apex:column> 
                <apex:column headerValue="Bin Number" > 
                    <Apex:outputText value="{!u.binNo }"  />
                </apex:column>
             <!--   <apex:column headerValue="Location" > 
                    <Apex:outputText value="{!u.locatn }"  /> 
                </apex:column>-->
            </apex:pageBlockTable>   
            <apex:pageBlockButtons location="both">
                <apex:commandButton value="Add to Order" action="{!AddSerailNumbertoOrder}" style="margin-top: -2pt;" rerender="showmsg"  />
                <apex:commandButton action="{!cancel}" value="Cancel" onclick="return confirmCancel()" immediate="true" id="cancel" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </Apex:form>
</apex:page>


Thanks,
Mahesh

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Mahesh, 

The constructor for the wrapperfetch class expects a parameter of type Serial_Number__c and OrderItem. However, you are passing only one parameter in your code serialNumbers.add(new wrapperfetch(a));. Add the correct parameters to fix your issue
Public class wrapperfetch{
        Public Boolean selected {get; set;}
        Public Serial_Number__c sno{get; set;}
        Public string prodname {get; set;}
        Public OrderItem Oitem{get; set;}
        Public string serialno {get; set;}
        Public Boolean status {get; set;}
        Public Id stid {get; set;}
        Public Id opid {get; set;}
        
        Public wrapperfetch(Serial_Number__c sn,OrderItem Oi){
            //this.selected=check;
            this.Oitem= Oi;
            this.opid =Oi.Id;
            this.prodname =sn.Product_Name__c;
            this.sno=sn;
            this.serialno = sn.Serial_Number__c;
            this.status=sn.status__c;
            this.stid=sn.id;
Let me know if this helps

Anudeep
Mahesh Babu 187Mahesh Babu 187
Hi Anudeep,

As you can see  the code :

 public pagereference Search()
    {
        String finalSearchValue = '%' + fullname + '%';
        serialNumbers = [Select Id, Serial_Number__c, Product_Name__c, Status__c FROM Serial_Number__c
                    where Name like :finalSearchValue];
        fetchseriallist = new list<wrapperfetch>();  
        for(Serial_Number__c a: serialNumbers)
        {
            serialNumbers.add(new wrapperfetch(a));
        }
       return null;
   }

I have returned a List of serialNumbers. But how to pass the other parameter in it. In my wrapper class, my other parameter is OrderItem. I don't understand how to pass the second parameter there.
Please help me solve this.

Thanks,
Mahesh
AnudeepAnudeep (Salesforce Developers) 
Hi Mahesh, 

You are calling wrapperfetch accurately at one point in your code (fetchseriallist.add(new wrapperfetch(sn,item))), you could probably code along the same lines
 
For(OrderItem item :OrderItemList){
            counter = 0;
            For(Serial_Number__c sn :SerialList ){
                If( sn.Product__c == item.Product2Id && counter < item.Quantity)
                {   SelectedSerialList = new list<Serial_Number__c>();
                    fetchseriallist.add(new wrapperfetch(sn,item));
                    SelectedSerialList.add(sn);
                    counter = counter +1; }
                else{continue;}
            }

​​​​​​​Anudeep
 
Mahesh Babu 187Mahesh Babu 187
Hi Anudeep,
I have modified my Apex code. It is working fine but the search functionality is returning empty list.

APEX CODE

Public class fetchserials{
    
    Public string OrderID {get; set;}
    Public order OrderInstance {get; set;}
    Public List<OrderItem> OrderItemList {get; set;}
    Public Set<id> ProductIdSet {get; set;} //list to add product Ids
    Public list<Serial_Number__c> SerialList {get; set;}
    Public list<Serial_Number__c> SelectedSerialList {get; set;}
    Public Map<OrderItem,list<Serial_Number__c>> OrderVSerial{get; set;} // list to add OrderItem with their Serial Numbers
    Public List<wrapperfetch> fetchseriallist {get; set;}
    // public Integer Counter;
    Public List<Order_Line_Item_Serials__c> insertordersn {get; set;} // list to add the selected orders
    Public map<id,Serial_Number__c> smap = new map<id,Serial_Number__c>();
    public List<Serial_Number__c> serialNumbers;
    public string fullname{get;set;}
    public string finalSearchValue{get;set;}
    //ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.error,'Enter the List');
    // ApexPages.addMessage(myMsg);
    
    Public fetchserials(){
        OrderItemList = new List<OrderItem>(); // intialize
        OrderVSerial = new Map<OrderItem,List<Serial_Number__c>>();
        fetchseriallist = new List<wrapperfetch>();
        insertordersn = new List<Order_Line_Item_Serials__c>();
        SelectedSerialList = new list<Serial_Number__c>();
        SerialList = new list<Serial_Number__c>();
        OrderID = ApexPages.currentPage().getParameters().get('Id'); //Getting the id of current order
      OrderInstance = [SELECT Id FROM Order where Id=:OrderID ];
       OrderItemList=[SELECT Id,OrderId,product2Id,PricebookEntry.Product2.Name, Quantity FROM OrderItem where OrderId =:OrderID];
       loadSerials();
    }
    //adding page reference of the current page
  /*  public PageReference step1() {
        loadSerials();
          if(SerialList==null){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 
'Please select the serial number'));

        return Page.fetchserials;
    }*/
    
    public PageReference cancel() {
        PageReference c=new PageReference('/'+OrderID);
        return c;
    }   
    
    Public void loadSerials(){
        ProductIdSet = new set<Id>();
        For(OrderItem items :OrderItemList){
            ProductIdSet.add(items.Product2Id);// order items products add into productidset 
        }
        SerialList = [SELECT Id,Serial_Number__c,Product_Name__c ,Name,Product__c,Status__c,Bin_Number__c,Location__c
                      FROM Serial_Number__c where Status__c=FALSE And Product__c In:ProductIdSet limit 200 ];
        system.debug('Serialx' +SerialList );
        
        /*if(SerialList == NULL)
{
SerialList.addError('There are no values in the Serials List.'); 
}*/
        // add error exception handling
        system.debug('Serialx' +SerialList );
        For(OrderItem item :OrderItemList){
            //counter = 0;
            For(Serial_Number__c sn :SerialList ){
                If( sn.Product__c == item.Product2Id) // && counter < item.Quantity)
                {   SelectedSerialList = new list<Serial_Number__c>();
                 //     mahesh   fetchseriallist.add(new wrapperfetch(sn,item));
                 fetchseriallist.add(new wrapperfetch(sn));
                 SelectedSerialList.add(sn);
                 //counter = counter +1; 
                }
                else{continue;}
            }
            OrderVSerial.put(item,SelectedSerialList);
            /*=====
try{
if(SerialList == NULL ){ ApexPages.addMessages(e); }

catch (DMLException e){
ApexPages.addMessages(myMsg);
// Code for sending Email
}
============*/
        }
    }
    
    public void search(){
        finalSearchValue = '%' + fullname + '%';
       System.debug(finalSearchValue);
        fetchseriallist = new list<wrapperfetch>();
        for(Serial_Number__c s : [Select Id, Serial_Number__c, Product_Name__c, Status__c, Bin_Number__c, Location__c FROM Serial_Number__c
                                  where Name like :finalSearchValue]){
                                      System.debug(s);
                                      fetchseriallist.add(new wrapperfetch(s));    
                                      System.debug(s);
                                  }
        
    }
    
    Public class wrapperfetch{
        Public Boolean selected {get; set;}
        Public Serial_Number__c sno{get; set;}
        Public string prodname {get; set;}
        Public OrderItem Oitem{get; set;}
        Public string serialno {get; set;}
        Public Boolean status {get; set;}
        Public String locatn{get;set;}
        Public String binNo{get;set;}
        Public Id stid {get; set;}
        Public Id opid {get; set;}
        
        //    Public wrapperfetch(Serial_Number__c sn,OrderItem Oi){
        Public wrapperfetch(Serial_Number__c sn){  
            //this.selected=check;
            //    this.Oitem= Oi;
            //    this.opid =Oi.Id;
            this.prodname =sn.Product_Name__c;
            this.sno=sn;
            this.locatn=sn.Location__c;
            this.binNo=sn.Bin_Number__c;
            this.serialno = sn.Serial_Number__c;
            this.status=sn.status__c;
            this.stid=sn.id;
            
        }
    }
    
    Public PageReference AddSerailNumbertoOrder(){
        
        for (wrapperfetch st: fetchseriallist ){
            
            If (st.selected == true ){
                Order_Line_Item_Serials__c ordersn= new Order_Line_Item_Serials__c();
                //Order_Line_Item_Serials__c make it to true
                //make a new Order_Line_Item_Serials__c list and make it to true.
                // put that into   ordersn.Serial_Number__c=st.stid;  a new list 
                ordersn.Serial_Number__c=st.stid;
                ordersn.Order_Product__c=st.opid;
                insertordersn.add(ordersn);
                
                Serial_Number__c  serials = new Serial_Number__c();
                serials.id = st.stid;
                serials.Status__c = TRUE;
                serials.Location__c=st.locatn;
                serials.Bin_Number__c = st.binNo;
                SelectedSerialList.add(serials);
                smap.put(serials.id,serials );
                
            }
            
        }
        if (insertordersn.size() > 0){    
                insert insertordersn;    
              //  update smap.values();    
            }
        return Page.fetchserials;    
    }  
}

Thanks,
Mahesh
Mahesh Babu 187Mahesh Babu 187
Hi Anudeep,
I solved the issue.

for(Serial_Number__c s : [Select Id, Serial_Number__c, Product_Name__c, Status__c, Bin_Number__c, Location__c FROM Serial_Number__c
                                  where Name like :finalSearchValue])

I was searching in where clause Name instead of Serial_Number__c. 

The correct code for users for further reference:

APEX CODE

Public class fetchserials{
    
    Public string OrderID {get; set;}
    Public order OrderInstance {get; set;}
    Public List<OrderItem> OrderItemList {get; set;}
    Public Set<id> ProductIdSet {get; set;} //list to add product Ids
    Public list<Serial_Number__c> SerialList {get; set;}
    Public list<Serial_Number__c> SelectedSerialList {get; set;}
    Public Map<OrderItem,list<Serial_Number__c>> OrderVSerial{get; set;} // list to add OrderItem with their Serial Numbers
    Public List<wrapperfetch> fetchseriallist {get; set;}
    // public Integer Counter;
    Public List<Order_Line_Item_Serials__c> insertordersn {get; set;} // list to add the selected orders
    Public map<id,Serial_Number__c> smap = new map<id,Serial_Number__c>();
    public List<Serial_Number__c> serialNumbers;
    public string fullname{get;set;}
    public string finalSearchValue{get;set;}
    
    //ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.error,'Enter the List');
    // ApexPages.addMessage(myMsg);
    
    Public fetchserials(){
        OrderItemList = new List<OrderItem>(); // intialize
        OrderVSerial = new Map<OrderItem,List<Serial_Number__c>>();
        fetchseriallist = new List<wrapperfetch>();
        insertordersn = new List<Order_Line_Item_Serials__c>();
        SelectedSerialList = new list<Serial_Number__c>();
        SerialList = new list<Serial_Number__c>();
        OrderID = ApexPages.currentPage().getParameters().get('Id'); //Getting the id of current order
      OrderInstance = [SELECT Id FROM Order where Id=:OrderID ];
       OrderItemList=[SELECT Id,OrderId,product2Id,PricebookEntry.Product2.Name, Quantity FROM OrderItem where OrderId =:OrderID];
       loadSerials();
    }
    //adding page reference of the current page
  /*  public PageReference step1() {
        loadSerials();
          if(SerialList==null){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 
'Please select the serial number'));

        return Page.fetchserials;
    }*/
    
    public PageReference cancel() {
        PageReference c=new PageReference('/'+OrderID);
        return c;
    }   
    
    Public void loadSerials(){
        ProductIdSet = new set<Id>();
        For(OrderItem items :OrderItemList){
            ProductIdSet.add(items.Product2Id);// order items products add into productidset 
        }
        SerialList = [SELECT Id,Serial_Number__c,Product_Name__c ,Name,Product__c,Status__c,Bin_Number__c,Location__c
                      FROM Serial_Number__c where Status__c=FALSE And Product__c In:ProductIdSet limit 200 ];
        system.debug('Serialx' +SerialList );
        
        /*if(SerialList == NULL)
{
SerialList.addError('There are no values in the Serials List.'); 
}*/
        // add error exception handling
        system.debug('Serialx' +SerialList );
        For(OrderItem item :OrderItemList){
            //counter = 0;
            For(Serial_Number__c sn :SerialList ){
                If( sn.Product__c == item.Product2Id) // && counter < item.Quantity)
                {   SelectedSerialList = new list<Serial_Number__c>();
                 //     mahesh   fetchseriallist.add(new wrapperfetch(sn,item));
                 fetchseriallist.add(new wrapperfetch(sn));
                 SelectedSerialList.add(sn);
                 //counter = counter +1; 
                }
                else{continue;}
            }
            OrderVSerial.put(item,SelectedSerialList);
            /*=====
try{
if(SerialList == NULL ){ ApexPages.addMessages(e); }

catch (DMLException e){
ApexPages.addMessages(myMsg);
// Code for sending Email
}
============*/
        }
    }
    
    public void search(){
        finalSearchValue = '%' + fullname + '%';
       System.debug(finalSearchValue);
        fetchseriallist = new list<wrapperfetch>();
        List<Serial_Number__c> serialList =  [Select Id, Serial_Number__c, Product_Name__c, Status__c, Bin_Number__c, Location__c FROM Serial_Number__c
                                  where Serial_Number__c like :finalSearchValue];
            System.debug(serialList.size());
        for(Serial_Number__c s : serialList){
                                      System.debug(s);
                                      fetchseriallist.add(new wrapperfetch(s));    
                                      System.debug(s);
                                  }
        
    }
    
    Public class wrapperfetch{
        Public Boolean selected {get; set;}
        Public Serial_Number__c sno{get; set;}
        Public string prodname {get; set;}
        Public OrderItem Oitem{get; set;}
        Public string serialno {get; set;}
        Public Boolean status {get; set;}
        Public String locatn{get;set;}
        Public String binNo{get;set;}
        Public Id stid {get; set;}
        Public Id opid {get; set;}
        
        //    Public wrapperfetch(Serial_Number__c sn,OrderItem Oi){
        Public wrapperfetch(Serial_Number__c sn){  
            //this.selected=check;
            //    this.Oitem= Oi;
            //    this.opid =Oi.Id;
            this.prodname =sn.Product_Name__c;
            this.sno=sn;
            this.locatn=sn.Location__c;
            this.binNo=sn.Bin_Number__c;
            this.serialno = sn.Serial_Number__c;
            this.status=sn.status__c;
            this.stid=sn.id;
            
        }
    }
    
    Public PageReference AddSerailNumbertoOrder(){
        
        for (wrapperfetch st: fetchseriallist ){
            
            If (st.selected == true ){
                Order_Line_Item_Serials__c ordersn= new Order_Line_Item_Serials__c();
                //Order_Line_Item_Serials__c make it to true
                //make a new Order_Line_Item_Serials__c list and make it to true.
                // put that into   ordersn.Serial_Number__c=st.stid;  a new list 
                ordersn.Serial_Number__c=st.stid;
                ordersn.Order_Product__c=st.opid;
                insertordersn.add(ordersn);
                
                Serial_Number__c  serials = new Serial_Number__c();
                serials.id = st.stid;
                serials.Status__c = TRUE;
                serials.Location__c=st.locatn;
                serials.Bin_Number__c = st.binNo;
                SelectedSerialList.add(serials);
                smap.put(serials.id,serials );
                
            }
            
        }
        if (insertordersn.size() > 0){    
                insert insertordersn;    
              //  update smap.values();    
            }
        return Page.fetchserials;    
    }       
}

VISUALFORCE PAGE

<apex:page controller="fetchserials" lightningstylesheets="true">
    <script>
    function confirmCancel() {
        var isCancel = confirm("Are you sure you wish to cancel?");
        if (isCancel) return true;
        return false;
    }  
    </script>
    <Apex:form >
        <apex:pageBlock >
            
            <apex:pageMessages id="showmsg"></apex:pageMessages>
            <apex:pageBlock >
                <apex:inputText value="{!fullname}" html-placeholder="Search" />
                <apex:commandButton value="search" action="{!Search}" />
            </apex:pageBlock>
            <apex:pageBlockTable value="{!fetchseriallist }" var="u"  style="width:100%">
                
                <apex:column headerValue="Select" > 
                    <Apex:inputcheckbox value="{!u.Selected}"  /> 
                </apex:column>  
                <apex:column headerValue="Serial Number" > 
                    <Apex:outputText value="{!u.serialno }"   /> 
                </apex:column> 
                <apex:column headerValue="Product Name" > 
                    <Apex:outputText value="{!u.prodname }"  /> 
                </apex:column> 
                <apex:column headerValue="Status" > 
                    <Apex:outputText value="{!u.status}"   />
                </apex:column> 
                <apex:column headerValue="Bin Number" > 
                    <Apex:outputText value="{!u.binNo }"  />
                </apex:column>
             <!--   <apex:column headerValue="Location" > 
                    <Apex:outputText value="{!u.locatn }"  /> 
                </apex:column>-->
            </apex:pageBlockTable>   
            <apex:pageBlockButtons location="both">
                <apex:commandButton value="Add to Order" action="{!AddSerailNumbertoOrder}" style="margin-top: -2pt;" rerender="showmsg"  />
                <apex:commandButton action="{!cancel}" value="Cancel" onclick="return confirmCancel()" immediate="true" id="cancel" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </Apex:form>
</apex:page>


Thanks,
Mahesh
This was selected as the best answer