Skip to content

Commit

Permalink
Add toLabel function (#43)
Browse files Browse the repository at this point in the history
This fixes issue #41
  • Loading branch information
XiaoyangYan authored and HenryRLee committed Jan 15, 2020
1 parent a0860c4 commit 5a7ceda
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/classes/Query.cls
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ public class Query {
return this;
}

public Query toLabel(String field) {
functionFieldList.add(new FunctionFieldTuple(TO_LABEL, field));
return this;
}

public Query toLabel(String field, String alias) {
functionFieldList.add(new FunctionFieldTuple(TO_LABEL, field, alias));
return this;
}

public Query max(String field) {
functionFieldList.add(new FunctionFieldTuple(MAX, field));
return this;
Expand Down Expand Up @@ -1545,6 +1555,7 @@ public class Query {
private final String SUM = 'SUM';
private final String COUNT = 'COUNT';
private final String COUNT_DISTINCT = 'COUNT_DISTINCT';
private final String TO_LABEL = 'TOLABEL';

// Object Schema variables
private String objectName;
Expand Down
25 changes: 25 additions & 0 deletions src/classes/QueryTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,31 @@ public class QueryTest {
System.assertEquals(result.get('expr5'), 16);
}

@isTest
static void toLabelTest() {
Account acc1 = new Account(Name = 'New Account 1');

insert acc1;

Opportunity opp = new Opportunity(
AccountId = acc1.Id,
Name = 'New Opportunity',
CloseDate = Date.today().addDays(3),
TotalOpportunityQuantity = 10,
StageName = 'New');
insert opp;

List<Opportunity> opp0 = new Query('Opportunity').
selectField('Id').
toLabel('StageName').addConditionEq('Id', opp.Id).run();
System.assertEquals(opp0[0].get('StageName'), 'New');

List<Opportunity> opp1 = new Query('Opportunity').
selectField('Id').
toLabel('StageName', 'myStageName').addConditionEq('Id', opp.Id).run();
System.assertEquals(opp1[0].get('myStageName'), 'New');
}

@isTest
static void groupByTest() {
Account acc1 = new Account(Name = 'Account 1', Rating = '1', NumberOfEmployees = 1);
Expand Down

0 comments on commit 5a7ceda

Please sign in to comment.