+ Start a Discussion
Leticia Monteiro FreitasLeticia Monteiro Freitas 

Upload a records from pricebook with a visualforce page

Hi, 

I'm trying to create a  visualforce page wich uploads a csv file for create a Pricebook and a pricebook entries. There is a funcitonality to update my pricebook from account that was informed on csv file. 
But when I upload the file - I get a message: List out Inboud : 1 and I dont know what to do. Please, can anyone suggest me something?

My Code:
Apex Class
/*Author: Leticia Freitas v1 - Data de Criação:04/07/2018
Carregar uma lista de preços por cliente via csv */

public class importDataFromCSVController {

public Blob csvFileBody{get;set;}
public string csvAsString{get;set;}
public String[] csvFileLines{get;set;}
public String[] csvBodyData{get;set;}

public List <pricebook2> headerList{get;set;} //header do catalogo de preços
public List <pricebookentry> bodyList{get;set;}// entradas de preços
PricebookEntry body = new PricebookEntry();
Product2 IdProduct;
Account  IdAccount;


 
public importDataFromCSVController(){
    csvFileLines = new String[]{};
    bodyList = New List <PricebookEntry>();
    headerList = New List <Pricebook2>();
   }
  

  public void importCSVFile(){
      
          /*Manipulação do arquivo para um string, por linha*/
          
           csvAsString = EncodingUtil.base64Encode(csvFileBody);
           csvFileLines = csvAsString.split('\n'); 
                      
           /*Lendo cabeçalho do catalogo de preços */
             Pricebook2 header = new Pricebook2 () ;
             string[] csvHeaderData = csvFileLines[1].split(';');
             header.name = csvHeaderData[1] ; 
             headerList.add(header);
             insert headerList; 
             
            
           
           /* Atualizar o campo pricebook*/
          
              Pricebook2 IDPrice = header;
              System.debug('Código conta'+ csvHeaderData[0]);
              try{
              IdAccount = [Select ID from Account where AccountNumber =: csvHeaderData[0]];
              IdAccount.Account_Pricebook__c =  IDPrice.id;
              Update IdAccount;
                   
              }catch (Exception A)
              {
                ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'Não tem conta vinculada');
                ApexPages.addMessage(errorMessage);
               }
             
               
           //lendo entrada da tabela de preços
           for(Integer i=3;i<csvFileLines.size();i++)
           {
            body.Pricebook2 = IDPrice; 
            csvBodyData = csvFileLines[i].split(';');
            IdProduct = [SELECT ProductCode FROM Product2 WHERE id =:csvBodyData[0]];
            body.Product2 = IdProduct;
            body.UnitPrice = Integer.valueOf(csvBodyData[1]);
            bodyList.add(body);
           }
           
        insert bodyList; 
        }
     }   
VF page:


//visualforce
<apex:page controller="importDataFromCSVController">
    <apex:form >
        <apex:pagemessages />
        <apex:pageBlock >
            <apex:pageBlockSection columns="2"> 
                  <apex:inputFile value="{!csvFileBody}"  filename="{!csvAsString}"/>
                  <apex:commandButton value="Importar Lista de Preços" action="{!importCSVFile}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
   </apex:form>
</apex:page>

Thank you :)