liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
public with sharing class LexUtils {
    //获取记录类型Id
    @AuraEnabled
    public static string getRecordTypeIdByName(String Name,String DeveloperName){
        return [SELECT Id FROM RecordType where name =: Name and DeveloperName =:DeveloperName].Id;
 
    }
    //获取当前用户信息
    @AuraEnabled(cacheable=true)
    public static Sobject getCurrentUserInfo(){
        return getRecord(UserInfo.getUserId(),'User');
    }
    //根据用户名获取用户信息    
    @AuraEnabled(cacheable=true)
    public static User getUserByName(String name){
        if(String.isBlank(name)){
            return null;
        }
        List<User> listUser = [select id, Name from User where Name=:name];
        return listUser.size()>0 ? listUser[0] : null;
    }
    //获取全字段记录信息(记录ID,记录ApiName)
    @AuraEnabled(cacheable=false)
    public static Sobject getRecord(String recordId,String objectApiName){
        SObjectType sobjType = Schema.getGlobalDescribe().get(objectApiName);
        Map<String,Schema.SObjectField> mfields = sobjType.getDescribe().fields.getMap();
        List<String> listAllFields = new List<String>();
        listAllFields.addAll(mfields.keyset());
        for(String fieldApi : mfields.keyset() ){
            Schema.DescribeFieldResult sFieldDescribe = mfields.get(fieldApi).getDescribe();
            if( sFieldDescribe.getType() ==  DisplayType.Reference && String.isNotBlank(sFieldDescribe.getRelationshipName())){
                //system.debug('###fieldApi='+fieldApi+' ##sFieldDescribe.getReferenceTargetField()='+sFieldDescribe.getReferenceTargetField()+' ##sFieldDescribe='+sFieldDescribe);
                if(sFieldDescribe.getRelationshipName() == 'LastAmountChangedHistory' || sFieldDescribe.getRelationshipName() == 'QuoteLineItem' ||
                 sFieldDescribe.getRelationshipName() == 'LastCloseDateChangedHistory' || sFieldDescribe.getRelationshipName() == 'OriginalOrderItem'){
                    continue;
                }
                listAllFields.add(sFieldDescribe.getRelationshipName() +'.Name');
            }
        }
        system.debug('#####'+String.join(listAllFields, ','));
        String queryString = 'select ' + String.join(listAllFields, ',') + ' from ' + objectApiName + ' where id=:recordId';
        List<Sobject> listSobject = Database.query(queryString);
        return listSobject.size() > 0? listSobject[0]:null;
    }
    //提交审批
    @AuraEnabled
    public static String submitApproval(String recordId){
        Savepoint sp = Database.setSavepoint();
        try {
            Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
            req1.setObjectId(recordId);
            Approval.ProcessResult submitResult = Approval.process(req1);
            return 'OK';
        }
        catch (Exception e) {
            Database.rollback(sp);
            String chineseErrorMessage = '';
            String errorMessage = e.getMessage();
            
            String chinesePattern = '[\\u4e00-\\u9fa5]+';
            Pattern p = Pattern.compile(chinesePattern);
            Matcher matcher = p.matcher(errorMessage);
            
            while (matcher.find()) {
                chineseErrorMessage += matcher.group();
            }
            return chineseErrorMessage;
        }
    }
}