function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
wchoywchoy 

Losing field level information

I'm having an issue where when I perform a dml operation on a sobject list, I lose field level information when catching the dml exception.  Does anyone know a work around for this issue?

 

Here is a quick example.

 

List<SObject> obj = [SELECT SomeField__c FROM CustomObject__c];

 

try

{

    update obj;

}

catch (DmlException e)

{

    // catching a field level validation 

    e.getDmlFields(0); //this returns null

}

 

Thanks!

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

e.getDmlFields(0) --> This will return name of the fiels if it is required at field level.

e.getDmlFields(0); --> This will return null if you define a custom validation rule and select the Error Location as "Top of the page"

e.getDmlFields(0); --> This will return field name if you define a custom validation rule and select the Error Location as Field and select appropriate field.

wchoywchoy

Yeah, I do have a custom validation rule and it is set to it's field level.  Any other thoughts?

Tim BarsottiTim Barsotti

The getDMLFields method will only return an error if there is an error thrown by one of the fields. Did your first record fail? getDmlFields(0) will only return the error if the first record failed. You need to iterate through your list of records to pull all exceptions: 

 

It is possible that a different issue caused your error so you probably would want to system.debug(getDmlMessage()

 

try{
update x;
} catch (exception e) {
for (Integer i = 0; i < e.getNumDml(); i++) { // Process exception here System.debug(e.getDmlFields(i));
System.debug(e.getDmlMessage(i)); }
}

 

 If you want to see the data of what was being updated, I suggest you try something along the lines of: system.debug(sObj);

wchoywchoy

Yeah, the first record failed on a field level validation that I set.

 

All of the DmlException methods (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_exception_methods.htm)

return the correct information except for getDmlFields().  Even getDmlFieldNames() returns the correct field name in which the dml operation is failing on.

Tim BarsottiTim Barsotti

Ah yes. Upon looking closer the getDMLFields does not return a string. It returns data type: Schema.sObjectField []. So it returns the token for the field. That might be why it shows up as null in logs. 

 

Might need to try something like GetDMLFields(0).getName() to return a string. 

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_class_Schema_SObjectField.htm

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_Schema_DescribeFieldResult_instance_methods.htm