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
Vianney Bancillon 8Vianney Bancillon 8 

Import QuoteLine items into Quote from csv file

Hi everyone, I try to write a code to import QuoteLineItems into Quote from csv file.
My code generate no error but it does not work: my quote is still empty : no QuoteLine appear.
Could you please help me to solve it?
Here is the code of my VF page:
<apex:page standardController="Quote" extensions="importQuoteLineItemFromCSVController">
    
       <apex:form >
           <apex:pagemessages />
            <apex:pageBlock title="QuoteLineItems" id="QuoteLineItems">
                <apex:pageBlockSection >
                    <p>
                        Hi {!$User.FirstName}  You're workin on {!Quote.Name}
                    </p>                
                </apex:pageBlockSection>
                <apex:pageBlockSection columns="2">
                    <apex:inputFile value="{!csvFileBody}" filename="{!csvAsString}"/>&nbsp;
                    <apex:commandButton value="Import" action="{!importCSVFile}"/>
                </apex:pageBlockSection>
                
                <apex:pageBlockTable value="{!qlilist}" var="qli">
                    <apex:column value="{!qli.Quantity}"/>
                    <apex:column value="{!qli.Code_produit__c}"/>
                    <apex:column value="{!qli.id}"/>                
                </apex:pageBlockTable> 
                
           </apex:pageBlock>
        </apex:form>   
</apex:page>
and my controller...
public class importQuoteLineItemFromCSVController {
public Blob csvFileBody{get;set;}
public string csvAsString{get;set;}
public String[] csvFileLines{get;set;}
public Quote q {get;set;}

public PricebookEntry pbe;

public List<QuoteLineItem> qlilist{get;set;}
public importQuoteLineItemFromCSVController(ApexPages.StandardController stdController){
    this.q=(Quote)stdController.getRecord();
    csvFileLines = new String[]{};
    qlilist = new List<QuoteLineItem>(); 
  }  
  public void importCSVFile(){       
       try
       {
           csvAsString = csvFileBody.toString();
           csvFileLines = csvAsString.split('\n'); 
            
           for(Integer i=1;i<csvFileLines.size();i++)
           {
               QuoteLineItem qli = new QuoteLineItem() ;
               string[] csvRecordData = csvFileLines[i].split(',');
               qli.Product2.ProductCode = csvRecordData[0];
               qli.Quantity = decimal.valueOf(csvRecordData[1]);
               qli.QuoteId = q.Id;
               qli.UnitPrice = pbe.UnitPrice;
               qlilist.add(qli);        
              
           }
           INSERT qlilist;                  	
        }
      
        catch (Exception e)
        {
            ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importin data Please make sure input csv file is correct');
            ApexPages.addMessage(errorMessage);
        }  
  }
}

Many thanks...
Vianney