088_BatchApex_Callout
global class BatchSync implements Database.Batchable<sObject>, Database.AllowsCallouts { public String query = 'Select ID, Name from Account';
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator(query);
} global void execute(Database.BatchableContext BC, List<Account> records) {
String endpoint; for ( integer i = 0; i< records.size(); i++ ){
try {
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
// Set values to Params endpoint = 'Your endpoint'; req.setHeader('Authorization', header);
req.setHeader('Content-Type', 'application/json');
req.setEndpoint(endpoint);
req.setMethod('POST');
req.setBody('Information you wanna send');
req.setCompressed(true); // This is imp according to SF, but please check if
// the webservice accepts the info. Mine did not :P
// Had to set it to false if (!Test.isRunningTest()) {
res = http.send(req);
String sJson = res.getBody();
System.debug('Str:' + res.getBody());
}
// now do what u want to with response.
}
catch (Exception e) {
System.debug('Error:' + e.getMessage() + 'LN:' + e.getLineNumber() );
}
}
} global void finish(Database.BatchableContext BC){
}
}
BatchSync BS = new BatchSync();
Database.executeBatch(BS,10); // you can also do less than 10
You can also take help from these link
1-https://developer.salesforce.com/forums/?id=906F0000000kK6VIAU
2-http://www.forcedisturbances.com/2012/03/caching-data-from-googles-geocoding.html
http://www.platforce.org/batch-apex.html
https://www.biswajeetsamal.com/blog/batch-apex-with-webservice-callout/
global class batchAccountUpdate implements Database.Batchable<sObject>, Database.AllowsCallouts{
global Database.QueryLocator start(Database.BatchableContext BC)
{
string obj = 'ACA';
String query = 'Select Id, CreatedDate, CreatedBy.Name, Attest_ID__c from Case where Ticket_Type__c = :obj';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> scope)
{
List<Voice_File_Loader__c> searchVFL = [Select Id, Call_Date_Time__c, End_Window__c, Agent_Name__c, Voice_File_Location__c from Voice_File_Loader__c];
for (Account checkCase : scope){
for (Voice_File_Loader__c matchVFL :searchVFL){
boolean after = (checkCase.CreatedDate >= matchVFL.Call_Date_Time__c);
boolean before = (checkCase.CreatedDate <= matchVFL.End_Window__c);
boolean timeCheck = (after && before);
boolean nameCheck = (checkCase.CreatedBy.Name.equalsIgnoreCase(matchVFL.Agent_Name__c));
if (timeCheck && nameCheck){
Attachment att = new Attachment();
Http binding = new Http();
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint(matchVFL.Voice_File_Location__c);
HttpResponse res = binding.send(req);
Blob b = res.getbodyasblob();
att.name = 'Voice Attestation.wav';
att.body = b;
att.parentid = checkCase.Id;
system.debug('#############'+ att);
insert att;
delete matchVFL;
}
}
}
}
global void finish(Database.BatchableContext BC)
{
}
batchAccountUpdate a = new batchAccountUpdate();
database.executebatch(a,10);
global class AccountBatchApex implements Database.Batchable<sObject>, Database.AllowsCallouts{
global Database.QueryLocator start(Database.BatchableContext bc){
String soqlQuery = 'SELECT Name, AccountNumber, Type From Account';
return Database.getQueryLocator(soqlQuery);
}
global void execute(Database.BatchableContext bc, List<Account> scope){
for (Account acc : scope){
if(acc.Type.equals('Customer - Direct')){
try{
HttpRequest request = new HttpRequest();
HttpResponse response = new HttpResponse();
Http http = new Http();
String username = 'YourUsername';
String password = 'YourPassword';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
request.setHeader('Authorization', authorizationHeader);
request.setHeader('Content-Type', 'application/json');
request.setEndpoint('Your Endpoint URL');
request.setMethod('POST');
request.setBody('Information to Send');
response = http.send(request);
if (response.getStatusCode() == 200) {
String jsonResponse = response.getBody();
System.debug('Response-' + jsonResponse);
}
}
catch(Exception){
System.debug('Error-' + e.getMessage());
}
}
}
}
global void finish(Database.BatchableContext bc){
}
}
https://blogs.absyz.com/2017/12/11/making-callouts-with-batch-apex-for-data-of-over-12-mb/
global class mybatchclass Implements Database.Batchable <sObject>,database.stateful,database.allowcallouts {
//variable to be used in SOQL
public Date today = system.today();
public set<ID> finalaccounts = new set<Id>();
public String SOQL = 'SELECT Id, Name, Type from Account where CreatedDate =: today' ;
// Start Method of Batch class
global Database.queryLocator start(Database.BatchableContext bc){
// Query the records
return Database.getQueryLocator(SOQL);
}
// Execute Method of Batch class
global void execute(Database.BatchableContext bc, List<Account> accountlist) {
// Iterate over the list of contacts and get their Account ids
for(Account cc:accountlist){
finalaccounts.add(cc.Id);
}
}
// Finish Method of Batch class. This is where you make the callout
global void finish(Database.BatchableContext bc) {
attachfiles.attachfilestoaccount(finalaccounts);
//make the callout here for getting the attachments data of size greater than 12 MB
}
}
// You can invoke the batch class from the anonymous debug window with the below syntax
mybatchclass mb = new mybatchclass();
Database.executebatch(mb);
public class attachfiles(){
public static void attachfilestoaccount(set<id> listaccount)
{
// First set the callout parameters such as the authToken, Endpoint and the accessToken
String authToken = 'test authorization token';
String authEndpoint = 'test authEndpoint';
String calloutEndpoint = 'test calloutEndpoint';
String accessToken = 'test_act';
// Now make the HTTP callout
Http h1 = new Http();
HttpRequest hreq2 = new HttpRequest();
String dealId = '';
hreq2.setEndPoint(calloutEndpoint);
hreq2.setMethod('POST');
hreq2.setHeader('Content-type','application/xml');
hreq2.setHeader('Authorization','Bearer'+' '+accessToken);
hreq2.setTimeout(30000);
hreq2.setBodyAsBlob(Blob.valueOf('testClass'));
// Get the response which contains the attachment
HttpResponse res = h1.send(hreq2);
List<Attachment> atLst = new List<Attachment>();
for(Account acc:listaccount){
Attachment att1 = new Attachment();
att1.Body = Blob.valueOf(res.getbody());
att1.Name = String.valueOf('Response.txt');
att1.ParentId = acc.Id;
atLst.add(att1);
}
// Finally, insert the list
if(atLst.size()> 0)
insert atLst;
}
}
http://mysfdc1.blogspot.com/2017/07/how-to-make-http-callouts-in-batch-class.html
global class BatchSync implements Database.Batchable<sObject>, Database.AllowsCallouts {
public String query = 'Select ID, Name from Account';
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> records) {
String endpoint;
for ( integer i = 0; i< records.size(); i++ ){
try {
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
// Set values to Params
endpoint = 'Your endpoint';
req.setHeader('Authorization', header);
req.setHeader('Content-Type', 'application/json');
req.setEndpoint(endpoint);
req.setMethod('POST');
req.setBody('Information you wanna send');
req.setCompressed(true); // This is imp according to SF, but please check if
// the webservice accepts the info. Mine did not :P
// Had to set it to false
if (!Test.isRunningTest()) {
res = http.send(req);
String sJson = res.getBody();
System.debug('Str:' + res.getBody());
}
// now do what u want to with response.
}
catch (Exception e) {
System.debug('Error:' + e.getMessage() + 'LN:' + e.getLineNumber() );
}
}
}
global void finish(Database.BatchableContext BC){
}
}
关于 异步处理的一些限制 : http://www.salesforcenextgen.com/asynchronous-apex/
088_BatchApex_Callout的更多相关文章
随机推荐
- 逐步讲解如何在 Proteus 中新建工程
前言 Proteus 新建工程虽然不难,但对于电子小白来说可能便成了学习路上的绊脚石,本篇我将逐步讲解如何在 Proteus 中新建工程. 最新版 Proteus 8.15 最新版 Proteus 8 ...
- angular使用_HttpClient或者Fetch发送POST/GET请求下载/上传文件
一:下载文件写法 1.post请求_HttpClient写法. myTest() { const params = { aa: "aa", bb: "bb" } ...
- 电脑微信小程序抓包
电脑微信小程序抓包 在渗透的过程中,对于网站找不出什么漏洞的时候我们就可以,对目标进行小程序和公众号漏洞的发掘 0x01 设置微信代理使用Burp抓取数据包 发现我们抓取的数据包很多都是乱码 Prox ...
- 【FAQ】申请运动健康服务验证环节常见问题及解答
华为 HMS Core 运动健康服务(HUAWEI Health Kit)提供原子化数据开放.应用在获取用户数据授权后,可通过接口访问运动健康数据,对用户数据进行读写等操作,为用户提供运动健康类数据服 ...
- Quartz.Net 官方教程 Best Practices
最佳实践 JobDataMap 建议只存储基本数据(含String),避免序列化问题 作业执行期间,JobDetail和Trgger的底层共用一个JobDataMap 实例,因此Trigger的数据会 ...
- Vue 01 简介
1 官网 1)英文官网: https://vuejs.org/ 2)中文官网: https://cn.vuejs.org/ 2 介绍与描述 1) 动态构建用户界面的渐进式 JavaScript ...
- JAVA虚拟机08--垃圾回收--HotSpot的算法实现细节
1 stop the world 2 减少stop the world的时间-OopMap 3 OopMap数据结构的维护-安全点-安全区域 3.1安全点 3.2在垃圾回收时如何让所有线程到达最近的安 ...
- JDK、tomcat、MySQL5.7安装教程
JDK自定义安装 一.安装JDK.JRE 1.在E盘下建立一个java文件夹,在java文件夹下分别建立jdk和jre文件夹 2.双击安装包 3.点击下一步,更改安装路径,安装到第一步创建好的jdk文 ...
- Vue前后端交互、生命周期、组件化开发
目录 Vue前后端交互.生命周期.组件化开发 一.Vue用axios与后端交互 二.Vue的生命周期 三.组件化开发 Vue前后端交互.生命周期.组件化开发 一.Vue用axios与后端交互 如果 ...
- 安卓开发 java控制UI
创建布局管理器对象 设置背景 设置活动界面 按钮事件 按钮显示