Skip to content

Commit

Permalink
Merge pull request #1500 from JeroenSfdc/patch-8
Browse files Browse the repository at this point in the history
Update QueryWithLimit.cls
  • Loading branch information
alexed1 authored Dec 21, 2023
2 parents 5d31ea4 + f4ee814 commit 202fc44
Showing 1 changed file with 19 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,45 +1,43 @@
public with sharing class QueryWithLimit {


/* Class definition for throwing custom exceptions */
public class FlowApexActionException extends Exception{}

@InvocableMethod(label='Query N records [USF Collection Processor]' description='Returns a list of N records, where is N specified by a user as a flow input.' category='Util' iconName='resource:CollectionProcessorsSVG:colproc')
public static list<QueryResults> getNrecords(QueryParameters[] queryParams){
if(queryParams[0].numberOfRecords >= 50000)
@InvocableMethod(Label='Query N records [USF Collection Processor]' Description='Returns a list of N records, where is N specified by a user as a flow input.' Category='Util' IconName='resource:CollectionProcessorsSVG:colproc')
public static List<QueryResults> getNrecords(QueryParameters[] queryParams){

if (queryParams[0].numberOfRecords >= 50000) {
throw new FlowApexActionException('You cannot query more than 50000 records.');
}

list<QueryResults> result = new list<QueryResults>();
string query = 'Select '+ queryParams[0].fieldsToQuery + ' FROM ' + queryParams[0].objectApiName + ' LIMIT ' + queryParams[0].numberOfRecords;
try{
sObject[] recordList = database.query(query);
//system.debug(recordList);
List<QueryResults> result = new List<QueryResults>();
String query = 'SELECT '+ queryParams[0].fieldsToQuery + ' FROM ' + queryParams[0].objectApiName + ' LIMIT ' + queryParams[0].numberOfRecords;
try {
SObject[] recordList = Database.query(query);
QueryResults qr = new QueryResults();
qr.records = recordList;
result.add(qr);
} catch(Exception e){
} catch(Exception e) {
throw e;
}
return result;
}

/* Input parameters for the Apex action */
public class QueryParameters{
@InvocableVariable(label='Api name of the Object' required = true )
public string objectApiName;
@InvocableVariable(Label='Api name of the Object' Required = true )
public String objectApiName;

@InvocableVariable(label='API names of the fields to query(Comma separated)' required = true)
public string fieldsToQuery;
@InvocableVariable(Label='API names of the fields to query(Comma separated)' Required = true)
public String fieldsToQuery;

@InvocableVariable(label='Number of records to query' required = true)
public integer numberOfRecords;
@InvocableVariable(Label='Number of records to query' Required = true)
public Integer numberOfRecords;
}

/* Output parameters of the Apex action */
public class QueryResults{
@InvocableVariable(label='List of records')
public sObject[] records;
@InvocableVariable(Label='List of records')
public SObject[] records;
}

}
}

0 comments on commit 202fc44

Please sign in to comment.