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
da wangda wang 

opportunity product related list bulk upload

i want to develop a function to upload bulk CSV data into profuct as below screenshot.
User-added image
how to write the VF page and APEX code and how to deal with the csv file. i have no idea about the code develope.....
William LópezWilliam López
Hello da wang,

I tried this in the past and I think the best way to parce the CVS file its using an external server like heroku, but it can be done all native in apex, here are my ideas how to tackle this.

The thing that you need to take into cosideration, the products that you want to upload need to be in the pricebook selected for that opportunity.

Step 1: Form your upload button desing an upload page like this one:

http://developer.force.com/cookbook/recipe/uploading-a-document-using-visualforce-and-a-custom-controller

Step 2: In the controller do a "insert document" and save the document.

The next step that its to parce this document its the tricky part of salesforce. you can get the document.body and split the CVS parts based on the lines of the file and then commas of each line.

Here is some code to start with a custom solution within apex:
 
for(String line : d.body.toString().split('\\n')) { 

   //each line will be a record   
     for(String col:line .split(',')) {
              //Here based on the line position save the col value into a object field
     } 

    //Here you need to set up the rest of the info for the OpportunityLineItem based on the pricebook and the products selected

}

Let me know how it goes.
da wangda wang
hi Bill López ,

I have done step1 as you recommanded. but one thing is different , my requeirment is to upload csv data into opprotunity product related list not document folder just like below screenshot. After done step 1 , i upload one file , and it's been uploaded to document folder.  i am a freshman , so its kind of difficult for me to write code to complete the requirment, so i just imitate and copy other's code .could you show me the whole code of my requeirment?.below is my salesforce account: could you help me in my SFDC environment? username: da.wang@salesforce.com    password:199419adam     Apex class:FileUploadController   Page:OpportunityProducts. thanks a lot for your kind help.
User-added image