0

I have a requirement to insert records into custom object called Custom_Business__c using a csv file which is stored as a static resource.

Below is the code I've achieved so far

public class importDataFromCSVController {
    public String[] filelines {get;set;}
    public String[] nameFile {get;set;}
    public importDataFromCSVController() {

        Savepoint sp;
        sp = Database.setSavepoint();
        StaticResource FiveDigitUPCodeMappingDoc = [Select Id,Body from StaticResource where name ='Custom Csv File'];
        String hex5digitUPMapStr = EncodingUtil.convertToHex(FiveDigitUPCodeMappingDoc.Body);
        System.assertEquals(0, hex5digitUPMapStr.length() & 1);
        final Integer bytes5digitUPMap = hex5digitUPMapStr.length() >> 1;
        String[] bytes5digitUPStringArr = new String[bytes5digitUPMap];
        for(Integer i = 0; i < bytes5digitUPMap; ++i)
            bytes5digitUPStringArr[i] =  hex5digitUPMapStr.mid(i << 1, 2);
        nameFile = EncodingUtil.urlDecode('%' + String.join(bytes5digitUPStringArr, '%'), 'ISO-8859-1');
        filelines = nameFile.split('\n');
        list<Custom_Business__c> updatecustomlist=new list<Custom_Business__c>();
        for (Integer i=1;i<filelines.size();i++) {
            String[] inputvalues = new String[]{};
                inputvalues = filelines[i].split(',');
            system.debug('Custom ID is'+inputvalues[1]);
        }
    }
}

Now, using Custom csv file which is stored in static resource, I need to insert records into Custom Business Object. Can anyone please suggest insert process from above code.

3
  • This really doesn't make sense. Why are you inserting records from a Static Resource? Why would that process need to be executed more than once? Why is this key business data stored in such an awful format for Apex consumption?
    – David Reed
    Commented May 30, 2020 at 5:44
  • That was my requirement. The csv file will be having few records and using that, we need to insert records into custom business object. I came across vf page with insert button and other scenarios but instead, is there any way to insert csv file stored in static resource using apex class. Or is there any logic to parse csv from anonymous window which can be used only once. Please suggest
    – S kanth
    Commented May 30, 2020 at 6:45
  • Any data loader can parse a CSV file and import records for you. If this is a one-time process, I would recommend using such a tool, not writing Apex code.
    – David Reed
    Commented May 30, 2020 at 21:25

1 Answer 1

0
Database.SaveResult[] csList = Database.insert(updatecustomlist, false);

for (Database.SaveResult cs : csList) {
   if (cs.isSuccess()) {
      // Audit steps
   } else {
      // This condition will be executed for failed records
      for(Database.Error objErr : cs.getErrors()) {
         System.debug('The following error has occurred.');

         System.debug(objErr.getStatusCode() + ': ' + objErr.getMessage());
         System.debug('The following fields are in error:' 
            + objErr.getFields());
      }
   }

If you want to Create or Update (if existing) at once , use Database.upsert. Using the Database methods allows for partial inserts/upserts even in case few records fail. The failure of these few records can also be logged and reported in the else portion of the above code.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .