buli
2022-04-25 4cf305347ea6f6a73e03fa9427a3de80ca35ae7a
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Author: Yanan Chen
 * Created Date: 02/22/2022
 * Purpose: Utility class for describe layouts
 * Test Class: NewAndEditContactController
 * History: 
 *      02/22/2022 - Yanan Chen - Initial Code.
 * 
 * */
global class NewAndEditContactController extends NewAndEditBaseController {
 
    public String accountId{get; set;}
    public Account account{get; private set;}
 
    public NewAndEditContactController(ApexPages.StandardController controller){
        
        List<String> fieldList = new List<String>(Schema.getGlobalDescribe().get('Contact').getDescribe().fields.getMap().keyset());  
        
        // Add fields to controller. This is to avoid the SOQL error in visualforce page
        //Get Account Id from url param
        // this.accountId = ApexPages.currentPage().getParameters().get('retURL');
        system.debug('mso='+ApexPages.currentPage().getParameters());
        this.accountId = ApexPages.currentPage().getParameters().get('accid');
        if(string.isBlank(this.accountId)){
            system.debug('retake accid');
            this.accountId = ApexPages.currentPage().getParameters().get('con4_lkid');
        }
        System.debug('accountId = ' + accountId);
 
        // System.debug('accountType = ' + );
        if(!Test.isRunningTest()){
            controller.addFields(fieldList);
        }
        
        Init(controller.getRecord());
        
        if (this.accountId != '/003/o' && this.accountId != null && this.isNewMode) {
            ID accId = ID.valueOf(this.accountId);   
            System.debug('accId = ' + accId);
            
            account = [select id, Name, PhoneD__c, FaxD__c, Address1D__c, PostCodeD__c, ProductSegment__c from Account where id =:accId];
            System.debug('account = ' + account);
            
        }
 
        SObject obj = controller.getRecord();
        if(obj.Id == null){
            //初始化加载值
            obj.put('OwnerId',UserInfo.getUserId());
        }
 
        if (this.account != null) {
            obj.put('AccountId', this.accountId);
            obj.put('ProductSegment'+this.account.ProductSegment__c+'__c', true);
            obj.put('Fax', this.account.FaxD__c);
            obj.put('FaxD__c', this.account.FaxD__c);
            obj.put('Phone', this.account.PhoneD__c);
            obj.put('PhoneD__c', this.account.PhoneD__c);
            obj.put('Address1__c', this.account.Address1D__c);
            obj.put('Address1D__c', this.account.Address1D__c);
            obj.put('Postcode__c', this.account.PostcodeD__c);
            obj.put('PostcodeD__c', this.account.PostcodeD__c);
        }
    }
    
    PageReference RedirectStandardPage(){
        system.debug('RedirectStandardPage');
        Map<string,string> mso = ApexPages.currentPage().getParameters();
        system.debug(mso);
        PageReference pg = null;
        mso.remove('sfdc.override');
        mso.remove('save_new');
        system.debug('recordId='+recordId);
        if(string.isBlank(recordId)){
            pg = new PageReference('/003/e');
        }else{
            pg = new PageReference('/'+recordId+'/e');
        }
        pg.getParameters().putAll(mso);
        //pg.getParameters().put('RecordType',mso.get('RecordType'));
        //pg.getParameters().put('accid',mso.get('accid'));
        pg.getParameters().put('nooverride','1');
        pg.setRedirect(true);
        
        return pg;
    }
 
    public PageReference PageLoad(){
        system.debug('rtTypeId='+rtTypeId);
        
        string s = null;
        string accid = ApexPages.currentPage().getParameters().get('accid');
        if(string.isBlank(accid)){
            system.debug('retake accid');
            accid = ApexPages.currentPage().getParameters().get('con4_lkid');
        }
        if(!string.isBlank(accid)){
            List<Account> accs = [select RecordType.DeveloperName from account where id = :accid];
            system.debug(accs);
            if(accs.size()>0){
                s = Schema.SObjectType.Account.getRecordTypeInfosById().get(accs[0].RecordTypeId).getDeveloperName();
                system.debug('s='+s);
                if(s == 'Dealer' || s == 'Agency'){
                    return RedirectStandardPage();
                }
            }
            
            
        }
        system.debug('null');
        return null;
    }
    
    @RemoteAction
    global static Response saveContact(String contactJson, String transId, Boolean isNew){
        if(Test.isRunningTest()){
            return new Response();
        }
        return save(new Contact(), contactJson, transId, isNew);
    }
}