@@ -122,24 +122,26 @@ public String getAccessTokenRetryableMode() throws ExecutionException, RetryExce
122122 }
123123
124124 /**
125- * Fetch the list of records from ServiceNow table.
125+ * Fetches a list of records from a ServiceNow table.
126126 *
127- * @param tableName The ServiceNow table name
128- * @param valueType The value type
129- * @param startDate The start date
130- * @param endDate The end date
131- * @param offset The number of records to skip
132- * @param limit The number of records to be fetched
133- * @return The list of Map; each Map representing a table row
127+ * @param tableName The name of the ServiceNow table.
128+ * @param valueType The type of value to retrieve (e.g., actual or display).
129+ * @param filterQuery An encoded query to filter the records. For example, to filter records created
130+ * between two dates, the query would be: {@code
131+ * sys_created_onBETWEENjavascript:gs.dateGenerate
132+ * ('2023-01-01','start')@javascript:gs.dateGenerate('2023-01-31','end')}
133+ * @param offset The number of records to skip from the beginning of the result set.
134+ * @param limit The maximum number of records to retrieve.
135+ * @return A list of maps, where each map represents a record from the table.
136+ * @throws ServiceNowAPIException If an error occurs while fetching the records.
134137 */
135138 public List <Map <String , String >> fetchTableRecords (
136- String tableName ,
137- SourceValueType valueType ,
138- String startDate ,
139- String endDate ,
140- int offset ,
141- int limit )
142- throws ServiceNowAPIException {
139+ String tableName ,
140+ SourceValueType valueType ,
141+ String filterQuery ,
142+ int offset ,
143+ int limit )
144+ throws ServiceNowAPIException {
143145 ServiceNowTableAPIRequestBuilder requestBuilder = new ServiceNowTableAPIRequestBuilder (
144146 this .conf .getRestApiEndpoint (), tableName , false , schemaType )
145147 .setExcludeReferenceLink (true )
@@ -150,39 +152,16 @@ public List<Map<String, String>> fetchTableRecords(
150152 requestBuilder .setOffset (offset );
151153 }
152154
153- applyDateRangeToRequest (requestBuilder , startDate , endDate );
155+ if (!Util .isNullOrEmpty (filterQuery )) {
156+ requestBuilder .setQuery (filterQuery );
157+ }
154158
155159 String accessToken = getAccessToken ();
156160 requestBuilder .setAuthHeader (accessToken );
157161 RestAPIResponse apiResponse = executeGetWithRetries (requestBuilder .build ());
158162 return parseResponseToResultListOfMap (apiResponse .getResponseBody ());
159163 }
160164
161- private void applyDateRangeToRequest (ServiceNowTableAPIRequestBuilder requestBuilder , String startDate ,
162- String endDate ) {
163- String dateRange = generateDateRangeQuery (startDate , endDate );
164- if (!Strings .isNullOrEmpty (dateRange )) {
165- requestBuilder .setQuery (dateRange );
166- }
167- }
168-
169- private String generateDateRangeQuery (String startDate , String endDate ) {
170- if (Util .isNullOrEmpty (startDate ) || Util .isNullOrEmpty (endDate )) {
171- return "" ;
172- }
173-
174- String dateRange = "" ;
175- try {
176- String createdOnDateRange = String .format (DATE_RANGE_TEMPLATE , FIELD_CREATED_ON , startDate , endDate );
177- String updatedOnDateRange = String .format (DATE_RANGE_TEMPLATE , FIELD_UPDATED_ON , startDate , endDate );
178- dateRange = String .format ("%s^OR%s" , createdOnDateRange , updatedOnDateRange );
179- } catch (Exception e ) {
180- LOG .error ("Error in generateDateRangeQuery, hence ignoring the date range" , e );
181- }
182-
183- return dateRange ;
184- }
185-
186165 private int getRecordCountFromHeader (RestAPIResponse apiResponse ) {
187166 String headerValue = apiResponse .getHeaders ().get (ServiceNowConstants .HEADER_NAME_TOTAL_COUNT );
188167 return Strings .isNullOrEmpty (headerValue ) ? 0 : Integer .parseInt (headerValue );
@@ -226,18 +205,16 @@ private String getErrorMessage(String responseBody) {
226205 *
227206 * @param tableName The ServiceNow table name
228207 * @param valueType The value type
229- * @param startDate The start date
230- * @param endDate The end date
231208 * @param offset The number of records to skip
232209 * @param limit The number of records to be fetched
233210 * @return The list of Map; each Map representing a table row
234211 */
235212 public List <Map <String , String >> fetchTableRecordsRetryableMode (String tableName , SourceValueType valueType ,
236- String startDate , String endDate , int offset ,
213+ String filterQuery , int offset ,
237214 int limit ) throws ServiceNowAPIException {
238215 final List <Map <String , String >> results = new ArrayList <>();
239216 Callable <Boolean > fetchRecords = () -> {
240- results .addAll (fetchTableRecords (tableName , valueType , startDate , endDate , offset , limit ));
217+ results .addAll (fetchTableRecords (tableName , valueType , filterQuery , offset , limit ));
241218 return true ;
242219 };
243220
@@ -415,15 +392,18 @@ private Schema prepareSchemaWithMetadataAPI(RestAPIResponse restAPIResponse, Lis
415392 }
416393
417394 /**
418- * Get the total number of records in the table
395+ * Gets the total number of records in the table based on the provided date range.
419396 *
420- * @param tableName ServiceNow table name for which record count is fetched.
421- * @return the table record count
422- * @throws ServiceNowAPIException
397+ * @param tableName ServiceNow table name for which record count is fetched
398+ * @param startDate Start date to use for filtering records
399+ * @param endDate End date to use for filtering records
400+ * @return The total number of records
401+ * @throws ServiceNowAPIException If an error occurs while fetching the record count
423402 */
424- public int getTableRecordCount (String tableName )
403+ public int getTableRecordCount (String tableName , @ Nullable String startDate , @ Nullable String endDate )
425404 throws ServiceNowAPIException {
426- return getTableRecordCount (tableName , getAccessToken ());
405+ String filterQuery = Util .generateDateRangeQuery (startDate , endDate );
406+ return getTableRecordCountUsingFilterQuery (tableName , getAccessToken (), filterQuery );
427407 }
428408
429409 /**
@@ -434,13 +414,18 @@ public int getTableRecordCount(String tableName)
434414 * @return the table record count
435415 * @throws ServiceNowAPIException
436416 */
437- public int getTableRecordCount (String tableName , String accessToken ) throws ServiceNowAPIException {
417+ public int getTableRecordCountUsingFilterQuery (String tableName , String accessToken ,
418+ @ Nullable String filterQuery )
419+ throws ServiceNowAPIException {
438420 ServiceNowTableAPIRequestBuilder requestBuilder = new ServiceNowTableAPIRequestBuilder (
439421 this .conf .getRestApiEndpoint (), tableName , false , schemaType )
440422 .setExcludeReferenceLink (true )
441423 .setDisplayValue (SourceValueType .SHOW_DISPLAY_VALUE )
442424 .setLimit (1 );
443425 RestAPIResponse apiResponse = null ;
426+ if (!Util .isNullOrEmpty (filterQuery )) {
427+ requestBuilder .setQuery (filterQuery );
428+ }
444429 requestBuilder .setResponseHeaders (ServiceNowConstants .HEADER_NAME_TOTAL_COUNT );
445430 requestBuilder .setAuthHeader (accessToken );
446431 apiResponse = executeGetWithRetries (requestBuilder .build ());
0 commit comments