+ 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.
​​​​​​​