Last active
August 7, 2023 04:33
-
-
Save A-pZ/ce6906b16527802dcf4dd191c4daef48 to your computer and use it in GitHub Desktop.
Salesforceの取引先を新規登録する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.apz.salesforcesample.model; | |
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | |
import com.fasterxml.jackson.databind.annotation.JsonNaming; | |
import lombok.Getter; | |
import lombok.NoArgsConstructor; | |
import lombok.Setter; | |
import lombok.ToString; | |
/** | |
* Salesforce取引先。 | |
* 取引先情報の中から必要なものだけ設定しています。 | |
* SalesforceのJSONはPascalCase(jacksonではUpperCamelCase)なため、UpperCamelCaseStrategyを設定しています。 | |
*/ | |
@JsonNaming(PropertyNamingStrategies.UpperCamelCaseStrategy.class) | |
@NoArgsConstructor @Getter @Setter @ToString | |
public class Company { | |
private String name; | |
private String id; | |
private String recordTypeId; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 com.github.apz.salesforcesample.model.RegisterResult; | |
import lombok.AllArgsConstructor; | |
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; | |
@Repository | |
@AllArgsConstructor | |
@Slf4j | |
public class CompanyRepository { | |
SalesforceProperties salesforceProperties; | |
WebClient salesforceWebClient; | |
public RegisterResult register(Company company, AuthenticationResult authenticationResult) { | |
RegisterResult result = salesforceWebClient.post() // 登録はPOSTを用いる | |
.uri(salesforceProperties.getApplicationPath()+ "/sobjects/Account") // 取引先=Account | |
.header("Authorization", authenticationResult.bearerToken()) // 認証済みのトークンをHTTPヘッダに追加 | |
.contentType(MediaType.APPLICATION_JSON) | |
.body(Mono.just(company), Company.class) // HTTPリクエストボディに追加する取引先 | |
.retrieve() | |
.bodyToMono(RegisterResult.class) // 登録結果 | |
.onErrorResume(WebClientResponseException.class, // Salesforceがエラーを返したときのエラー情報 | |
exception -> Mono.just(new RegisterResult() {{ | |
setErrors( | |
exception.getResponseBodyAs(new ArrayList<ErrorDetail>().getClass()) | |
); | |
}})) | |
.block(); | |
log.info("result: {}", result); | |
return result; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.apz.salesforcesample.model; | |
import lombok.Getter; | |
import lombok.NoArgsConstructor; | |
import lombok.ToString; | |
import java.util.List; | |
@NoArgsConstructor @Getter @ToString | |
public class ErrorDetail { | |
private String message; | |
private String errorCode; | |
private List<String> fields; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.apz.salesforcesample.model; | |
import lombok.Getter; | |
import lombok.NoArgsConstructor; | |
import lombok.ToString; | |
import java.util.List; | |
@NoArgsConstructor | |
@ToString | |
@Getter | |
public class RegisterResult { | |
private String id; | |
private boolean success; | |
List<ErrorDetail> errors; | |
public void setErrors(List<ErrorDetail> errors) { | |
this.errors = errors; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment