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
LIM AI KHOONLIM AI KHOON 

A function to let user mass update record

Hi, I want to create a function where users can mass update their existing data by uploading the CSV file. This is the code. But I get an error.
public class FileUpdate {
    public string nameFile{get;set;}
    public Blob contentFile{get;set;}
    String[] filelines = new String[]{};
    List<Account> accstoupload;

    /***This function reads the CSV file and inserts records into the Account object. ***/
    public Pagereference ReadFile()
    {
        try{
                //Convert the uploaded file which is in BLOB format into a string
                nameFile =blobToString( contentFile,'ISO-8859-1');
                
                //Now sepatate every row of the excel file
                filelines = nameFile.split('\n');
                
                //Iterate through every line and create a Account record for each row
                accstoupload = new List<Account>();
                for (Integer i=1;i<filelines.size();i++)
                {
                    String[] inputvalues = new String[]{};
                    inputvalues = filelines[i].split(',');
                    
                    Account a = new Account();
                    a.Name = inputvalues[0];
                    a.ShippingStreet = inputvalues[1];       
                    a.ShippingCity = inputvalues[2];
                    a.ShippingState = inputvalues[3];
                    a.ShippingPostalCode = inputvalues[4];
                    a.ShippingCountry = inputvalues[5];
        
                    accstoupload.update(a);
                }
         }
         catch(Exception e){
                 ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured reading the CSV file'+e.getMessage());
                ApexPages.addMessage(errormsg);
         }       
        //Finally, insert the collected records
        try{
            insert accstoupload;
        }
        catch (Exception e)
        {
            ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured inserting the records'+e.getMessage());
            ApexPages.addMessage(errormsg);
        }    
        return null;
    }
   
   /**** This function sends back to the visualforce page the list of account records that were inserted ****/ 
    public List<Account> getuploadedAccounts()
    {
        if (accstoupload!= NULL)
            if (accstoupload.size() > 0)
                return accstoupload;
            else
                return null;                    
        else
            return null;
    }  

    public static String blobToString(Blob input, String inCharset){
        String hex = EncodingUtil.convertToHex(input);
        System.assertEquals(0, hex.length() & 1);
        final Integer bytesCount = hex.length() >> 1;
        String[] bytes = new String[bytesCount];
        for(Integer i = 0; i < bytesCount; ++i)
            bytes[i] =  hex.mid(i << 1, 2);
        return EncodingUtil.urlDecode('%' + String.join(bytes, '%'), inCharset);
    }   
}
This is the error
Method does not exist or incorrect signature: void update(Account) from the type List<Account>

I get an error in this line:
accstoupload.update(a);
May I know how to debug this? 
If we want to have a mass upload record from CSV files to salesforce, we can use
accstoupload.add(a);
But why I cannot change from add to update when I want to update the existing records?

 
Best Answer chosen by LIM AI KHOON
AbhinavAbhinav (Salesforce Developers) 
Hey Lim... that what I highlighted we  don't update a record by using 

ListOfRecord.update(a) in your case it is accstoupload.update(a)

if we want to update a record than we add it to list first  ListOfRecord.add(a) and then use DMl Update .
Update ListOfRecord

I hope I made it clear.

Thanks!

All Answers

AbhinavAbhinav (Salesforce Developers) 
HI Lim,

When you are writing this accstoupload.add(a);  you are just adding one account record to the list. Its not getting inserted/ uploaded from here.

You have used insert accstoupload; to insert/upload records.

So this accstoupload.update(a)  in incorrect . 

Thanks!
LIM AI KHOONLIM AI KHOON
If accstoupload.update(a) is correct, why I still get the same error? 
this is the error Method does not exist or incorrect signature: void update(Account) from the type List<Account>
AbhinavAbhinav (Salesforce Developers) 
Hey Lim... that what I highlighted we  don't update a record by using 

ListOfRecord.update(a) in your case it is accstoupload.update(a)

if we want to update a record than we add it to list first  ListOfRecord.add(a) and then use DMl Update .
Update ListOfRecord

I hope I made it clear.

Thanks!
This was selected as the best answer
LIM AI KHOONLIM AI KHOON
oh, understand already. Thank you :-)