我正在SF中的自定义对象上创建Apex触发器。现在,我有一个对象约会,我想在保存新记录时触发标注。由于我们的内部流程,我们无需担心更新。仅新记录。
话虽如此,我已经创建了触发器和类的基础,并且它们可以正常工作。触发器运行标记为Future的类以运行异步。但是,课堂是我迷路的地方。
我想将一些变量从正在创建的约会对象中的记录传递到代码中。我的代码将HTTP POST发送给将SMS发送给客户的服务。我想使用电话号码,客户名称以及消息中包含约会日期和时间的消息。该数据存储的字段是:
i360__Prospect_Phone__c
i360__Correspondence_Name__c
i360__Start__c
i360__Start_Time__c
例如,我需要电话号码和姓名才能转移到下面的班级代码中。至于消息,我想以包含变量的字符串发送出去。示例:“您好i360__Correspondence_Name__c,您与COMPANY的约会已安排在i360__Start__c的i360__start_Time__c上。如有任何疑问,请回复或致电。”
这是我的触发代码:
trigger sendtext on i360__Appointment__c (after insert) {
System.debug('Making future call to update account');
for (i360__Appointment__c app : Trigger.New) {
PodiumText.updateAccount(app.Id, app.Name);
}
}
这是我的班级代码:
public class PodiumText {
//Future annotation to mark the method as async.
@Future(callout=true)
public static void updateAccount(String id, String name) {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.podium.com/api/v2/conversations');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
request.setHeader('Accept', 'application/json');
request.setHeader('Authorization', 'IDSTRING');
request.setBody('{"customerPhoneNumber":"PHONENUMBER","message":"Testing","locationId":"49257","customerName":"CORRESPONDENCENAME"}');
HttpResponse response = http.send(request);
// Parse the JSON response
if (response.getStatusCode() != 201) {
System.debug('The status code returned was not expected: ' +
response.getStatusCode() + ' ' + response.getStatus());
} else {
System.debug(response.getBody());
}
}
}
任何帮助都会很棒。
理想情况下,您一次不会将此@future方法称为一个帐户。每个事务限制为50个调用(https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm寻找“带有将来注释的方法的最大数量”。因此,当您从UI插入1条记录时,您现在所拥有的将可以完美地工作,但是如果您处理海量数据,则将导致严重的失败。加载。
因此您可以调用它一次,但将数据与列表(数组)一起传递。我们可以只传递记录ID,然后在方法本身中可以查询您需要的字段。或者,您可以在触发器中构建消息的文本,然后将字符串列表传递给@future方法。这是个人喜好,我认为仅传递记录ID会更清洁。这样触发器就不在乎到底会发生什么,它只是“哟,根据此数据发送您需要的任何消息”。并且实际的标注代码保持在一起(端点和消息构建),因此,如果您要更改某些内容,则只需1个文件即可完成。
类似的东西(消息的顺序重要吗?如果不是,它甚至会更简单)
trigger sendtext on i360__Appointment__c (after insert) {
List<Id> ids = new List<Id>();
for (i360__Appointment__c app : trigger.New) {
ids.add(app.Id);
}
System.debug('Making future call to update account');
PodiumText.updateAccounts(ids);
}
然后
@future(callout=true)
public static void updateAccounts(List<Id> ids){
Map<Id, i360__Appointment__c> appointments = new Map<Id, i360__Appointment__c>([SELECT Id, Name,
i360__Prospect_Phone__c, i360__Correspondence_Name__c, i360__Start__c, i360__Start_Time__c
FROM i360__Appointment__c
WHERE Id IN :ids]);
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.podium.com/api/v2/conversations');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
request.setHeader('Accept', 'application/json');
request.setHeader('Authorization', 'IDSTRING');
for(Id i : ids){
i360__Appointment__c appointment = appointments.get(i);
Map<String, String> message = new Map<String, String>{
'customerPhoneNumber' => appointment.i360__Prospect_Phone__c,
'message' => 'Testing',
'locationId' => '49257',
'customerName' => appointment.i360__Correspondence_Name__c
};
String messageJson = JSON.serialize(message);
System.debug(messageJson);
request.setBody(messageJson);
HttpResponse response = http.send(request);
// Parse the JSON response
if (response.getStatusCode() != 201) {
System.debug('The status code returned was not expected: ' +
response.getStatusCode() + ' ' + response.getStatus());
} else {
System.debug(response.getBody());
}
}
}
如果此方法有效(我还没有检查它是否可以编译并且没有安装此应用),那么最后一部分将是放置真实消息,而不是“ Testing”。这部分取决于这些字段的确切含义(开始时间是文本/选择列表字段还是时间字段?)。有一个有趣的String.format()方法,但我们可以使其保持简单(感觉就像我已经向您展示了很多内容一样,查询,地图,JSON序列化...)
String message = 'Hello ' + appointment.i360__Correspondence_Name__c + ', your appointment with COMPANY has been scheduled on ' + appointment.i360__Start__c + ' at ' + appointment.i360__start_Time__c + '. Please respond or call if you have any questions.';
(实际上我给您的内容也不是100%完美。标注总数不得超过100。因此,如果您要加载海量数据,则需要重新编写此代码,以便将来进行2次调用100记录每个或10 * 20 ...)