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
DJ 367DJ 367 

attach file based on system url

Hello All,

I have a excel sheet in which I have following columns AccountName,AccountNumber,Type,Industry,Filelocation
whenever I am importing a record a new account to be created with attachment(attachment should be from location mentioned in the excel sheet)

User-added image
for example I have this data whenever I click on import button it should insert the account with attachment located in the 
C:\AccountFile.

Can someone please help me to achieve this, I have inserted the record but not able to attach file to the account.

my code is here
<apex:page controller="importDataFromCSVController">
    <apex:form >
        <apex:pagemessages />
        <apex:pageBlock >
            <apex:pageBlockSection columns="4"> 
                  <apex:inputFile value="{!csvFileBody}"  filename="{!csvAsString}"/>
                  <apex:commandButton value="Import Account" action="{!importCSVFile}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
        <apex:pageBlock >
           <apex:pageblocktable value="{!accList}" var="acc">
              <apex:column value="{!acc.name}" />
              <apex:column value="{!acc.AccountNumber}" />
              <apex:column value="{!acc.Type}" />
              <apex:column value="{!acc.Accountsource}" />
              <apex:column value="{!acc.Industry }" />
        </apex:pageblocktable>
     </apex:pageBlock>
   </apex:form>
</apex:page>

controller
 
public class importDataFromCSVController {
public Blob csvFileBody{get;set;}
public string csvAsString{get;set;}
public String[] csvFileLines{get;set;}
public List<account> acclist{get;set;}
  public importDataFromCSVController(){
    csvFileLines = new String[]{};
    acclist = New List<Account>(); 
  }
  
  public void importCSVFile(){
       try{
           csvAsString = csvFileBody.toString();
           csvFileLines = csvAsString.split('\n'); 
            
           for(Integer i=1;i<csvFileLines.size();i++){
               Account accObj = new Account() ;
               string[] csvRecordData = csvFileLines[i].split(',');
               accObj.name = csvRecordData[0] ;             
               accObj.accountnumber = csvRecordData[1];
               accObj.Type = csvRecordData[2];
               accObj.AccountSource = csvRecordData[3];   
               accObj.Industry = csvRecordData[4];                                                                             
               acclist.add(accObj);   
           }
        //insert acclist;
        }
        catch (Exception e)
        {
            ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importin data Please make sure input csv file is correct');
            ApexPages.addMessage(errorMessage);
        }  
  }
}

​​​​​​​

Thanks
KrishnaAvvaKrishnaAvva
Hi DJ,

Please find the detailed explanation with example here : https://blog.jeffdouglas.com/2010/04/28/uploading-an-attachment-using-visualforce-and-a-custom-controller/.

Please mark this as SOLVED if it had helped you. Thanks!

Regards,
Krishna Avva
DJ 367DJ 367
Hi,

Thanks for your reply but this is not what I am looking for.

Here is my controller which adding the attachment to the record , however attached file is just a simple string rather than file .
Will really appreciate you if you can help me to resolve this. Thanks
public class importDataFromCSVController {
public Blob csvFileBody{get;set;}
public string csvAsString{get;set;}
public String[] csvFileLines{get;set;}

public List<account> acclist{get;set;}
public List<Attachment> accAtt{get;set;}


  public importDataFromCSVController(){
    csvFileLines = new String[]{};
    acclist = New List<Account>(); 
    accAtt = new List<Attachment>();
  }
  
  public void importCSVFile(){
       try{
           csvAsString = csvFileBody.toString();
           csvFileLines = csvAsString.split('\n'); 
            
           for(Integer i=1;i<csvFileLines.size();i++){
               Account accObj = new Account() ;
               string[] csvRecordData = csvFileLines[i].split(',');
               accObj.name = csvRecordData[0] ;             
               accObj.accountnumber = csvRecordData[1];
               accObj.Type = csvRecordData[2];
               accObj.AccountSource = csvRecordData[3];   
               accObj.Industry = csvRecordData[4]; 
               accObj.FileLocation__c = csvRecordData[5];    
               accObj.FileName__c= csvRecordData[6];
               acclist.add(accObj);   
           }
        insert acclist;
        
        
        for(Account a :acclist){
            Attachment attachment = new Attachment();
            attachment.name = a.FileName__c;
            attachment.body = Blob.valueOf(a.FileLocation__c);
            attachment.OwnerId = UserInfo.getUserId();
            attachment.ParentId = a.id; 
            accAtt.add(attachment);
        }
        insert accAtt; 

        }
        catch (Exception e)
        {
            ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importin data Please make sure input csv file is correct');
            ApexPages.addMessage(errorMessage);
        }  
  }
}