+ Start a Discussion
Prateek Singh SengarPrateek Singh Sengar 

Issues with Apply Domain Layer Principles in Apex

I am stuck in the "Implement a basic Domain class and Apex trigger" challenge. The challenge states
Implement a basic Domain class and accompanying Apex trigger with default and update logic based upon domain conventions.

Create a basic Domain class named Accounts that extends fflib_SObjectDomain.
Create a trigger named AccountsTrigger for Account that calls the fflib_SObjectDomain triggerHandler method for all trigger methods.
Implement defaulting logic that executes when a record is inserted and sets the Description field to the value Domain classes rock!
Implement update logic that calculates the Levenshtein distance between the phrase Domain classes rock! and whatever the contents of the Description field is when an Account is updated. Use the Apex String method getLevenshteinDistance(stringToCompare) and store the result in the Annual Revenue field.



I am getting the following 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.

My Domain class code is
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 onApplyDefaults()
    {
        // Apply defaults to account
        for(Account acc: (List<Account>) Records)
        {
            acc.Description = 'Domain classes rock!';
        }
    }
    
    public override void onbeforeUpdate(Map<Id,SObject> existingRecords)
    {
        updateAnnualRevenue(existingRecords);    
        
    }
    
    private void updateAnnualRevenue(Map<Id,SObject> existingRecords)
    {
        //calculate value and assign to annualrev
		String defaultStr = 'Domain classes rock!';
        for(Account acc: (List<Account>)Records)
        {
            Account oldVal = (Account)existingRecords.get(acc.Id);
            String s = oldVal.Description; 
            acc.AnnualRevenue = defaultStr.getLevenshteinDistance(s);
        }    
    }
    
}

 
Best Answer chosen by Prateek Singh Sengar
Prateek Singh SengarPrateek Singh Sengar
Figured out the error

The code should compare the old value of description to the new value and not the phrase mentiond in the challege.

The working code is 
private void updateAnnualRevenue(Map<Id,SObject> existingRecords)
    {
        //calculate value and assign to annualrev
        for(Account acc: (List<Account>)Records)
        {
           	Account oldVal = (Account)existingRecords.get(acc.Id);
            String s = oldVal.Description; 
            acc.AnnualRevenue = (acc.Description).getLevenshteinDistance(s);
        }    
    }

 

All Answers

Alain CabonAlain Cabon
Hello,  

Try inversing the fields of your calculation.  
 
acc.AnnualRevenue = s.getLevenshteinDistance(defaultStr);

Regards
Alain
Prateek Singh SengarPrateek Singh Sengar
Tried that as well didnt worked -------- Original Message -------- Subject: (Salesforce Developers): New reply to your question.
Prateek Singh SengarPrateek Singh Sengar
Figured out the error

The code should compare the old value of description to the new value and not the phrase mentiond in the challege.

The working code is 
private void updateAnnualRevenue(Map<Id,SObject> existingRecords)
    {
        //calculate value and assign to annualrev
        for(Account acc: (List<Account>)Records)
        {
           	Account oldVal = (Account)existingRecords.get(acc.Id);
            String s = oldVal.Description; 
            acc.AnnualRevenue = (acc.Description).getLevenshteinDistance(s);
        }    
    }

 
This was selected as the best answer
Alain CabonAlain Cabon
Ok I passed this module too yesterday and I used the phrase mentiond in the challege like you for the first try (just inversing).
The controls by the robot of the trailhead should be based on unit testing (?).