Salesforce Exception常用的方法及自定义Exception

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/itsme_web/article/details/81032507

1、【Exception常用的方法及含义】:Create Custom Exceptions
System.DmlException e
a、e.getNumDml(),获取dml操作中出异常的总个数;
b、e.getDmlMessage(),获取Dml具体异常信息;
c、To create your custom exception class, extend the built-in Exception class and make sure your class name ends with the word Exception
2、【Sample】:

public without sharing class LeadUtil {
// Class for custom Exception
	private class LeadUtilException extends Exception {}
 
	public static Boolean UserHasDeleteLeadPermission() {
  		return [
   			SELECT Id, HasDeleteLeadPermission__c
   			FROM User
   			WHERE Id = : userInfo.getUserId()
  		].HasDeleteLeadPermission__c;
	}
 
	public static void HandleLeadDeletion(List < Lead > leadList) {
  		Boolean changeAllowed = UserHasDeleteLeadPermission();
 
  		for (Lead lead: leadList)
   			if (lead.MasterRecordId == null && !changeAllowed)
    			throw new LeadUtilException('Delete is not allowed!');
	}
}
public class MetadataServiceFunction {
    public class MetadataServiceFunction extends Exception {}

    /**
     * Example helper method to interpret a SaveResult, throws an exception if errors are found
     **/
    public static void handleSaveResults(MetadataService.SaveResult saveResult) {
        // Nothing to see?
        if(saveResult==null || saveResult.success)
            return;
        // Construct error message and throw an exception
        if(saveResult.errors!=null) {
            List<String> messages = new List<String>();
            messages.add(
                (saveResult.errors.size()==1 ? 'Error ' : 'Errors ') +
                    'occured processing component ' + saveResult.fullName + '.');
            for(MetadataService.Error error : saveResult.errors)
                messages.add(
                    error.message + ' (' + error.statusCode + ').' +
                    ( error.fields!=null && error.fields.size()>0 ?
                        ' Fields ' + String.join(error.fields, ',') + '.' : '' ) );
            if(messages.size()>0)
                throw new MetadataServiceFunction(String.join(messages, ' '));
        }
        if(!saveResult.success)
            throw new MetadataServiceFunction('Request failed with no specified error.');
    }
}

猜你喜欢

转载自blog.csdn.net/itsme_web/article/details/81032507