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
shakila Gshakila G 

My apex class doesnt allow the data to insert and also it dosnot giving any error in VF page?

Hi All,
I have written the apex class to insert a record in Salesforce before inserting record itself it's getting redirect from the page.
Kindly let me know how to overcome from the issue

My VF Page:
<apex:page Controller="Rawmaterial_Cost_Accounting"  tabStyle="Vendor__c" sidebar="false">
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css"></link>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
<script>
//purchd RM
var prdrmtags = [];
<apex:repeat value="{!rmTagLst}" var="rm">
    prdrmtags.push('{!rm}');
</apex:repeat>
<apex:repeat value="{!rmTagLst2}" var="rm2">
    prdrmtags.push('{!rm2}');
</apex:repeat>



//vendor 
var vendortags = []; 
<apex:repeat value="{!vendorTagLst}" var="vendor">
    vendortags.push('{!vendor}');
</apex:repeat>
<apex:repeat value="{!vendorTagLst2}" var="vendor2">
    vendortags.push('{!vendor2}');
</apex:repeat>

//received by(employee)
var recvBytags = [];
<apex:repeat value="{!receivedByTagLst}" var="recvBy">
    recvBytags.push('{!recvBy}');
</apex:repeat>
<apex:repeat value="{!receivedByTagLst2}" var="recvBy2">
    recvBytags.push('{!recvBy2}');
</apex:repeat>

$( function() {
    $( ".prdrmtags" ).autocomplete({
        source: prdrmtags
    });
     $( ".vendortags" ).autocomplete({
        source: vendortags
    });
         $( ".recvBytags" ).autocomplete({
        source: recvBytags
    });
    
 $(".prdrmtags").on( "autocompletechange", function(event,ui) {
   // post value to console for validation
     setRmUnit();
     //  alert('changed');
 });
} );
</script>
   <apex:form >
        <apex:pageBlock title="Purchasd Raw Material Informations" rendered="{!show1}" >
         <apex:pageblockButtons location="Bottom" >
        <apex:inputtext value="{!addCount}" />
           <apex:commandButton value="Add Row" action="{!addPrdRM}"  rendered="{!show1}" ID="theButton"/>
          </apex:pageblockButtons>  
        
    <apex:pageBlockTable value="{!iPrdRmLst}" var="iPrdRm" id="iPrdRmTb">
        <apex:column >
            <apex:facet name="header">Raw Material</apex:facet>
            <apex:inputField value="{!iPrdRm.Raw_Material__c}" style="width: 250px;" styleClass="prdrmtags" required="true">
               
            </apex:inputField>
        </apex:column> 
        
        <apex:column >
            <apex:facet name="header">Vendor Name</apex:facet>
            <apex:inputField value="{!iPrdRm.Vendor__c}" style="width: 250px;" styleClass="vendortags" required="true">
               
            </apex:inputField>
        </apex:column> 
        
        <apex:column >
            <apex:facet name="header">Price</apex:facet>
            <apex:inputField value="{!iPrdRm.Price_Unit__c}" style="width: 250px;" required="true">
               
            </apex:inputField>
        </apex:column> 
        
        
       <!--- <apex:column >
            <apex:facet name="header">Purchase Unit</apex:facet>
            <apex:outputField value="{!iPrdRm.Unit__c}" style="width: 250px;"/>
        </apex:column>  -->
       
         
       
    </apex:pageBlockTable>
    
    <apex:pageblockButtons location="Bottom" >
                  <apex:commandButton value="Save" action="{!saveEntity}" />
          </apex:pageblockButtons>            
     
              
            
   </apex:pageBlock> 
   </apex:form>
</apex:page>

Apex class:
public class Rawmaterial_Cost_Accounting{
    //list 1
    List<String> rmTagLst=new List<String>();
    List<String> vendorTagLst=new List<String>();
    List<String>  receivedByTagLst=new List<String>();     
     public static String  purchdOrderNm{get; set;}     
     boolean show = true;  boolean show1 = true; boolean Iflag= False;
    //list 2
    List<String> rmTagLst2=new List<String>();
    List<String> vendorTagLst2=new List<String>();    
    List<String>  receivedByTagLst2=new List<String>();
    
    Map<String,Id> iRmNmToRmId=new Map<String,Id>();
    Map<String,String> iRmNmToRmUnit=new Map<String,String>();
    Map<String,Id> iRmIdToRmInvId=new Map<String,Id>();
     Map<String,Id> Ipolist=new Map<String,Id>();
    Map<String,Id> iVndrNmToVndrId=new Map<String,Id>();
    Map<String,Id> iRecvByNmToEmpId=new Map<String,Id>();
    Public List<RawMaterial_Supplied__c > iPrdRmLst =New  List<RawMaterial_Supplied__c >();
      List<RawMaterial_Supplied__c > iPrdRmLst1=new List<RawMaterial_Supplied__c >();
     public static Integer addCount {get; set;}
     
    RawMaterial_Supplied__c  iPrdRm=new RawMaterial_Supplied__c ();
    Object_Helper__c iObj=new Object_Helper__c();
    
    public Object_Helper__c getIObj(){
        return iObj;
    }
    public void setIObj(Object_Helper__c iObj){
        this.iObj=iObj;
    }
    public List<RawMaterial_Supplied__c > getIPrdRmLst(){
        return iPrdRmLst;
    }
    public void setIPrdRmLst(List<RawMaterial_Supplied__c > iPrdRmLst){
        this.iPrdRmLst=iPrdRmLst;
    }
    public RawMaterial_Supplied__c  getIPrdRm(){
        return iPrdRm;
    }
    public void setIPrdRm(RawMaterial_Supplied__c  iPrdRm){
        this.iPrdRm=iPrdRm;
    }
    
    //list 1
    public List<String> getRmTagLst(){
        return rmTagLst;
    }
    public void setRmTagLst(List<String> rmTagLst){
        this.rmTagLst=rmTagLst;
    }    
    public boolean getshow1()
    {
         return show1;
    }    
        
    public List<String> getVendorTagLst(){
        return vendorTagLst;
    }
    public void setVendorTagLst(List<String> vendorTagLst){
        this.vendorTagLst=vendorTagLst;
    }
    
     
    
    public List<String> getReceivedByTagLst(){
        return receivedByTagLst;
    }
    public void setReceivedByTagLst(List<String> receivedByTagLst){
        this.receivedByTagLst=receivedByTagLst;
    }
    
  
    
    
    //list 2
    public List<String> getRmTagLst2(){
        return rmTagLst2;
    }
    public void setRmTagLst2(List<String> rmTagLst2){
        this.rmTagLst2=rmTagLst2;
    }    
    
    
    public List<String> getVendorTagLst2(){
        return vendorTagLst2;
    }
    public void setVendorTagLst2(List<String> vendorTagLst2){
        this.vendorTagLst2=vendorTagLst2;
    }
    
    
    public List<String> getReceivedByTagLst2(){
        return receivedByTagLst2;
    }
    public void setReceivedByTagLst2(List<String> receivedByTagLst2){
        this.receivedByTagLst2=receivedByTagLst2;
    }
    
   
    
    Integer counter=0;    
    
    public  Rawmaterial_Cost_Accounting(){
        
        
        //rw material autocomplete  
        List<RawMaterial__c> iRmLst=[SELECT id, Name,selected__C FROM RawMaterial__c];
        
        for(RawMaterial__c iRM :iRmLst){
            
            iRmNmToRMId.put(iRM.Name.trim(), iRM.Id);
          
            
            //to diplay in javascript tag
            if(iRM.Name.contains('\'')){
                iRM.Name= iRM.Name.replace('\'', '\\\'');
                
            }
            ++counter;
            if(counter<1000){  
                rmTagLst.add(iRM.Name);
            }else{
                rmTagLst2.add(iRM.Name); 
            }
        }        
        //re initialize counter
        counter=0;        
        //vendor autocomplete
        List<Vendor__c> iVendorLst=[SELECT id, Name FROM Vendor__c];
        
        for(Vendor__c iVendor :iVendorLst){
            
            iVndrNmToVndrId.put(iVendor.Name, iVendor.Id);
            
            //to diplay in javascript tag
            if(iVendor.Name.contains('\'')){
                iVendor.Name= iVendor.Name.replace('\'', '\\\'');
                
            }
            ++counter;
            if(counter<1000){  
                vendorTagLst.add(iVendor.Name);
            }else{
                vendorTagLst2.add(iVendor.Name);
            }
        }      
        
         
          
        //re initialize counter
        counter=0;
        
        //received by autocomplete
        List<Employee__c> iEmployeeLst=[SELECT id, Name FROM Employee__c];
        
        for(Employee__c iEmp :iEmployeeLst){
            
            iRecvByNmToEmpId.put(iEmp.Name, iEmp.Id);
            
            ++counter;
            if(counter<1000){  
                receivedByTagLst.add(iEmp.Name);
            }else{
                receivedByTagLst2.add(iEmp.Name); 
            }
        }                
        system.debug('iRmNmToRMId'+iRmNmToRMId);
    }
    
    
    
    public PageReference addPrdRM(){  
    If(addCount!=Null){    
       for (Integer idx=0; idx<addCount; idx++) { 
        RawMaterial_Supplied__c  iPrdRm=new RawMaterial_Supplied__c ();
         iPrdRmLst.add(iPrdRm);        
    }
    
    }
       if(iPrdRmLst.size()<=0){            
            show1 = True;           
        }   
    return null;
    }
    
    public PageReference saveEntity(){
        try{ 
            List<Id> iRmIdLst=new List<String>();
            
            for(RawMaterial_Supplied__c   iPrdRm:iPrdRmLst){
                iPrdRm.Period_From__c=system.Today();
                iPrdRm.Price_Unit__c= iPrdRm.Price_Unit__c;
                iPrdRm.Vendor__c=iVndrNmToVndrId.get(iPrdRm.Vendor__c); 
                iPrdRm.Raw_Material__c=iRmNmToRMId.get(iPrdRm.Raw_Material__C);
                iPrdRmLst1.add(iPrdRm);
            }
            if(iPrdRmLst1.size()>0){
                insert iPrdRmLst1;
                Iflag=True;
                   
            }          
              
            else{
               // sendEmail(iPrdRmLst);
            }            
            if(Iflag==True){
            
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info,'Purchased raw materials succesfully added')); 
                 PageReference exRM = new PageReference('/apex/V2_Raw_Material_Received');               
                exRM.setRedirect(true);
                return exRM;  
                
               
                                                
            }else{
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info,'Purchased raw materials succesfully added')); 
            }   
            
        }catch(Exception e){
            system.debug('Exception Line' +e.getLineNumber());  
            system.debug('Exception Cause' +e.getCause());  
            system.debug('Exception' +e.getStackTraceString()); 
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info,'Oops: something went wrong. Please contact admin.'));
        }        
        return null;
    }
     
    }1
 
SandhyaSandhya (Salesforce Developers) 
Hi,

Try to put system.debugs in below if statements and see if you are getting the values in list iPrdRmLst1
 
if(iPrdRmLst1.size()>0){
                insert iPrdRmLst1;
                Iflag=True;
                   
            }

Best Regards,
Sandhya