You need to sign in to do that
Don't have an account?
Saad Ahmad 27
Import Quote Line Item CSV using Apex & VisualForce Page
My requirement is to be able to import quote line items into a quote using a CSV. I have written the following code but I'm unable to get it working. The code shows no errors but when I try to import a CSV, it doesn work. Thank you in advance for any guidance you can provide:
APEX Code:
Visualforce Page Code
APEX Code:
public class importDataFromCSVController { public Blob csvFileBody{get;set;} public string csvAsString{get;set;} public String[] csvFileLines{get;set;} public List<QuoteLineItem> quoteLineItemList{get;set;} public List<Quote> quoteNumberList{get;set;} public importDataFromCSVController(){ csvFileLines = new String[]{}; quoteLineItemList = New List<QuoteLineItem>(); } public void importCSVFile(){ List<String> allProductCodes = new List<String>(); List<String> allQuoteNumbers = new List<String>(); Map<String, Id> quoteNumber = new Map<String, Id>(); //Query for Products that have matching Product Code Map<String, Id> productCode = new Map<String, Id>(); for (Product2 p : [SELECT Id, Name from Product2 where Name in :allProductCodes LIMIT 1]) { productCode.put(p.Name,p.Id); } //Query for Quotes that have matching Quote Number Map<String, Id> quoteNum = new Map<String, Id>(); for (Quote q : [SELECT Id, QuoteNumber from Quote where QuoteNumber in :allQuoteNumbers LIMIT 1]) { quoteNum.put(q.QuoteNumber, q.Id); } // Set the Product Id for each Product Code for(Integer j=0;j<quoteLineItemList.size();){ QuoteLineItem qli1; Id Productid = productCode.get(allProductCodes[j]); if (Productid != null) { qli1.Product2.Id = Productid; } // Set the Quote Id for each Quote for (Integer z=0;z<quoteNumberList.size();){ Quote q1; Id quoteNumberId = quoteNum.get(allQuoteNumbers[z]); if (quoteNumberId != null) { q1.Id = quoteNumberId; } } } try{ csvAsString = csvFileBody.toString(); csvFileLines = csvAsString.split('\n'); for(Integer i=1;i<csvFileLines.size();i++){ QuoteLineItem lineItem = new QuoteLineItem() ; string[] csvRecordData = csvFileLines[i].split(','); allQuoteNumbers.add(csvRecordData[0]); allProductCodes.add(csvRecordData[1]); lineItem.Quantity = decimal.valueof(csvRecordData[2]); lineItem.UnitPrice = decimal.valueof(csvRecordData[3]); lineItem.Global_Discount__c = decimal.valueof(csvRecordData[4]); quoteLineItemList.add(lineItem); } insert quoteLineItemList; } catch (Exception e) { ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please try importing the file again'); ApexPages.addMessage(errorMessage); } } }
Visualforce Page Code
<apex:page controller="importDataFromCSVController"> <apex:form > <apex:pagemessages /> <apex:pageBlock > <apex:pageBlockSection columns="5"> <apex:inputFile value="{!csvFileBody}" filename="{!csvAsString}"/> <apex:commandButton value="Import Quote Line Items" action="{!importCSVFile}"/> </apex:pageBlockSection> </apex:pageBlock> <apex:pageBlock > <apex:pageblocktable value="{!quoteLineItemList}" var="acc"> <apex:column value="{!acc.Quote}" /> <apex:column value="{!acc.Product2}" /> <apex:column value="{!acc.Quantity}" /> <apex:column value="{!acc.UnitPrice}" /> <apex:column value="{!acc.Global_Discount__c}" /> </apex:pageblocktable> </apex:pageBlock> </apex:form> </apex:page>
Instead of converting using toString, try using below code:
All Answers
Instead of converting using toString, try using below code: