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
use crate::RestApi;
use crate::errors::Error;
use crate::rest_api::HTTPMethod;
use crate::rest_api::builder::sobjects::SObjectsBuilder;
use crate::rest_api::builder::sobjects::updated::sobject_updated_response::SObjectUpdatedResponse;
use chrono::{DateTime, Utc};
mod sobject_updated_response;
impl RestApi {
/// sObject Get Updated
///
/// Retrieves the list of individual records that have been updated within the given timespan
/// for the specified object (`sobject_name`).
///
/// # Parameters
/// - `sobject_name`: The API name of the Salesforce object whose updated records are being queried.
/// - `start_date`: Starting date/time (Coordinated Universal Time (UTC)—not local— timezone) of the
/// timespan for which to retrieve the data. The API ignores the seconds portion of the
/// specified dateTime value (for example, 12:30:15 is interpreted as 12:30:00 UTC).
/// The date and time must be formatted as described in Valid Date and DateTime Formats.
/// The date/time value for start must chronologically precede end.
/// This parameter should be URL-encoded.
/// - `end_date`: Ending date/time (Coordinated Universal Time (UTC)—not local— timezone) of the
/// timespan for which to retrieve the data. The API ignores the seconds portion of the
/// specified dateTime value (for example, 12:35:15 is interpreted as 12:35:00 UTC).
/// The date and time must be formatted as described in Valid Date and DateTime Formats.
/// This parameter should be URL-encoded.
///
/// # Returns
/// - `Ok(UpdatedSObjectsResponse)`: A structured response containing the details of the updated objects.
/// - `Err(Error)`: An error if the request fails or the API response is invalid.
///
/// # Notes
/// - Deleted records are written to a delete log which this resource accesses. A background
/// process that runs every two hours purges records that have been in an organization's
/// delete log for more than two hours if the number of records is above a certain limit.
/// Starting with the oldest records, the process purges delete log entries until the delete
/// log is back below the limit. This is done to protect Salesforce from performance issues
/// related to massive delete logs
/// - Information on deleted records is returned only if the current session user has access to them.
/// - Results are returned for no more than 15 days previous to the day the call is executed
/// (or earlier if an administrator has purged the Recycle Bin).
/// - There is a limit of 600,000 IDs returned from this resource.
/// If more than 600,000 IDs are found, EXCEEDED_ID_LIMIT is returned.
/// You can correct the error by choosing start and end dates that are closer together.
///
/// # Example
/// ```rust
/// use rustsf::{RestApi, SalesforceCredentials};
/// use anyhow::Result;
/// use chrono::{Utc, TimeZone};
///
/// #[tokio::main]
/// async fn main() {
/// let credentials = SalesforceCredentials::sfdx_url("force://PlatformCLI::token@example.com").unwrap();
/// let api = RestApi::new(credentials).await.unwrap();
///
/// let start_date = Utc.with_ymd_and_hms(2023, 1, 1, 0, 0, 0).unwrap();
/// let end_date = Utc.with_ymd_and_hms(2023, 1, 31, 23, 59, 59).unwrap();
/// let result = api.sobject_get_updated("Account", &start_date, &end_date).await;
///
/// match result {
/// Ok(response) => {
/// println!("Updated objects: {:?}", response);
/// }
/// Err(e) => {
/// eprintln!("Error fetching updated objects: {:?}", e);
/// }
/// }
/// }
/// ```
///
/// # See
/// <https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_getupdated.htm>
pub async fn sobject_get_updated(
&self,
sobject_name: &str,
start_date: &DateTime<Utc>,
end_date: &DateTime<Utc>,
) -> Result<SObjectUpdatedResponse, Error> {
self.build()
.sobjects()
.updated(sobject_name, start_date, end_date)
.send()
.await
}
}
impl<'a> SObjectsBuilder<'a> {
pub fn updated(
&self,
sobject_name: &str,
start_date: &DateTime<Utc>,
end_date: &DateTime<Utc>,
) -> SObjectUpdatedBuilder<'_> {
SObjectUpdatedBuilder::new(self, sobject_name, start_date, end_date)
}
}
pub struct SObjectUpdatedBuilder<'a> {
builder: &'a SObjectsBuilder<'a>,
sobject_name: String,
start_date: String,
end_date: String,
}
impl<'a> SObjectUpdatedBuilder<'a> {
pub(crate) fn new(
builder: &'a SObjectsBuilder,
sobject_type: &str,
start_date: &DateTime<Utc>,
end_date: &DateTime<Utc>,
) -> SObjectUpdatedBuilder<'a> {
Self {
builder,
sobject_name: sobject_type.to_string(),
start_date: start_date.to_string(),
end_date: end_date.to_string(),
}
}
pub async fn send(&self) -> Result<SObjectUpdatedResponse, Error> {
let url = format!("{}/updated", self.sobject_name);
let start_formatted = DateTime::parse_from_rfc3339(&self.start_date)
.map(|dt| dt.with_timezone(&Utc))
.or_else(|_| self.start_date.parse::<DateTime<Utc>>())
.map(|dt| dt.format("%Y-%m-%dT%H:%M:%S.000Z").to_string())?;
let end_formatted = DateTime::parse_from_rfc3339(&self.end_date)
.map(|dt| dt.with_timezone(&Utc))
.or_else(|_| self.end_date.parse::<DateTime<Utc>>())
.map(|dt| dt.format("%Y-%m-%dT%H:%M:%S.000Z").to_string())?;
let params = vec![
("start".to_string(), start_formatted),
("end".to_string(), end_formatted),
];
let response = self.builder.send(HTTPMethod::GET, url, ¶ms).await?;
RestApi::handle_json_response(response).await
}
}