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
Omega3kOmega3k 

Need Help with APEX Class

Hello,

Can someone help me with the folowing error:

"Package Upload Error: There are problems that prevent this package from being uploaded."

Component Type:
Apex Class


Problem:
System.QueryException: List has no rows for assignment to SObject
Class.MyProfilePageContorller.testSave: Line 78, column 35
External entry point:

Here's the code where the error occurs:
static testMethod void testSave() {         
        // Modify the test to query for a portal user that exists in your org
        User existingPortalUser = [SELECT id, profileId, userRoleId FROM User WHERE UserRoleId <> null AND UserType='CustomerSuccess' LIMIT 1];
        System.assert(existingPortalUser != null, 'This test depends on an existing test portal user to run');

Any help is appreciated...

Thanks!

sravusravu

Could you please check the highlighted part of the query once.

 

User existingPortalUser = [SELECT id, profileId, userRoleId FROM User WHERE UserRoleId <> null AND UserType='CustomerSuccess' LIMIT 1];
System.assert(existingPortalUser != null, 'This test depends on an existing test portal user to run');

Ritesh AswaneyRitesh Aswaney

The test class is depending on existing data (the select) rather than creating its own test data. Check whether the table that is being queried, i.e. User has a record satisfying the 'where clause' criteria i.e. UserRoleId not null and UserType = 'CustomerSuccess'.

 

If a record with that criteria doesnt exist, then a quick fix would be to insert a record into the User table, so that a row will be returned to satisfty the test.

Omega3kOmega3k

Thanks a lot! That solved that problem, but another one arose further down.

 

Problem: System.AssertException: Assertion Failed

 

Here's the code:

 

static testMethod void testSave() {         
        // Modify the test to query for a portal user that exists in your org
        User existingPortalUser = [SELECT id, profileId, userRoleId FROM User WHERE UserRoleId <> null LIMIT 1];
        System.assert(existingPortalUser != null, 'This test depends on an existing test portal user to run');
        
        String randFax = Math.rint(Math.random() * 1000) + '5551234';
        
        System.runAs(existingPortalUser) {
            MyProfilePageController controller = new MyProfilePageController();
            System.assertEquals(existingPortalUser.Id, controller.getUser().Id, 'Did not successfully load the current user');
            System.assert(controller.isEdit == false, 'isEdit should default to false');
            controller.edit();
            System.assert(controller.isEdit == true);
            
            controller.cancel();
            System.assert(controller.isEdit == false);
            
            controller.getUser().Fax = randFax;
            controller.save();
            System.assert(controller.isEdit == false);
        }
        
        // verify that the user and contact were updated
        existingPortalUser = [Select id, fax, Contact.Fax from User where id =: existingPortalUser.Id];
        System.assert(existingPortalUser.fax == randFax);
        System.assert(existingPortalUser.Contact.fax == randFax);
    }

}

 

The error takes place at the highlighted line...

 

Thanks for all the help... as you can probably tell, I'm very new at this...

Imran MohammedImran Mohammed

Are you sure that the value that fax holds is equal to the value Contact.fax holds?

If not, then the assertion fails.

 

 

Omega3kOmega3k

I think that's the problem... how do I update the Contact.fax field so that it has the same value as fax?

Ritesh AswaneyRitesh Aswaney

You are initializing the randFax variable in your code -

 String randFax = Math.rint(Math.random() * 1000) + '5551234';

 

Set it to the same value as the one you have saved on your Contact record.

 

What the test is trying to do is assert that randfax is equal to the fax number on the contact.

 

The idea is to set up identical data so the tests pass...!