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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use self::salesforce_create_response::SalesforceCreateResponse;
use self::sobject_create_result::SObjectsCreateResult;
use super::SObjectsBuilder;
use crate::RestApi;
use crate::errors::Error;
use crate::rest_api::HTTPMethod;
use serde::Serialize;
use serde::de::DeserializeOwned;
use std::fmt::Debug;
mod salesforce_create_response;
mod sobject_create_result;
impl<'a> SObjectsBuilder<'a> {
/// Creates a single new record in a Salesforce with the provided values.
///
/// # Generic
/// - `T`: A type that implements the `Serialize` trait, representing the parameters for the new record.
///
/// # Parameters
/// - `sobject_type`: A string slice that holds the API name of the Salesforce object (e.g., "Account", "Contact").
/// - `record`: An instance of type `T` containing the details of the record to be created.
///
/// # Returns
/// - `Result<T, Error>`:
/// - On success, returns the updated instance of type 'T' containing the Salesforce record id.
/// - On failure, returns an `Error` detailing what went wrong during the request.
///
/// # Errors
/// This function returns an `Error` in the following cases:
/// - If the Salesforce client fails to resolve the base path.
/// - If the HTTP POST request to the Salesforce API fails.
/// - If the response contains invalid JSON or an error from Salesforce.
///
/// # Example
/// ```rust
/// use rustsf::{RestApi, SalesforceCredentials};
/// use serde_json::json;
///
/// #[tokio::main]
/// async fn main() {
/// let credentials = SalesforceCredentials::sfdx_url("force://PlatformCLI::token@example.com").unwrap();
/// let api = RestApi::new(credentials).await.unwrap();
///
/// match api.build()
/// .sobjects()
/// .create("Account", json!({"Name": "Acme"}))
/// .send()
/// .await {
/// Ok(response) => println!("Record ID: {:?}", response.take_id()),
/// Err(error) => println!("Error creating account: {:?}", error),
/// }
/// }
/// ```
///
/// # Notes
/// - Ensure that the `sobject_type` matches the correct API name of the Salesforce object.
/// - The `record` object should conform to the structure expected by the Salesforce API for the specified object.
///
/// # Dependencies
/// - Ensure the `T` type implements the `Serialize` trait (usually through a derived implementation).
///
/// # See
/// https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_sobject_basic_info_post.htm
pub fn create<T: Debug + DeserializeOwned + Serialize>(
&'a self,
sobject_type: &str,
record: T,
) -> SObjectCreateBuilder<'a, T> {
SObjectCreateBuilder::new(sobject_type.to_string(), record, self)
}
}
impl RestApi {
/// Creates a single new record in a Salesforce with the provided values.
///
/// # Generic
/// - `T`: A type that implements the `Serialize` trait, representing the parameters for the new record.
///
/// # Parameters
/// - `sobject_type`: A string slice that holds the API name of the Salesforce object (e.g., "Account", "Contact").
/// - `record`: An instance of type `T` containing the details of the record to be created.
///
/// # Returns
/// - `Result<T, Error>`:
/// - On success, returns the updated instance of type 'T' containing the Salesforce record id.
/// - On failure, returns an `Error` detailing what went wrong during the request.
///
/// # Errors
/// This function returns an `Error` in the following cases:
/// - If the Salesforce client fails to resolve the base path.
/// - If the HTTP POST request to the Salesforce API fails.
/// - If the response contains invalid JSON or an error from Salesforce.
///
/// # Example
/// ```rust
/// use rustsf::{RestApi, SalesforceCredentials};
/// use serde_json::json;
///
/// #[tokio::main]
/// async fn main() {
/// let credentials = SalesforceCredentials::sfdx_url("force://PlatformCLI::token@example.com").unwrap();
/// let api = RestApi::new(credentials).await.unwrap();
///
/// match api.create("Account", json!({"Name": "Acme"})).await {
/// Ok(response) => println!("Created record with ID: {:?}", response.id),
/// Err(err) => eprintln!("Failed to create record: {:?}", err),
/// }
/// }
/// ```
///
/// # Notes
/// - Ensure that the `sobject_type` matches the correct API name of the Salesforce object.
/// - The `record` object should conform to the structure expected by the Salesforce API for the specified object.
///
/// # Dependencies
/// - Ensure the `T` type implements the `Serialize` trait (usually through a derived implementation).
///
/// # See
/// <https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_sobject_basic_info_post.htm>
pub async fn create<T: Debug + DeserializeOwned + Serialize>(
&self,
sobject_type: &str,
record: T,
) -> Result<SalesforceCreateResponse, Error> {
Ok(self
.build()
.most_recently_used_header(false)
.sobjects()
.create(sobject_type, record)
.send()
.await?
.take_response())
}
}
pub struct SObjectCreateBuilder<'a, T: Debug + DeserializeOwned + Serialize> {
builder: &'a SObjectsBuilder<'a>,
record: T,
sobject_type: String,
}
impl<'a, T: Debug + DeserializeOwned + Serialize> SObjectCreateBuilder<'a, T> {
pub(crate) fn new(sobject_type: String, record: T, builder: &'a SObjectsBuilder) -> SObjectCreateBuilder<'a, T>
where
T: Debug + Serialize,
{
Self {
builder,
record,
sobject_type,
}
}
/// Sends the creation request to the Salesforce REST API
///
/// This asynchronous method sends an HTTP POST request to create a new Salesforce
/// sObject of the specified type. The `record` provided in the request payload
/// contains the fields and values to be sent to Salesforce. The result of the
/// request is processed, and a structured `SObjectsCreateResult` is returned.
///
/// # Example
/// ```rust
/// use rustsf::{RestApi, SalesforceCredentials};
/// use serde_json::json;
///
/// #[tokio::main]
/// async fn main() {
/// let credentials = SalesforceCredentials::sfdx_url("force://PlatformCLI::token@example.com").unwrap();
/// let api = RestApi::new(credentials).await.unwrap();
///
/// match api.build()
/// .sobjects()
/// .create("Account", json!({"Name": "Acme"}))
/// .send()
/// .await {
/// Ok(response) => println!("Record ID: {:?}", response.take_id()),
/// Err(error) => println!("Error creating account: {:?}", error),
/// }
/// }
/// ```
pub async fn send(self) -> Result<SObjectsCreateResult<T>, Error> {
let http_response = self
.builder
.send(HTTPMethod::POST, format!("{}", self.sobject_type), &self.record)
.await?;
Ok(SObjectsCreateResult::new(
self.record,
RestApi::handle_json_response(http_response).await?,
))
}
}