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
Terri T JilesTerri T Jiles 

Apply Unit of Work Principles in Apex - Test Passing, but Still Error When Submitting

Hi All,

I receive the following error when attempting to check the challenge for Apply Unit of Work

Challenge Not yet complete... here's what's wrong: 
The 'challangeComplete' 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.

However, when I run my code in Developer Console, my test passes.  Any ideas on the problem?  Here is the code:

@isTest
public class UnitOfWorkTest {
    @isTest 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 a = new Account(Name= 'Test' + i);
            uow.registerNew(a);
            
            for (Integer j=0 ; j<5 ; j++) {
                Contact c = new Contact(LastName = 'Test'+i + ' ' +j);
                uow.registerNew(c, Contact.AccountId, a);
                
                Note n = new Note(Body='Test '+i + '' + j, Title='Test'+i+j);
                uow.registerRelationship(n, Note.ParentId, a);
                uow.registerNew(n, Note.ParentId, a);
            }
        }

        uow.commitWork();
 
        fflib_SObjectUnitOfWork uow2 = new fflib_SObjectUnitOfWork(
            new Schema.SObjectType[]{
                Account.SObjectType,
                Contact.SObjectType,
                Note.SObjectType
            }
        );        
        for (Account a : [SELECT Id, Name, (SELECT Id, LastName FROM Contacts), (SELECT Id, ParentId, Title, Body FROM Notes) FROM Account]) {
            a.Name = 'Test';
            uow2.registerDirty(a);
            
            Integer i = 0;
            for (Contact c : a.Contacts) {
                c.LastName = 'Test';
                uow2.registerDirty(c);
                
                a.Notes[i].Body='Test';
                uow2.registerDirty(a.Notes[i]);
                i++;
            }
        }        
        
        test.startTest();
        uow2.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());
    }
}
Best Answer chosen by Terri T Jiles
Stephen Stanley 2Stephen Stanley 2
You haven't followed the instructions exactly.  Check the name of your test method against the requirements of the "challange"

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi,

Please consider below points.

1.Please check if you have connected to same DE org where you have done your work in the trailhead.To do this click on" launch your hands on org" and select the DE org or trailhead playground where you have your work and then check challenge.

 OR


Go to Trailhead Profile -- settings -- make the DE org which you have worked as default then check the challenge.


Trailhead released with new updates. Please refer below link how to take challenges in trailhead.

https://force.desk.com/customer/portal/articles/2643793-trailhead-profile-signup-login-faq?b_id=13478

 
Hope this helps you!

If this helps you please mark it as solved.

Thanks and Regards
Sandhya
Terri T JilesTerri T Jiles
Hi Sandhya,

thanks for your response.  Unfortunately, this doesn't resolve the issue.  I still get the error.  I re-wrote the test class and still get the same error.

My test class passes when executed in Developer Console, still.  However, the same error message persists.

This is the revised code


@isTest
public class UnitOfWorkTest {
    @isTest 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 a = new Account(Name= 'Test' + i);
            uow.registerNew(a);
            
            for (Integer j=0 ; j<5 ; j++) {
                Contact c = new Contact(LastName = 'Test'+i + ' ' +j);
                uow.registerNew(c, Contact.AccountId, a);
                
                Note n = new Note(Body='Test '+i + '' + j, Title='Test'+i+j);
                //uow.registerRelationship(n, Note.ParentId, a);
                //uow.registerNew(n, Note.ParentId, a);
                uow.registerNew(n, Note.ParentId, c);
            }
        }

        uow.commitWork();
 
        fflib_SObjectUnitOfWork uow2 = new fflib_SObjectUnitOfWork(
            new Schema.SObjectType[]{
                Account.SObjectType,
                Contact.SObjectType,
                Note.SObjectType
            }
        );        
        
        Id oldAccountId;
        Account a2;
        for (Contact c : [SELECT Id, LastName, AccountId, Account.Name, (SELECT Id, ParentId, Title, Body FROM Notes) FROM Contact Order By AccountId, Id]) {
            
            if (oldAccountId != c.AccountId) {
                oldAccountId = c.AccountId;
                a2 = new Account(Id=c.AccountId, Name='Test');
                uow2.registerDirty(a2);
            }
            
                c.LastName = 'Test';
                uow2.registerDirty(c);
                

                c.Notes[0].Body = 'Test';

                uow2.registerDirty(c.Notes[0]);

        }        
        
        test.startTest();
        uow2.commitWork();
        //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());
    }
}
Stephen Stanley 2Stephen Stanley 2
You haven't followed the instructions exactly.  Check the name of your test method against the requirements of the "challange"
This was selected as the best answer
Terri T JilesTerri T Jiles
Thank you Stephen!  I had the method name mispelled.
Jakub SchonJakub Schon

Why do you need a second uow?

I've passed challenge with this code:

@IsTest
    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());
    }

I didn't even used uow.registerRelationship. What for? Note has only one relationship and it's with acc. so uow.registerNew(n, Note.ParentId, acc) will take care of that. If Note would have another relationship, it would make sense to use registerRelationship but this is simple example so why complicate things.

(I just wasn't sure that if I have to create all these records or if there is some class/method for this challenge which would do it for me :-D )

Stephen Stanley 2Stephen Stanley 2
Read the error message very carefully. The message may appear to have a typo but it hasn't
Jess BurghJess Burgh
Hey all. I'm also getting an error message when trying to pass this challenge. Any suggestions?? Thanks!!
Terri T JilesTerri T Jiles
Put it into a list variable and then perform the DML. That is what worked for me
Francis CrumpFrancis Crump
Finally got this to work as well, just be sure to deploy the files necessary on the two buttons, and after the code from Terri is entered, give it a test run and wait about 30 seconds before trying to complete the challenge.  If you hit the complete button too soon it doesn't catch, but after a little delay it works.  Jakub's code did NOT work for me, with an error saying line 2 "void" not recognized.
Jakub SchonJakub Schon

@Francis Crump

Well I didn't paste it all.

You need to wrapp it inside @isTest public UnitOfWorkTest {

[myCode]

}

I thought it would be clear. I paste there only method. 

Sugandha Kumari 11Sugandha Kumari 11
For me deplyment fails for the 2 pre requisite libraries , how to proceed with this?
KapavariVenkatramanaKapavariVenkatramana
Realy awesome code WOrked effienciently.Thanks a lot.
Realy awesome code WOrked effienciently.Thanks a lot.
​​​​​​​
Carlos Naranjo 15Carlos Naranjo 15
This is also a way to complete this challenge: 
@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());
    }
}

 
GAURAV SETHGAURAV SETH
I am getting below error when running Unit test.
System.LimitException: Too many SOQL queries: 101
How can I overcome this ?
shikher jainshikher jain
You must run the test class before submitting the challenge.
Cecilia QuintanaCecilia Quintana
Hi all! I'm trying to pass this challenge and I got a strange Assert error ( is strange to me because I structure the code in order to attempt the correct value for each assertion eg: loop (x100 (loopx5)) = 500 records ). I don't if Is something I am missing from my code or I have to see the classes form the "Apex Mock" or "apex common" libraries I've downloaded to aim the "Unit of Work" pattern. 

My code: 
@isTest
public with sharing class UnitOfWorkTest {
    public UnitOfWorkTest() {}

    @isTest
    static void challengeComplete(){
        // Create a Unit Of Work
        fflib_SObjectUnitOfWork uow = new fflib_SObjectUnitOfWork(
            new Schema.SObjectType[] {
            Account.SObjectType,
            Contact.SObjectType,
            Note.SObjectType});
                // Do some work!
                for(Integer o=0; o<100; o++) {
                    Account acc = new Account();
                    acc.Name = 'UoW Test Name ' + o;
                    uow.registerNew(acc);
                    for(Integer i=0; i<5; i++) {
                         Contact con = new Contact();
                         con.firstName = 'test' + i;
                         con.lastName = 'tester' + i;
                         uow.registerNew(con, Contact.accountId, acc);
                         Note aNote = new Note();
                         aNote.title = 'Test Note'+ i;
                         aNote.body = 'Some body Note' + i;
                         uow.registerNew(aNote, 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());
        }
}


Error: System.AssertException: Assertion Failed: Expected: 500, Actual: 600

Note: I've tried to change the j=0 to j=1 and the assertion error is "expected=500 actual=400"


 
Kamesh SinghKamesh Singh
Follow below steps.

1.Deploy the ApexMocks open source library.
2. Deploy the Apex Common open source library. 
3. Run all the test class in your org
4. Create below UnitOfWorkTest
5. Run this calss: UnitOfWorkTest
6. Pass the challenge.


@isTest
public class UnitOfWorkTest {
    @isTest 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 a = new Account(Name= 'Test' Account + i);
            uow.registerNew(a);
            
            for (Integer j=0 ; j<5 ; j++) {
                Contact c = new Contact(LastName = 'Test Contant '+i + ' ' +j);
                uow.registerNew(c, Contact.AccountId, a);
                
                Note n = new Note(Body='Test '+i + '' + j, Title='Test Notes'+i+j);
                //uow.registerRelationship(n, Note.ParentId, a);
                //uow.registerNew(n, Note.ParentId, a);
                uow.registerNew(n, Note.ParentId, c);
            }
        }

        uow.commitWork();
 
        fflib_SObjectUnitOfWork uow2 = new fflib_SObjectUnitOfWork(
            new Schema.SObjectType[]{
                Account.SObjectType,
                Contact.SObjectType,
                Note.SObjectType
            }
        );        
        
        Id oldAccountId;
        Account a2;
        for (Contact c : [SELECT Id, LastName, AccountId, Account.Name, (SELECT Id, ParentId, Title, Body FROM Notes) FROM Contact Order By AccountId, Id]) {
            
            if (oldAccountId != c.AccountId) {
                oldAccountId = c.AccountId;
                a2 = new Account(Id=c.AccountId, Name='Test');
                uow2.registerDirty(a2);
            }
            
                c.LastName = 'Test';
                uow2.registerDirty(c);
                

                c.Notes[0].Body = 'Test';

                uow2.registerDirty(c.Notes[0]);

        }        
        
        test.startTest();
        uow2.commitWork();
        //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());
    }
}
Ajitesh Singh 19Ajitesh Singh 19
Hi,

I have followed the below steps and it worked for me :
  1. Deploy the ApexMocks open source library.
  2. Deploy the Apex Common open source library.
  3. Create the below class in your and run test once before checking the challenge
 
@isTest
public class UnitOfWorkTest {
    @isTest 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 = 'Account Name '+i);
            uow.registerNew(acc);
            for(Integer j = 0; j<5;j++){
                Contact con = new Contact(LastName = 'Contact Name '+j);
                uow.registerNew(con,Contact.AccountId, acc);                
                Note no = new Note(Body='Body No'+j,Title='Title '+j);
                uow.registerNew(no,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());
    }
}

 
p sonwanep sonwane
i am getting this error : System.ListException: List index out of bounds: 0