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
testrest97testrest97 

Custom CSV upload using apex on button click

Hello All

I have a requirement of taking a csv and uploading records into a object, no validations required. Just parsing the csv and inserting records.
 Could anyone point me to any blog/website where I can find this stuff. Any help is appreciated.
Nirmal ChristopherNirmal Christopher
If you want to insert a CSV as a attachment to a record or do you want to insert the  records in the CSV file to a object's database ? If you want to insert records use apex data loader. if you want to insert the record as a ttachment  just parse the file and insert the record as an attachment.
testrest97testrest97
insert the  records in the CSV file to a object's database
JJSHHSJJSHHS
if you want to insert the record as a ttachment  just parse the file and insert the record as an attachment. - how to parse and insert the record as an attachment, can some one please clarify?
Nirmal ChristopherNirmal Christopher
@JJSHHS

to insert a record in attachment via apex just create a instance of the attachment object and do a DML inser. Something like this 
Attachment appAttach = new attachment();
     attachment.OwnerId = UserInfo.getUserId();
        
     attachment.ParentId = c.id ;// the record the file is attached to
     try {
      insert attachment;
       appAttach.body = attachment.Body;
       appAttach.name = attachment.name;
                      
            } catch (DMLException e) {
     ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
      return null;
     } finally {
      attachment = new Attachment();
     }
     appAttach.OwnerId = UserInfo.getUserId();
     appAttach.ParentId = appAttachID  ;// the record the file is attached to
     try {
      insert appAttach ;
}............................


If you are getting the attachmet from a REST api or if u wanna send the attachment via email. Parsethe attachment body to a Blob data type (If required)
Chandra PrakashChandra Prakash
Hello ,

Please try below link..

http://www.ericsantiago.com/eric_santiago/2011/03/upload-and-parse-csv-via-visualforce.html

Thanks..