0

I ve tried this script to create accounts from excel file and it works,but if this file had more fields ,I would have to store their values one by one.Is there a way to do this mapping using something like acc.put(fieldname,value) inside a loop?.

            String[] filelines = new String[]{};
            List<Account> accstoupload;
            StaticResource srObject = [select id,body from StaticResource 
            Where Name = 'AccountUploadTemplate'];
            String contents = srObject.body.toString();


            filelines = contents.split('\n');


            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.add(a);
            }
     insert accstoupload;
  • I would have to store their values one by one - what do you mean by this? – salesforce-sas Aug 22 at 14:08
  • what i meant was that file to create account records has 6 columns,each colum represents a field,in the script i had to store those field values one by one(a.Name = inputvalues[0]; a.ShippingStreet = inputvalues[1]; , and so on),its no big deal.But if the file had 30 colums, it would be kind of a problem,thats why i ve been loking for something generic – Dead_end_Developer Aug 22 at 14:14
0

Put exact field API names in the header of excel which will be 0 index. Collect the fieldNames and the do as following (will work for any number of fields):

    filelines = contents.split('\n');
    accstoupload = new List<Account>();

    String[] fieldNames = filelines[0].split(',');
    for (Integer i=1, linesCnt=filelines.size() ; i<linesCnt ; i++) {
        String[] inputvalues = filelines[i].split(',');
        Account newAccount = new Account();
        for(Integer c=0, fCount=fieldNames.size() ; c<fCount ; c++) {
            newAccount.put(fieldNames[c],inputvalues[c]);
        }

        accstoupload.add(newAccount);
    }
    insert accstoupload;
  • I see,problem is I must use the field names in the file – Dead_end_Developer Aug 22 at 15:54
  • Yes, exact field names so that they are mapped correctly as per account objects – salesforce-sas Aug 22 at 15:57
  • why is it a problem, in any case you will 1st row as field names? – salesforce-sas Aug 22 at 15:58
0

Store the name of the field API's which are in the header, in a list. Make sure the name of the field in the header are field API name.

filelines = contents.split('\n');
List<String> fieldNames = filelines[0].split(',');

for (Integer i=1;i<filelines.size();i++)
{
    String[] inputvalues = new String[]{};
    inputvalues = filelines[i].split(',');

    Account a = new Account();

    for(Integer data=0;data<inputvalues.size();data++)
    {
        String api = fieldNames[data];
        String value = inputvalues[data];
        a.put(api, value);
    }

    accstoupload.add(a);
}
0

Yes, you can do that. You can go through sObject class salesforce documentation wherein you can search for put method which serves your purpose.

public Object put(String fieldName, Object value)

So, if you want to create an Account object dynamically and set its value dynamically, you can probably store the mapping of the csv column label with the field names, object names, data type etc in custom settings or custom metadata and then use this custom setting data to set the values of sObject using put method of sObject class.

Here is an example where custom setting holds the mapping between Account object's API names with its corresponding column name in CSV file

//here you can have a key as the csv column header name
//if you have multiple objects, then you can create a unique key and then get the data object wise
//and use the map to get all fields
Map<String_dataset_name, CustomSettingName__c> mcs = CustomSettingName__c.getAll();

filelines = contents.split('\n');
for (Integer i=1;i<filelines.size();i++)
{
    String[] inputvalues = new String[]{};
    inputvalues = filelines[i].split(',');

    sObject acc = Schema.getGlobalDescribe().get('Account').newSObject();
    for(CustomSettingName__c cs : mcs.values()){
        acc.put(mcs.get(cs.csvColumnName__c).fieldAPIName__c,inputvalues[cs.csvColumnName__c]);
    }
    accstoupload.add(a);
}
  • 1
    While you are correct, I do not see any improvement on the original situation. – Florian Heer Aug 22 at 14:23
  • @FlorianHeer Well I answered the basic question of whether it is possible to create sObject dynamically and set its values. Anyhow, I updated the answer with the possible approach. Hope that helps the OP – Vijay Ganji Aug 22 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.