buli
2022-05-14 ead4df22dca33a867279471821ca675f91dec760
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
public with sharing class TydelikeConController {
    public Account acc {get;set;}
    public Contact con {get;set;}
    public TydelikeContact__c tyc {get;set;}
    public TydelikeAccount__c tya {get;set;}
    public String accName { get; set; }
    public String tycoId { get; set; }
    public String tyacId { get; set; }
 
    public String baseUrl { get; set; }
    public String rtUrl { get; set; }
 
    public TydelikeConController() {
        baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        String path = URL.getCurrentRequestUrl().getPath();
        if (path.indexOf('/apex') > 0) {
            baseUrl += path.substring(0,path.indexOf('/apex'));
        } else if (path.indexOf('production/') > 0) {
            baseUrl += '/production';
        }
        rtUrl = System.currentPageReference().getParameters().get('retURL');
        if (rtUrl == null || rtUrl == 'null') {
            rtUrl = '';
        }
    }
    //页面初始化
    public PageReference init() {
        accName = System.currentPageReference().getParameters().get('accName');
        tycoId = System.currentPageReference().getParameters().get('tycoId');
        tyacId = System.currentPageReference().getParameters().get('tyacId');
        //传来的客户的名字查询
        List<Account> accList = [select Id,Name,ProductSegment__c from Account where Name =: accName];
        if (accList.size() > 0) {
            acc = accList[0];
        }
        //传来的联系人的id查询
        List<TydelikeContact__c> tycoList = [select 
        Id,Name,TyAddress__c,TyFaxD__c,TyPhoneD__c,TyPostcode__c,EnglishAddress__c,ContactEnglishName__c 
        from TydelikeContact__c 
        where Id =: tycoId];
        if (tycoList.size() > 0) {
            tyc = tycoList[0];
        }
 
        //初始化页面的显示
        con = new Contact();
        con.AccountId = acc.Id;
        con.LastName = tyc.Name;
        con.Address1D__c = tyc.TyAddress__c;
        con.FaxD__c = tyc.TyFaxD__c;
        con.StatusD__c = '草案中';
        con.MobilePhoneD__c = tyc.TyPhoneD__c;
        con.PostcodeD__c = tyc.TyPostcode__c;
        con.EnglishAddress__c = tyc.EnglishAddress__c;
        con.ContactEnglishName__c = tyc.ContactEnglishName__c;
        if (acc.ProductSegment__c == 'BS') {
            con.ProductSegmentBS__c = true;
        }else if(acc.ProductSegment__c == 'IE'){
            con.ProductSegmentIE__c = true;
        }else if(acc.ProductSegment__c == 'NDT'){
            con.ProductSegmentNDT__c = true;
        }else if(acc.ProductSegment__c == 'ANI'){
            con.ProductSegmentANI__c = true;
        }else if(acc.ProductSegment__c == 'RVI'){
            con.ProductSegmentRVI__c = true;
        }
 
        return null;
    }
 
    public PageReference save() {
        tycoId = System.currentPageReference().getParameters().get('tycoId');
        tyacId = System.currentPageReference().getParameters().get('tyacId');
        List<TydelikeContact__c> tyconList = new List<TydelikeContact__c>();
        TydelikeContact__c tycontact = new TydelikeContact__c();
        tycontact.Id = tycoId;
        tycontact.OfSkrap__c = true;
        tycontact.Ofomsa__c = true;
        tyconList.add(tycontact);
 
        List<TydelikeAccount__c> tyacList = new List<TydelikeAccount__c>();
        TydelikeAccount__c tyaccount = new TydelikeAccount__c();
        tyaccount.Id = tyacId;
        tyaccount.Ofomsa__c = true;
        tyacList.add(tyaccount);
        //临时客户与联系人的OfSkrap__c,Ofomsa__c可参与调查。
        Savepoint sp = Database.setSavepoint();
        try {
            update tyconList;
            update tyacList;
            insert con;
            
            String url = baseUrl + '/apex/TydelikeAC';
            return new Pagereference(url);
        } catch (Exception e) {
            Database.rollback(sp);
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, e.getMessage()));
        }
        return null;
    }
}