1

While trying to deploy apex changes to prod I receive an error for the MyProfilePageController class. I found https://developer.salesforce.com/forums/?id=906F000000099W9IAI. This is an issue from 2010, and the community says nuke it or fix it, ourselves because salesforce isn't going to. I would like to avoid deleting the class and would rather remediate the issue.

The Error when trying to push changes to prod is:

System.QueryException: List has no rows for assignment to SObject Class.MyProfilePageController.testSave: line 78, column 1

Your code coverage is 36%. You need at least 75% coverage to complete this deployment.

The Apex class in prod:

/**
 * An apex class that keeps updates of a portal user in sync with its corresponding contact.
   Guest users are never able to access this page.
 */
public class MyProfilePageController {

    private User user;
    private boolean isEdit = false;

    public User getUser() {
        return user;
    }

    public MyProfilePageController() {
        user = [SELECT id, email, username, usertype, communitynickname, timezonesidkey, languagelocalekey, firstname, lastname, phone, title,
                street, city, country, postalcode, state, localesidkey, mobilephone, extension, fax, contact.email
                FROM User
                WHERE id = :UserInfo.getUserId()];
        // guest users should never be able to access this page
        if (user.usertype == 'GUEST') {
            throw new NoAccessException();
        }
    }

    public Boolean getIsEdit() {
        return isEdit;
    }

    public void edit() {
        isEdit=true;
    }    

    public void save() {
        if (user.contact != null) {              
            setContactFields(user.contact);
        }

        try {
            update user;
            if (user.contact != null) { 
                update user.contact;
            }
            isEdit=false;
        } catch(DmlException e) {
            ApexPages.addMessages(e);
        }
    }

    public PageReference changePassword() {
        return Page.ChangePassword;
    }

    public void cancel() {
        isEdit=false;
        user = [SELECT id, email, username, communitynickname, timezonesidkey, languagelocalekey, firstname, lastname, phone, title,
                street, city, country, postalcode, state, localesidkey, mobilephone, extension, fax, contact.email
                FROM User
                WHERE id = :UserInfo.getUserId()];
    }

    private void setContactFields(Contact c) {
        c.title = user.title;
        c.firstname = user.firstname;
        c.lastname = user.lastname;
        c.email = user.email;
        c.phone = user.phone;
        c.mobilephone = user.mobilephone;
        c.fax = user.fax;
        c.mailingstreet = user.street;
        c.mailingcity = user.city;
        c.mailingstate = user.state;
        c.mailingpostalcode = user.postalcode;
        c.mailingcountry = user.country;
    }

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

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

}

After some changes, I am now at:

Your code coverage is 58%. You need at least 75% coverage to complete this deployment.

    /**
 * An apex class that updates portal user details.
   Guest users are never able to access this page.
 */
public with sharing class MyProfilePageController {
    private User user;
    private boolean isEdit = false;
    public User getUser() {
        return user;
    }
    public MyProfilePageController() {
        user = [SELECT id, email, username, usertype, communitynickname, timezonesidkey, languagelocalekey, firstname, lastname, phone, title,
                street, city, country, postalcode, state, localesidkey, mobilephone, extension, fax, contact.email
                FROM User
                WHERE id = :UserInfo.getUserId()];
        // guest users should never be able to access this page
        if (user.usertype == 'GUEST') {
            throw new NoAccessException();
        }
    }
    public Boolean getIsEdit() {
        return isEdit;
    }
    public void edit() {
        isEdit=true;
    }
    public void save() {
        try {
            update user;
            isEdit=false;
        } catch(DmlException e) {
            ApexPages.addMessages(e);
        }
    }
    public PageReference changePassword() {
        return Page.ChangePassword;
    }
    public void cancel() {
        isEdit=false;
        user = [SELECT id, email, username, communitynickname, timezonesidkey, languagelocalekey, firstname, lastname, phone, title,
                street, city, country, postalcode, state, localesidkey, mobilephone, extension, fax, contact.email
                FROM User
                WHERE id = :UserInfo.getUserId()];
    }    
}

I would appreciate some help getting to that golden 75+%.

  • 2
    you need to create user in test class and run as with that user – Santanu Boral May 14 '18 at 5:28
  • 1
    A good way to view what is covered vs. what not is on the developer console. The tool marks the non-covered lines in red. So once you have that detail, you can target to then cover all those lines with valid unit test cases. Run your test class in one of your sandboxes to be able to get this detail. – Jayant Das May 14 '18 at 13:17
  • 1
    If you can edit this class, I'd add a @testSetup method to create a new user, which meets the query requirements. Otherwise, I'd delete the class from your org & create my own version of it, by copying and pasting the code & using the same names, and add the same @testSetup method. – battery.cord May 14 '18 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.