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

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.');
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值