Merge pull request #20 from JAertgeerts/main
Add getIdFieldByStringField()
This commit is contained in:
@@ -29,6 +29,7 @@ Protected methods
|
|||||||
- [getDoubleFieldValues](#getdoublefieldvalues)
|
- [getDoubleFieldValues](#getdoublefieldvalues)
|
||||||
- [getFieldByField](#getfieldbyfield)
|
- [getFieldByField](#getfieldbyfield)
|
||||||
- [getFieldsByField](#getfieldsbyfield)
|
- [getFieldsByField](#getfieldsbyfield)
|
||||||
|
- [getIdFieldByStringField](#getidfieldbystringfield)
|
||||||
- [getIdFieldByIdField](#getidfieldbyidfield)
|
- [getIdFieldByIdField](#getidfieldbyidfield)
|
||||||
- [getIdFieldsByIdField](#getidfieldsbyidfield)
|
- [getIdFieldsByIdField](#getidfieldsbyidfield)
|
||||||
- [getIdFieldValues](#getidfieldvalues)
|
- [getIdFieldValues](#getidfieldvalues)
|
||||||
@@ -138,6 +139,18 @@ Contacts contacts = Contacts.newInstance(records);
|
|||||||
Map<Object, Set<Object>> contactIdByAccountId = contacts.getFieldsByField(Contact.Id, Contact.AccountId);
|
Map<Object, Set<Object>> contactIdByAccountId = contacts.getFieldsByField(Contact.Id, Contact.AccountId);
|
||||||
```
|
```
|
||||||
<br/><br/>
|
<br/><br/>
|
||||||
|
#### getIdFieldByStringField
|
||||||
|
Get a map with the string field as the key and the ID as values. Key fields containing null values are omitted.
|
||||||
|
|
||||||
|
`protected Map<String, Id> getIdFieldByStringField(Schema.SObjectField valueField, Schema.SObjectField keyField)`
|
||||||
|
|
||||||
|
_Example_
|
||||||
|
|
||||||
|
```apex
|
||||||
|
Contacts contacts = Contacts.newInstance(records);
|
||||||
|
Map<String, Id> accountIdByContactId = contacts.getIdFieldByStringField(Contact.Id, Contact.Name);
|
||||||
|
```
|
||||||
|
<br/><br/>
|
||||||
|
|
||||||
#### getIdFieldByIdField
|
#### getIdFieldByIdField
|
||||||
Get a map with the values of two Id fields. Key fields containing null values are omitted.
|
Get a map with the values of two Id fields. Key fields containing null values are omitted.
|
||||||
|
|||||||
@@ -397,6 +397,35 @@ public virtual class fflib_SObjects2
|
|||||||
return getRecords().get(0);
|
return getRecords().get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a map with the values of a String field as key and Id field
|
||||||
|
* Key fields containing null values are omitted
|
||||||
|
*
|
||||||
|
* @param valueField The Id field to use as the Value of the Map
|
||||||
|
* @param keyField The String field to use as the Key of the map
|
||||||
|
*
|
||||||
|
* @return Returns a map with the values of two fields
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* Contacts contacts = Contacts.newInstance(records);
|
||||||
|
* Map<String, Id> accountIdByContactId = contacts.getIdFieldByStringField(Contact.Id, Contact.Name);
|
||||||
|
*/
|
||||||
|
@TestVisible
|
||||||
|
protected virtual Map<String, Id> getIdFieldByStringField(Schema.SObjectField valueField, Schema.SObjectField keyField)
|
||||||
|
{
|
||||||
|
Map<String, Id> result = new Map<String, Id>();
|
||||||
|
for (SObject record : getRecords())
|
||||||
|
{
|
||||||
|
if (record.get(keyField) == null) continue;
|
||||||
|
|
||||||
|
result.put(
|
||||||
|
(String) record.get(keyField),
|
||||||
|
(Id) record.get(valueField)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a map with the values of two Id fields
|
* Get a map with the values of two Id fields
|
||||||
* Key fields containing null values are omitted
|
* Key fields containing null values are omitted
|
||||||
|
|||||||
@@ -121,6 +121,20 @@ private class fflib_SObjects2Test
|
|||||||
// todo - add assertions
|
// todo - add assertions
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@IsTest
|
||||||
|
static void itShouldReturnStringById()
|
||||||
|
{
|
||||||
|
final String accountName = 'My Account';
|
||||||
|
final Id accountId = fflib_IDGenerator.generate(Schema.Account.SObjectType);
|
||||||
|
final Account record = new Account(Id = accountId, Name = accountName);
|
||||||
|
fflib_SObjects2 domain = new fflib_SObjects2(new List<Account>{ record });
|
||||||
|
System.Test.startTest();
|
||||||
|
Map<String, Id> nameById = domain.getIdFieldByStringField(Schema.Account.Id, Schema.Account.Name);
|
||||||
|
System.Test.stopTest();
|
||||||
|
System.assert(nameById.containsKey(accountName));
|
||||||
|
System.assertEquals(accountId, nameById.get(accountName));
|
||||||
|
}
|
||||||
|
|
||||||
@IsTest
|
@IsTest
|
||||||
static void itShouldReturnStringFieldByIdField()
|
static void itShouldReturnStringFieldByIdField()
|
||||||
{
|
{
|
||||||
@@ -578,4 +592,4 @@ private class fflib_SObjects2Test
|
|||||||
clearField(Schema.Account.ShippingCountry);
|
clearField(Schema.Account.ShippingCountry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user