Skip to content

Instantly share code, notes, and snippets.

@A-pZ
Last active March 12, 2024 02:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save A-pZ/0bc5664a3a1a4fc796f0cd33000f3696 to your computer and use it in GitHub Desktop.
Save A-pZ/0bc5664a3a1a4fc796f0cd33000f3696 to your computer and use it in GitHub Desktop.
Bulk API 2.0を使ったクエリの登録
package com.github.apz.salesforcesample.repository;
import com.github.apz.salesforcesample.config.SalesforceProperties;
import com.github.apz.salesforcesample.model.AuthenticationResult;
import com.github.apz.salesforcesample.model.Company;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Repository;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import java.io.IOException;
import java.util.List;
@Repository
@AllArgsConstructor
@Slf4j
public class CompanyQueryRepository {
SalesforceProperties salesforceProperties;
WebClient salesforceWebClient;
public String createBulkQuery(AuthenticationResult authenticationResult) {
BulkQuery bulkQuery = new BulkQuery("SELECT Id, name, RecordTypeId FROM Account", "query");
String result = salesforceWebClient.post()
.uri(salesforceProperties.getApplicationPath()+ "/jobs/query")
.header("Authorization", authenticationResult.bearerToken())
.contentType(MediaType.APPLICATION_JSON)
.body(Mono.just(bulkQuery), BulkQuery.class).retrieve()
.bodyToMono(String.class)
.block();
log.info("result: {}", result);
return result;
}
@Getter @AllArgsConstructor
public static class BulkQuery {
private String query;
private String operation;
}
}
package com.github.apz.salesforcesample.repository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import spock.lang.Specification
@SpringBootTest
class SalesforceBulkQueryJobRegisterTest extends Specification{
@Autowired
SalesforceAuthentication authentication
@Autowired
CompanyQueryRepository repository
def "クエリジョブ作成"() {
when:
def authenticationResult = authentication.authentication()
String id = repository.createBulkQuery(authenticationResult)
then:
noExceptionThrown()
id
}
}
INFO 24164 --- [ Test worker] c.g.a.s.r.CompanyQueryRepository :
result: {
"id":"7500l000008qaGLAAY",
"operation":"query",
"object":"Account",
"createdById":"0050l000007kmChAAI",
"createdDate":"2023-08-07T09:08:25.000+0000",
"systemModstamp":"2023-08-07T09:08:25.000+0000",
"state":"UploadComplete",
"concurrencyMode":"Parallel",
"contentType":"CSV",
"apiVersion":53.0,
"lineEnding":"LF",
"columnDelimiter":"COMMA"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment