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
sylar20sylar20 

Upload Record for Custom Object

Hi,

I have created a custom controller and an VF page to import records for my Custom Object Username.

But the issue is I'm not able to import all records from the template.

Only the first record is getting imported.

 

VF page:

 

 

public class UsernameFileUploader 
{
    
public string nameFile{get;set;}
    public Blob contentFile{get;set;}
    String[] filelines = new String[]{};
    List<Username__c> urnmtoupload;
    
    public Pagereference ReadFile()
    {
        nameFile=contentFile.toString();
        filelines = nameFile.split('\n');
        urnmtoupload = new List<Username__c>();
        for (Integer i=1;i<filelines.size();i++)
        {
            String[] inputvalues = new String[]{};
                inputvalues = filelines[i].split(',');  
                Username__c a = new Username__c();
                a.Username__c = inputvalues[0];
                a.Contact_Name__c = inputvalues[1];       
                a.Phone__c = i=integer.valueof(inputvalues[2]);
                a.Email_Address__c = inputvalues[3];
                a.Billing_Account__c = inputvalues[4];
                a.View__c = inputvalues[5];
                a.Bill_Code__c = inputvalues[6];
                if (inputvalues[7] == 'True')
                {a.Lookup__c = True;}
                else
                {a.Lookup__c = False;}
                
                if (inputvalues[8] == 'True')
                {a.Summary__c = True;}
                else
                {a.Summary__c = False;}
                
                if (inputvalues[9] == 'True')
                {a.View_Score__c = True;}
                else
                {a.View_Score__c = False;}
                
                if (inputvalues[10] == 'True')
                {a.Report__c = True;}
                else
                {a.Report__c = False;}
                
                if (inputvalues[11] == 'True')
                {a.Draft__c = True;}
                else
                {a.Draft__c = False;}
                
                if (inputvalues[12] == 'True')
                {a.Order__c = True;}
                else
                {a.Order__c = False;}

                if (inputvalues[13] == 'True')
                {a.Pass_Fail__c = True;}
                else
                {a.Pass_Fail__c = False;}
                
                
                a.Additional_Notes__c = inputvalues[14];


            urnmtoupload.add(a);
        }
        try{
        insert urnmtoupload;
        }
        catch (Exception e)
        {
            ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template or try again later');
            ApexPages.addMessage(errormsg);
        }    
        return null;
    }
    
    public List< Username__c> getuploadedUsernames()
    {
        if (urnmtoupload!= NULL)
            if (urnmtoupload.size() > 0)
                return urnmtoupload;
            else
                return null;                    
        else
            return null;
    }            
}

 

 

 

Controller:

 

 

 

<apex:page Controller="UsernameFileUploader" >      
<apex:form >

<apex:pageBlock title="Upload data from CSV file">
             <center>
              <apex:inputFile value="{!contentFile}" filename="{!nameFile}" /> <apex:commandButton action="{!ReadFile}" value="Upload File" id="theButton" style="width:70px;"/>
              <br/> <br/> <font color="red"> <b>Note: Please use the standard template to upload Usernames. <a href="{!URLFOR($Resource.UserNameUploadTemplate)}" target="_blank"> Click here </a> to download the template. </b> </font>
             </center>  
      <apex:pageblocktable value="{!uploadedUsernames}" var="acc" rendered="{!NOT(ISNULL(uploadedUsernames))}">
          <apex:column headerValue=" Username ">
              <apex:outputField value="{!acc.Username__c}"/>
          </apex:column>
          <apex:column headerValue=" Contact Name ">
              <apex:outputField value="{!acc.Contact_Name__c }"/>
          </apex:column>
          <apex:column headerValue="Phone">
              <apex:outputField value="{!acc.Phone__c  }"/>
          </apex:column>
          <apex:column headerValue=" Email Address">
              <apex:outputField value="{!acc.Email_Address__c}"/>
          </apex:column>
          <apex:column headerValue=" Billing Account ">
              <apex:outputField value="{!acc.Billing_Account__c}"/>
          </apex:column>
          <apex:column headerValue="View">
              <apex:outputField value="{!acc.View__c }"/>
          </apex:column>
<apex:column headerValue=" Bill Code">
              <apex:outputField value="{!acc.Bill_Code__c }"/>
          </apex:column>
<apex:column headerValue="Lookup">
              <apex:outputField value="{!acc.Lookup__c }"/>
          </apex:column>
<apex:column headerValue="Summary">
              <apex:outputField value="{!acc.Summary__c  }"/>
          </apex:column>
<apex:column headerValue="View Score">
              <apex:outputField value="{!acc.View_Score__c }"/>
          </apex:column>
<apex:column headerValue=" Report">
              <apex:outputField value="{!acc.Report__c }"/>
          </apex:column>
<apex:column headerValue=" Draft">
              <apex:outputField value="{!acc.Draft__c }"/>
          </apex:column>
<apex:column headerValue=" Order">
              <apex:outputField value="{!acc.Order__c }"/>
          </apex:column>
<apex:column headerValue=" Pass / Fail">
              <apex:outputField value="{!acc.Pass_Fail__c }"/>
          </apex:column>

      </apex:pageblocktable> 
      </apex:pageBlock>       
   </apex:form>
</apex:page>

 

 

 

sravusravu

The problem might be with your readfile method in the highligthed part. If I am not wrong what you are directly hard coded the index values. For every iteration it will take the same set of values. This could be the reason that only one record is imported. If my guess is correct you will have to use two for loops in order to iterate through the rows in the file.

For the 1st iteration you will have to iterate through all the columns of the first row. end of the row will be your \n.

 

What you can think of  accessing matrix elements

 

String[] inputvalues = new String[]{};
inputvalues = filelines[i].split(',');
Username__c a = new Username__c();
a.Username__c = inputvalues[0];
a.Contact_Name__c = inputvalues[1];
a.Phone__c = i=integer.valueof(inputvalues[2]);
a.Email_Address__c = inputvalues[3];
a.Billing_Account__c = inputvalues[4];
a.View__c = inputvalues[5];
a.Bill_Code__c = inputvalues[6];
if (inputvalues[7] == 'True')
{a.Lookup__c = True;}
else
{a.Lookup__c = False;}

if (inputvalues[8] == 'True')
{a.Summary__c = True;}
else
{a.Summary__c = False;}

if (inputvalues[9] == 'True')
{a.View_Score__c = True;}
else
{a.View_Score__c = False;}

if (inputvalues[10] == 'True')
{a.Report__c = True;}
else
{a.Report__c = False;}

if (inputvalues[11] == 'True')
{a.Draft__c = True;}
else
{a.Draft__c = False;}

if (inputvalues[12] == 'True')
{a.Order__c = True;}
else
{a.Order__c = False;}

if (inputvalues[13] == 'True')
{a.Pass_Fail__c = True;}
else
{a.Pass_Fail__c = False;}


a.Additional_Notes__c = inputvalues[14];

sylar20sylar20

Can you please help me with the changes in the code??