function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kevin Collins 9Kevin Collins 9 

Issue with Trailhead challenge - Implement a basic Domain class and Apex trigger?

Challenge Not yet complete... here's what's wrong: 
The 'Accounts' class 'onApplyDefaults' method does not appear to be setting the Description field to 'Domain classes rock!' correctly for a default value.
public override void onApplyDefaults() 
	{
        List<Account> updatedAccounts = new List<Account>();
        
        for (Account acct : (List<Account>)Records) {
                acct.Description = 'Domain classes rock!';
                updatedAccounts.add(acct);
        }
        
        fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(new Schema.SObjectType[] { Account.SObjectType });
        uow.registerDirty(updatedAccounts);
        uow.commitWork();
    }

Any recommendations or suggestions would be greatly appreciated.
Suley KaboreSuley Kabore
Hello Kevin,
I passed this challenge without the code related to the fflib_SObjectUnitOfWork class. So the lines 3, 7, and [10-12] are not needed. I guess this part is done behind the sceen in the fflib_SObjectDomain. Unless there is another problem in the trigger itself or somewhere else, making these changes shoud get you through.
have a good one
Kevin Collins 9Kevin Collins 9
Thanks Suley,

I changed my code but for some reason I'm getting the same error.
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() 
	{
        
        for (Account acct : (List<Account>)Records) {
                acct.Description = 'Domain classes rock!';
        }
    }

    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);
    }
 
}

Challenge Not yet complete... here's what's wrong: 
The 'Accounts' class 'onApplyDefaults' method does not appear to be setting the Description field to 'Domain classes rock!' correctly for a default value.
Suley KaboreSuley Kabore
Hello again!
Here is the code I passed the challenge with:

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)
        {
            if(acc.Description == null)
            {
            acc.Description = 'Domain classes rock!';
            }
        }
    }
    
    public override void handleBeforeInsert()
    {
        onApplyDefaults();
    }
    
    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);
    }
    

}

trigger AccountsTrigger on Account ( 
    after delete, after insert, after update, after undelete, before delete, before insert, before update) 
 {
     //Creates Domain class instance and calls appropriate methods
     fflib_SObjectDomain.triggerHandler(Accounts.class);
}
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for same issue
1) https://developer.salesforce.com/forums/?id=9060G000000XgMoQAK
2) https://developer.salesforce.com/forums/?id=9060G000000XgPsQAK
 
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();
    }

Let us know if this will help you

 
m pandeym pandey
Account look Up        & standrd..
Project  Look Up            custom..

then form__c(custom object) has the Account & Project lookup in  form__c object..

if account lookup is selected ABC record 
then Project lookup should show associted or related records in lookup

this functionality should be in lookup and pick list....
so please send the code as  early as possible please
 
Kevin Collins 9Kevin Collins 9
I finally passed the challenge using a fresh org. I'd like to thank each of you for your assistance.
Niklas Hillgren 9Niklas Hillgren 9
There should be no to use the fflib_SObjectUnitOfWork onBeforeUpdate.
Naresh RamisettyNaresh Ramisetty
Trailhead Challange - Implement a basic Domain class and Apex trigger

AccountsTrigger : Use below code

trigger AccountsTrigger on Account (after delete, after insert, after update, after undelete, before delete, before insert, before update) {
    // Creates Domain class instance and calls appropriate methods
    fflib_SObjectDomain.triggerHandler(Accounts.class);
}

Accounts Class : Use below code

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() 
    {
        for (Account acct : (List<Account>)Records) {
            acct.Description = 'Domain classes rock!';
        }
    }
    
    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);
    }
}
Paras PolraParas Polra
trigger AccountsTrigger on Account (after delete, after insert, after update, after undelete, before delete, before insert, before update) {
    fflib_SObjectDomain.triggerHandler(Accounts.class);
}
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.
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(){
        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) {
        for(Account acc: (List<Account>)Records) {
           	Account oldVal = (Account)existingRecords.get(acc.Id);
            String s = oldVal.Description; 
            acc.AnnualRevenue = (acc.Description).getLevenshteinDistance(s);
        }    
    }
    
}

 
GopalGuptaGopalGupta
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() {
        for (Account acct:(List<Account>)Records) {
            acct.Description = 'Domain classes rock!';
        }
    }
        
    public override void onBeforeUpdate(Map<Id, sObject>existingRecords) {
        String rock = 'Domain classes rock!';
        List<Account> updatedAccounts = new List<Account>();
        for (Account acct:(List<Account>) Records) {
            acct.AnnualRevenue = rock.getLevenshteinDistance(acct.Description);
            updatedAccounts.add(acct);
        }
        
        fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(new Schema.SObjectType[] {Account.SObjectType});
        uow.registerDirty(updatedAccounts);
    }    
    
}
 
trigger AccountsTrigger on Account (before insert, after delete, after insert, after update, after undelete, before delete, before update) {
	fflib_SObjectDomain.triggerHandler(Accounts.class);
}

Try this
Lisa HooperLisa Hooper
I reviewed the code and all the comments in this discussion and compared it against what I have. I am still getting this error:
The 'Accounts' class 'onApplyDefaults' method does not appear to be setting the Description field to 'Domain classes rock!' correctly for a default value.

Accounts.apxc
//1. Create a basic Domain class named Accounts that extends fflib_SObjectDomain.

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);}}    
      
//2. Implement defaulting logic that executes when a record is inserted and sets the Description field to the value Domain classes rock!
    
    public override void onApplyDefaults() {
        for (Account a:(List<Account>)Records){
            a.Description = 'Domain classes rock!';}}       

//3. 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.

    public override void onBeforeUpdate(Map<Id, sObject>existingRecords){
        
        String r = 'Domain classes rock!';
        List <Account> updatedAccounts = new List<Account>();
        for (Account a: (List<Account>)Records) {
            a.AnnualRevenue = r.getLevenshteinDistance(a.Description);
            updatedAccounts.add(a);}   
    
    	fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(new Schema.SObjectType[] {Account.SObjectType});
    	uow.registerDirty(updatedAccounts);}
                                                            
}
AccountsTrigger.apxt
//Create a trigger named AccountsTrigger for Account that calls the fflib_SObjectDomain triggerHandler method for all trigger methods.

trigger AccountsTrigger on Account (before insert, before delete, before update, after delete, after insert, after update, after undelete) {
	fflib_SObjectDomain.triggerHandler(Accounts.class);}
What am I missing here?

 
Lisa HooperLisa Hooper
I was unable to delete my response above - please ignore post. I figured out the issue.