GWY
2022-04-27 12b7399736e90d33bfe0c2d29917d6f075246e00
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
public with sharing class OrderShippingNotificationController {
 
    public String type { get; set; }
 
    // 订单
    public Order ra { get; set; }
    // 订单ID
    public Id raid { get; private set; }
    // 订单记录类型
    public String recordtype { get; private set; }
    
    public Boolean hasError { get; private set; }
    public String baseUrl { get; private set; }    
 
    public OrderShippingNotificationController() {
        //Apexpages.currentPage().getHeaders().put('X-UA-Compatible', 'IE=8');
        baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        raid = ApexPages.currentPage().getParameters().get('raid');
        type = ApexPages.currentPage().getParameters().get('recordtype');
 
    }
 
    public OrderShippingNotificationController(ApexPages.StandardController stdController) {
        //Apexpages.currentPage().getHeaders().put('X-UA-Compatible', 'IE=8');
        baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        raid = ApexPages.currentPage().getParameters().get('raid');
        type = ApexPages.currentPage().getParameters().get('recordtype');
    }
 
    // 画面初始化
    public void init() {
        hasError = false;
        ra = new Order();
        List<Order> raList = [select Id, Name,ContractCode__c, ShippingNotes__c,RecordTypeId,ShippingRecieverEmailAdr__c, OppExpectedDeliveryDate__c, ContractLink__c from Order where Id = :raid];
        if (raList.size() > 0) {
            ra = raList[0];
        }
       
        ra.ShippingNotes__c = '期望发货日期:' + ra.OppExpectedDeliveryDate__c;
 
    }
 
    // 保存按钮
    public PageReference saveBtn() {
        hasError = false;
 
        String ShippingNotes = ra.ShippingNotes__c;
        if (ShippingNotes == null || ShippingNotes.trim().length() == 0) {
            ra.ShippingNotes__c.addError('请输入内容。');
            hasError=true;
            return null;
        }
 
        // 送信者
        String username = UserInfo.getName();
        // 发送时间
        Datetime dt = Datetime.now();
        // 相关用户检索
        List<Id> ccList = new List<Id>();
//      ccList.add(ra.Person_In_Charge__c);
//      ccList.add(ra.ApplyUser__c);
//        if (cc.JingliApprovalManager__c != null) {
//            ccList.add(cc.JingliApprovalManager__c);
//        }
//       if (cc.SalesManager__c != null) {
//            ccList.add(cc.SalesManager__c);
//        }
//        if (cc.BuchangApprovalManager__c != null) {
//            ccList.add(cc.BuchangApprovalManager__c);
//        }
//        if (cc.BuchangApprovalManagerSales__c != null) {
//            ccList.add(cc.BuchangApprovalManagerSales__c);
//        }
//        if (cc.ZongjianApprovalManager__c != null) {
//            ccList.add(cc.ZongjianApprovalManager__c);
//        }
        Map<Id, User> userMap = new Map<Id, User>([
            select Id, Name, Email from User where Id in :ccList
        ]);
        // 收信者
        String toName = '';
        List<String> toMailList = new List<String>();
        toName = ra.ShippingRecieverEmailAdr__c;
        toMailList.add(ra.ShippingRecieverEmailAdr__c);
 
        // 原应答沟通信息
//        String oldShippingNotes = ra.ShippingNotes__c;
        // 现应答沟通信息
//        String URL = baseUrl + '/production/' + ra.Id;
        String temp = '';
        temp += ra.ShippingNotes__c;
/***
        if (oldShippingNotes != null && oldShippingNotes.trim().length() > 0) {
            temp += '\n\n' + oldShippingNotes;
        } else {
            //于2016-07-01加入<a></a>
            temp += '\n\n备品借出申请链接: ' + baseUrl + '/' + ra.Id +' ';
        }
***/
        try {
            // 更新应答沟通
            ra.ShippingNotes__c = temp;
 
    //        update ra;
    //        ra.ShippingNotes__c = '';
 
            //Create a dummy instance of outbound email object
    //        Messaging.SingleEmailMessage message= new Messaging.SingleEmailMessage();
            //set a roolback point
    //        Savepoint sp = Database.setSavepoint();
            //set desired email template id
    //        message.templateId = 'xxxxxx';
            //set the target object record id(this is mandatory while using email template)
    //        Id recId = [select Id from Contact where Email != null limit 1].id;
    //        message.targetObjectId = recId;
            //fire dummy email
    //        messaging.sendEmail(new Messaging.SingleEmailMessage[] {message});
            //roll back to savepoint so that email is not sent
    //        Database.rollback(sp);
            //Create insatnce of actual email
            Messaging.SingleEmailMessage messageNEW= new Messaging.SingleEmailMessage();
            //get the bode from above dummy instance and set it to your actual email
            //messageNEW.HTMLBody = message.getHtmlBody();
              messageNEW.HTMLBody = temp;
              messageNEW.Subject = '发货提醒:订单'+ ra.ContractCode__c;
            //messageNEW.PlainTextBody = temp;
            //set desired email addresses
            messageNEW.setCharset('UTF-8');
            messageNEW.toAddresses = toMailList;
 //           messageNEW.ccAddresses = ccMailList;
            //send the mail
            messaging.sendEmail(new Messaging.SingleEmailMessage[] {messageNEW});
            ra.ShippingNotes__c = '';
 
 
        } catch (Exception ex) {
            system.debug('=====' + ex.getMessage());
            hasError = true;
            ApexPages.addMessages(ex);
            return null;
        }
 
        return null;
    }
}