public class MacNCController{
    String[] filelines = new String[]{};
    public String documentName {get;set;}
    public Blob csvFileBody{get;set;}
    public String[] contactDataLines {get;set;}
    public boolean readSuccess {get;set;}
    public List<String> lstFieldNames{get;set;}
    String contactDataAsString;
    String s;

    public String[] csvFieldNames;
        public string nameFile{get;set;}

        Contact conObj;
        public List <Contact> lstContacts {get;set;}

    String fieldValue;
    Integer fieldNumber;
    Map <String, Integer> fieldNumberMap = new Map < String, Integer > ();

  public MacNCController() 
    documentName = '';
    readSuccess = FALSE;
    contactDataLines = new String[] {};
        lstFieldNames = new List<String>();
        system.debug('**** csvFileBody ****'+csvFileBody);
   public void actionMethod(){
           csvFieldNames =new String[] {};
         system.debug('**** csvFileBody ****'+csvFileBody);
   system.debug('**** csvFileBody ****'+csvFileBody);

        filelines = nameFile.split('\n');
        csvFieldNames =filelines[0].split(',');
        System.debug('**** csvFieldNames  ****'+csvFieldNames );

            //system.debug('** accstoupload **'+accstoupload);
            SObjectType objectToken = Schema.getGlobalDescribe().get('Contact');
            Map < String, Schema.SObjectField > mapFieldList = objectToken.getDescribe().fields.getMap();
            system.debug('**** mapFieldList  **'+mapFieldList );
            for(String CsvFied:csvFieldNames ){
            system.debug('*** CsvFied ***'+CsvFied);
            for (Schema.SObjectField field: mapFieldList.values()) {
                                        Schema.DescribeFieldResult fieldResult = field.getDescribe();
                if(fieldResult.isCreateable()  == true){
                                if (fieldResult.getName() == CsvFied) {
                                system.debug('*** fieldResult.getName() ***'+fieldResult.getName());
                                system.debug('*** both matched ***');
                                system.debug('*** not matched ****');



Upload CSV file  
Here is requirement details,from csv iam passing contact data(with field names and values)iam reading the column names of csv and using schema checking in Standard object whether any object fields name is same or not,if it is creatable.now field from csv and field in Standard object are same,i did till here,now i want to create the record to the matched fields.how can i achieve,thanks in advance

enter image description here

  • can you update your question and ask with proper language so that it can be clearly understandable Commented Feb 4, 2017 at 4:33
  • sure,I have a requirement to create contact records getting from csv.since i dont want to hardcode,i had compared column names of csv and object fields which are creataeble,i found the fields,now how can i create data to that fields of Contact
    – Satya
    Commented Feb 4, 2017 at 4:36
  • you can create custom settings, put the order of column number, column headers of excel and Contact API names. Now loop the list from the excel row and try to map the columns and finally put that in a map, and then insert the map Commented Feb 4, 2017 at 4:55

1 Answer 1



1) Create a Custom Metadata Type (Contact_Metadata_Type__mdt) which will store Index and Contact API Names. This index should exactly match with the column index of the excel.

Contact metadata

2) Now loop through the excel rows and match the index of the column and store data in Contact object instance. Finally insert the data.

I have given comments in the code.

public void importCSVFile(){
           List<Contact> lstcontact = new List<Contact>();
           csvAsString = csvFileBody.toString();
           csvFileLines = csvAsString.split('\n'); 

            //retrive custom metadata types
            Map<Integer, String> settingsMap = new Map<Integer, String>();
            Contact_Metadata_Type__mdt[] contactRptData = [SELECT MasterLabel, 
                                                            FROM Contact_Metadata_Type__mdt];
            //put in order and API Names                                                
            for(Contact_Metadata_Type__mdt obj: contactRptData)
                settingsMap.put(obj.column_Index__c, obj.API_Name__c);

            //loop through the lines
           for(Integer i=1;i<csvFileLines.size();i++){
               Contact conObj = new Contact() ;
               string[] csvRecordData = csvFileLines[i].split(',');

               //for each line go through column by column and find the index from settinsMap
               for(Integer p=0; p<csvRecordData.size(); p++)
                   //prepare map like this:
                   //conObj.put(CONTACT API NAME, CONTACT DATA FROM EACH COLUMN);
                   conObj.put(settingsMap.get(p), csvRecordData[p]);

        insert lstcontact;
        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');

Note: I haven't compiled the code. Only tried to put logic properly. This approach should work.

  • give column names is nothing but harding,correct me if iam wrong
    – Satya
    Commented Feb 4, 2017 at 6:02
  • I have not hardcoded anything here, specified which index will refer to which API Names, otherwise how can you map? Even if you try to upload record from the dataloader you have to specify mapping. Hopefully, you have understood Commented Feb 4, 2017 at 6:08
  • iam facing one issuse if tried to upload a csv of 5 records,then in updated list size is 6,if i tried to delete the last,it is showing exception index out of bonds
    – Satya
    Commented Feb 4, 2017 at 8:40
  • That's a separate issue, you can accept this answer if your purpose has solved and post a new question Commented Feb 4, 2017 at 14:50

You must log in to answer this question.

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