I am trying to upload a csv, and it says BLOB is not a valid UTF-8 string.
public class ProductImportController{
public Blob csvFileBody { get; set; }
public String csvFileName { get; set; }
public Boolean showResults { get; set; }
public Boolean showImport { get; set; }
public Boolean isUploading { get; set; }
public List<Product2> prdctList { get; set; }
public List<PricebookEntry> pbeListStandard { get; set; }
public List<PricebookEntry> pbeListCustom { get; set; }
public ProductImportController(){
//Show/hide sections
showResults = false;
showImport = true;
isUploading = false;
}
public void upload(){
if(isUploading){ return; }
isUploading = true;
//Show/hide sections
showResults = true;
showImport = false;
try{
parseCsvInsertProductsPricebooks();
}catch(Exception e){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage() ));
//Show/hide sections
showResults = false;
showImport = true;
isUploading = false;
if(Test.isRunningTest()){
throw e;
}else{
return;
}
}
//Finished
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Product import completed.'));
isUploading = false;
}
public void parseCsvInsertProductsPricebooks(){
if(csvFileBody == null){
throw new ProductImportException('No CSV found.');
}
//Convert from blob to string
String csvString = csvFileBody.toString();
csvFileBody = null;
csvFileName = null;
if(String.isBlank(csvString)){
throw new ProductImportException('Empty CSV found.');
}
//Parse CSV into separate fields
List<List<String>> allFields = parseCSV(csvString);
for(Integer i = 0; i < allFields.size(); i++){
for(Integer j = 0; j < allFields.get(i).size(); j++){
System.debug('* ** *** ' + i + '-' + j + ' : "' + allFields.get(i).get(j) + '"');
}
}
if(allFields == null || allFields.isEmpty()){
throw new ProductImportException('Empty CSV found.');
}
//Use first line as header
List<String> headerFields = allFields.remove(0);
List<HeaderWrapper> headerList = parseHeaders(headerFields);
List<LineWrapper> lineList = new List<LineWrapper>();
//Parse remaining lines
if(allFields == null || allFields.isEmpty()){
throw new ProductImportException('No rows found.');
}else{
for(List<String> line : allFields){
lineList.add(new LineWrapper(line,headerList));
}
}
//Get all products
prdctList = new List<Product2>();
for(LineWrapper line : lineList){
prdctList.add(line.prdct);
}
//Insert products
try{
insert prdctList;
System.debug(prdctList);
}catch(Exception e){
throw new ProductImportException('Could not insert products. ' + e.getMessage() ,e);
}
//Insert standard pricebook entries
pbeListStandard = new List<PricebookEntry>();
for(LineWrapper line : lineList){
List<PricebookEntry> l = line.getStandard();
if(l != null){
pbeListStandard.addAll(l);
}
}
try{
if(!pbeListStandard.isEmpty()){
System.debug('* ** *** inserting standard pbe ' + pbeListStandard);
insert pbeListStandard;
System.debug(pbeListStandard);
}
}catch(Exception e){
throw new ProductImportException('Could not insert pricebook entries. ' + e.getMessage() ,e);
}
//Insert custom pricebook entries
pbeListCustom = new List<PricebookEntry>();
for(LineWrapper line : lineList){
List<PricebookEntry> l = line.getCustom();
if(l != null && !l.isEmpty()){
pbeListCustom.addAll(l);
}
}
try{
if(!pbeListCustom.isEmpty()){
System.debug('* ** *** inserting custom pbe ' + pbeListCustom);
insert pbeListCustom;
System.debug(pbeListCustom);
}
}catch(Exception e){
throw new ProductImportException('Could not insert pricebook entries. ' + e.getMessage(),e);
}
}
}