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
Big EarsBig Ears 

Exception handling - getDMLId() always returns null

Dear all,

I can't work out if I've misunderstood the documentation or if there's an issue with the code itself, but I'm trying to build a utility class that can handle DML exceptions and send me an email listing the issues. I pass in the exception and the class/method that caused it:
public static void exceptionhandler(DmlException e, string className){
	string messages='\r\r';
		
	for(integer i = 0 ; i < e.getNumDml(); i++){
		
		messages = messages+'\r\r'+i+1+') '+e.getDmlType(i)+'\r'+'Record Id - '+e.getDmlId(i)+'\rFields Affected - ';
			
		string fieldsAffectedDML = '';
		for(string s : e.getDmlFieldNames(i)){
			fieldsAffectedDML = fieldsAffectedDML+s+', ';
		}
			
		messages = messages+fieldsAffectedDML+'\rError Message - '+e.getDmlMessage(i)+'\r\r';
	}
		
	Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
	message.setSubject('DML Error in class/method - '+className);
	message.setPlainTextBody('There has been a DML Exception that may have caused some unexpected behaviours'+messages);
	message.setToAddresses(new String[] {'arouse@thunderhead.com'});
	Messaging.sendEmail(new Messaging.Email[] {message});
		
	}
Here are the issues I'm facing:
  • The getDMLId() function always returns null
  • The getDMLFieldNames() function always returns null
Does anybody have any ideas?

Best Answer chosen by Big Ears
Pavan Kumar KajaPavan Kumar Kaja
Hi ,

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

As per the document

 getDMLId():  Will available only updation or deletion failed cases ,  If in case insert fails getDMLId() always be null.

getDMLFieldNames() : Returns the field token or tokens for the field or fields that caused the error described by the ith failed row. For more information on field tokens,

for example iam inserting opportunity with  only field filled name 
on insert required fields are missed then those field names will come like StageName, CloseDate



INSERT:
There has been a DML Exception that may have caused some unexpected behaviours



01) REQUIRED_FIELD_MISSING
Record Id - null
Fields Affected - StageName, CloseDate,
Error Message - Required fields are missing: [StageName, CloseDate]

UPDATE:
There has been a DML Exception that may have caused some unexpected behaviours



01) FIELD_CUSTOM_VALIDATION_EXCEPTION
Record Id - 0069000000KBDBDAA5
Fields Affected -
Error Message - Due to validation

All Answers

Pavan Kumar KajaPavan Kumar Kaja
Hi ,

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

As per the document

 getDMLId():  Will available only updation or deletion failed cases ,  If in case insert fails getDMLId() always be null.

getDMLFieldNames() : Returns the field token or tokens for the field or fields that caused the error described by the ith failed row. For more information on field tokens,

for example iam inserting opportunity with  only field filled name 
on insert required fields are missed then those field names will come like StageName, CloseDate



INSERT:
There has been a DML Exception that may have caused some unexpected behaviours



01) REQUIRED_FIELD_MISSING
Record Id - null
Fields Affected - StageName, CloseDate,
Error Message - Required fields are missing: [StageName, CloseDate]

UPDATE:
There has been a DML Exception that may have caused some unexpected behaviours



01) FIELD_CUSTOM_VALIDATION_EXCEPTION
Record Id - 0069000000KBDBDAA5
Fields Affected -
Error Message - Due to validation

This was selected as the best answer
Big EarsBig Ears

Ashi,

Thank you. I've been calling the class from all over my org code, but you've rightly spotted that the only errors being generated have happened to be in insert calls. That's why I've not been seeing record ids. Thank you!
 

Andy