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
Ryan NeelRyan Neel 

Error with Apply Unit of Work Principles Trailhead module.

I've tried two bits of code now and I keep getting the error:

Challenge not yet complete in My Trailhead Playground 3
The 'challengeComplete' method in the 'UnitOfWorkTest' class has not successfully passed all tests. Ensure that you run the tests and it passes successfully before attempting this challenge again.


Here's the code I'm trying to run:

//Code attempt #2
public class UnitOfWorkTest {
    public static void challengeComplete() {
        fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(
            new Schema.SObjectType[]{
                Account.SObjectType,
                Contact.SObjectType, 
                Note.SObjectType});
        for (Integer i = 0; i < 100; i++) {
            Account acc = new Account(Name = 'TestAcc' + i);
            uow.registerNew(acc);
            for (Integer j = 0; j < 5; j++) {
                Contact c = new Contact(LastName = 'TestContact' + i + '_' + j);
                uow.registerNew(c, Contact.AccountId, acc);
                Note n = new Note(Title = 'TestNote' + i + '_' + j, Body = 'Test note body.');
                uow.registerNew(n, Note.ParentId, acc);
            }
        }
        uow.commitWork();
        System.assertEquals(100, [SELECT Id from Account].size());
        System.assertEquals(500, [SELECT Id from Contact].size());
        System.assertEquals(500, [SELECT Id From Note].size());
    }
}
/*My Original Code, attempt #1
public class UnitOfWorkTest {
    public static void challengeComplete(){
        //Create the unit of work
        fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(
            new Schema.SObjectType[]{
                Account.SObjectType,
                Contact.SObjectType,
                Note.SObjectType
            }
        );
        //Do Some Work!
        for(Integer a=[select id from account].size(); a<100; a++){
            Account acc = new Account();
            acc.Name = 'UoW Test Name' + a;
            uow.registerNew(acc);
            for(Integer c=0; c<5; c++){
                Contact cont = new Contact();
                cont.FirstName = 'James ';
                cont.LastName = acc.name + c;
                uow.registernew(cont, Contact.AccountId, acc);
                Note nt = new Note();
                nt.title = 'UoW Note #' + c;
                nt.Body = 'UoW Body ' + c;
                uow.registernew(nt, Note.ParentId, acc);
            }
        }
        //Commit the work to the database
        uow.commitWork();
        
        System.assertEquals(100, [select id from account].size());
        System.assertEquals(500, [select id from contact].size());
        System.assertEquals(500, [select id from note].size());
    }
}
*/
Best Answer chosen by Ryan Neel
Agustin BAgustin B
Hi, try this and execute the test before verifying the challengue is complete:
@IsTest
private class UnitOfWorkTest {

    private static List<Schema.SObjectType> MY_SOBJECTS =
            new Schema.SObjectType[]{
                    Account.SObjectType,
                    Contact.SObjectType,
                    Note.SObjectType
            };

    @IsTest static void challengeComplete() {
        fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(MY_SOBJECTS);

        for (Integer i = 0; i < 100; i++) {
            Account a = new Account(Name = 'Test' + i);
            uow.registerNew(a);

            for (Integer j = 0; j < 5; j++) {
                Contact c = new Contact(LastName = 'Test' + String.fromCharArray(new List<Integer>{
                        65 + i
                }));
                uow.registerNew(c, Contact.AccountId, a);

                Note n = new Note();
                n.Body = 'Test' + String.fromCharArray(new List<Integer>{
                        65 + i
                });
                n.Title = 'Test' + String.fromCharArray(new List<Integer>{
                        65 + i
                });
                uow.registerRelationship(n, Note.ParentId, a);
                uow.registerNew(n, Note.ParentId, a);
            }
        }

        Test.startTest();
        uow.commitWork();
        Test.stopTest();

        System.assertEquals(100, [SELECT Id FROM Account].size());
        System.assertEquals(500, [SELECT Id FROM Contact].size());
        System.assertEquals(500, [SELECT Id FROM Note].size());
    }
}

If it helps please like and mark as correct as it may help others asking the same.

All Answers

Agustin BAgustin B
Hi, try this and execute the test before verifying the challengue is complete:
@IsTest
private class UnitOfWorkTest {

    private static List<Schema.SObjectType> MY_SOBJECTS =
            new Schema.SObjectType[]{
                    Account.SObjectType,
                    Contact.SObjectType,
                    Note.SObjectType
            };

    @IsTest static void challengeComplete() {
        fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(MY_SOBJECTS);

        for (Integer i = 0; i < 100; i++) {
            Account a = new Account(Name = 'Test' + i);
            uow.registerNew(a);

            for (Integer j = 0; j < 5; j++) {
                Contact c = new Contact(LastName = 'Test' + String.fromCharArray(new List<Integer>{
                        65 + i
                }));
                uow.registerNew(c, Contact.AccountId, a);

                Note n = new Note();
                n.Body = 'Test' + String.fromCharArray(new List<Integer>{
                        65 + i
                });
                n.Title = 'Test' + String.fromCharArray(new List<Integer>{
                        65 + i
                });
                uow.registerRelationship(n, Note.ParentId, a);
                uow.registerNew(n, Note.ParentId, a);
            }
        }

        Test.startTest();
        uow.commitWork();
        Test.stopTest();

        System.assertEquals(100, [SELECT Id FROM Account].size());
        System.assertEquals(500, [SELECT Id FROM Contact].size());
        System.assertEquals(500, [SELECT Id FROM Note].size());
    }
}

If it helps please like and mark as correct as it may help others asking the same.
This was selected as the best answer
AbhishekAbhishek (Salesforce Developers) 
Hi Ryan,

If you still face the issue in trailhead.

Please report it here,

https://trailhead.salesforce.com/help?support=home#

https://trailhead.salesforce.com/help

So that our trailhead support engineers will look into it and get back to you.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Regards,
Salesforce Support
Ryan NeelRyan Neel
@Agustin B

I tried that but am now getting this error when trying to run it:

Line: 1, Column: 1
Type is not visible: UnitOfWorkTest
Agustin BAgustin B
Try using public instead of private on the definitions