i've got a csv file containing records to update/insert on salesforce. I did split the csv files in 2 lists of string, one containing the field api name and one containing the field values. The problem is: when i try to populate my generic sObject via the put method, everytime i come across a value different from String, lt's say Decimal, it gives me "illegal conversion" error. How can i dynamically cast the value from String into the correct type for that field? I tried to look into the Schema class, but probably i'm missing something.
Here's my code
List<List<String>> allFields = parseCSV(csv,false); //method that returns csv as a single String
List<String> fieldApiName = allFields.remove(0); //separating header(which contains field api name)
for(Integer k=0; k<allFields.size(); k++){
sObject obj = newObjFromString(objNameFromRow(row));
for(Integer i=0; i<allFields[k].size(); i++){
if(Schema.getGlobalDescribe().get(objNameFromRow(row)).getDescribe().fields.getMap().get(fieldApiName[i].replaceAll('"', '')).getDescribe().isCreateable()){ //checking if field is editable
obj.put(fieldApiName[i].replaceAll('"', ''), allFields[k][i].replaceAll('"', ''));
}
else{
continue;
}
}
How can i do a "dynamic" put of values into sObject?