You need to sign in to do that
Don't have an account?

Apply Domain Layer Principles in Apex challenge
Cannot get this to work - get the error:
Challenge Not yet complete... here's what's wrong:
The 'Accounts' class 'onBeforeUpdate' method does not appear to be calculating the Levenshtein distance between the phrase default Description ‘Domain classes rock!’ and the value in the updated Description and storing the result in the Annual Revenue field correctly.
Here's my code in the Accounts class:
Challenge Not yet complete... here's what's wrong:
The 'Accounts' class 'onBeforeUpdate' method does not appear to be calculating the Levenshtein distance between the phrase default Description ‘Domain classes rock!’ and the value in the updated Description and storing the result in the Annual Revenue field correctly.
Here's my code in the Accounts class:
public class Accounts extends fflib_SObjectDomain { public Accounts(List<Account> sObjectList) { super(sObjectList); } public class Constructor implements fflib_SObjectDomain.IConstructable { public fflib_SObjectDomain construct(List<sObject> sObjectList) { return new Accounts(sObjectList); } } public override void onBeforeInsert() { List<Account> newAccounts = new List<Account>(); for (Account acct : (List<Account>) Records) { acct.Description = 'Domain classes rock!'; newAccounts.add(acct); } fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(new Schema.SObjectType[] { Account.SObjectType }); uow.registerNew(newAccounts); } public override void onBeforeUpdate(Map<Id,sObject> Records) { String rock = 'Domain classes rock!'; Set<Id> Ids = Records.keySet(); List<Account> accountList = [SELECT Id, Description, AnnualRevenue FROM Account WHERE Id IN :Ids]; List<Account> updatedAccounts = new List<Account>(); for (Account acct : accountList) { if (acct.Description != NULL) { acct.AnnualRevenue = rock.getLevenshteinDistance(acct.Description); updatedAccounts.add(acct); } } fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(new Schema.SObjectType[] { Account.SObjectType }); uow.registerDirty(updatedAccounts); } public override void onApplyDefaults() { String rock = 'Domain classes rock!'; List<Account> accountList = (List<Account>)Records; List<Account> updatedAccounts = new List<Account>(); for (Account acct : accountList) { if (acct.Description != NULL) { acct.AnnualRevenue = rock.getLevenshteinDistance(acct.Description); updatedAccounts.add(acct); } } fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(new Schema.SObjectType[] { Account.SObjectType }); uow.registerDirty(updatedAccounts); uow.commitWork(); } }
public override void onBeforeUpdate(Map<Id,sObject> existingRecords) {
String rock = 'Domain classes rock!';
List<Account> updatedAccounts = new List<Account>();
for(Account acc : (List<Account>) Records) {
acc.AnnualRevenue = rock.getLevenshteinDistance(acc.Description);
updatedAccounts.add(acc);
}
fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(new Schema.SObjectType[] { Account.SObjectType });
uow.registerDirty(updatedAccounts);
}
All Answers
Change the input parameter like
public override void onBeforeUpdate(Map<Id,sObject> existingRecords) {
Because records is as the same the property Records. Salesforce is case sensetive
public override void onBeforeUpdate(Map<Id,sObject> existingRecords) {
String rock = 'Domain classes rock!';
List<Account> updatedAccounts = new List<Account>();
for(Account acc : (List<Account>) Records) {
acc.AnnualRevenue = rock.getLevenshteinDistance(acc.Description);
updatedAccounts.add(acc);
}
fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(new Schema.SObjectType[] { Account.SObjectType });
uow.registerDirty(updatedAccounts);
}
Error: Compile Error: Cannot save a trigger during a parse and save class call at line -1 column -1
What am I missing?
Guys, we are executing a Before Trigger context here... you don't need any uow on this case.
The Before Trigger context will take care of that for you.