Skip to main content
Added example
Source Link
John Towers
  • 3.9k
  • 1
  • 21
  • 30

You have to parse the string as a date using Date.valueOf() to make it a date before you can assign it to a date field.

I would use SobjecType.getDescribe().fields.getMap() to get a list of fields, loop over them to find the field currently being worked, check whether or not it's a date field, and then parse accordingly.

UPDATE with example:

I would change this:

for(Integer j=0;j<headerValues.size();j++){
        System.debug('**headerValues[j]**'+headerValues[j]+'***inputvalues[j]**'+inputvalues[j]);
        sObj.put(headerValues[j],String.ValueOf(inputvalues[j])) ;                 
    }

to something like this:

Map<String,Schema.SObjectField> fields = sobj.SObjectType.getDescribe().fields.getMap();
for(Integer j=0;j<headerValues.size();j++){
    Schema.DescribeFieldResult currentField = fields.get(headerValues[j]).getDescribe();
    if(currentField.getType() == Schema.DisplayType.Date){
        sObj.put(headerValues[j],String.ValueOf(Date.parse(inputvalues[j]))) ;
    } else {
        sObj.put(headerValues[j],String.ValueOf(inputvalues[j])) ;
    }                     
}

You have to parse the string as a date using Date.valueOf() to make it a date before you can assign it to a date field.

I would use SobjecType.getDescribe().fields.getMap() to get a list of fields, loop over them to find the field currently being worked, check whether or not it's a date field, and then parse accordingly.

You have to parse the string as a date using Date.valueOf() to make it a date before you can assign it to a date field.

I would use SobjecType.getDescribe().fields.getMap() to get a list of fields, loop over them to find the field currently being worked, check whether or not it's a date field, and then parse accordingly.

UPDATE with example:

I would change this:

for(Integer j=0;j<headerValues.size();j++){
        System.debug('**headerValues[j]**'+headerValues[j]+'***inputvalues[j]**'+inputvalues[j]);
        sObj.put(headerValues[j],String.ValueOf(inputvalues[j])) ;                 
    }

to something like this:

Map<String,Schema.SObjectField> fields = sobj.SObjectType.getDescribe().fields.getMap();
for(Integer j=0;j<headerValues.size();j++){
    Schema.DescribeFieldResult currentField = fields.get(headerValues[j]).getDescribe();
    if(currentField.getType() == Schema.DisplayType.Date){
        sObj.put(headerValues[j],String.ValueOf(Date.parse(inputvalues[j]))) ;
    } else {
        sObj.put(headerValues[j],String.ValueOf(inputvalues[j])) ;
    }                     
}
Source Link
John Towers
  • 3.9k
  • 1
  • 21
  • 30

You have to parse the string as a date using Date.valueOf() to make it a date before you can assign it to a date field.

I would use SobjecType.getDescribe().fields.getMap() to get a list of fields, loop over them to find the field currently being worked, check whether or not it's a date field, and then parse accordingly.