+ Start a Discussion
goabhigogoabhigo 

Error while deploying to production

We are receiving an error while deploying to production:
MyProfilePageController.testSave System.QueryException: List has no rows for assignment to SObject
Class.MyProfilePageController.testSave: line 78, column 35

MyProfilePageController class is automatically generated, I mean we didn't write code for this.

 

 

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

 

 

The line highlighted in red is the error line.

 

Please help me.

 

BomanBoman

Check what custom fields have been added to your User object in the Production instance. You might be missing the UserType field. Add that, just like you have in your sandbox, and then attempt to deploy.

forecast_is_cloudyforecast_is_cloudy

From the error it seems that either Customer Portal hasn't been enabled in the Production Org, or that no CP users have been created yet. You should modify the test class to insert a test CP user before making the SOQL statement that's thowing the error. You always want to create test data in your test classes - never reply on data existing in the target Org (as the current test class seeems to be doing).

Hope this helps.

bryan.gilbertbryan.gilbert

Hi

 

I had the same problem and our org has salesforce premium support. Here is the solution I got from them. It works.

 

In MyProfilePageController method testSave() you need to fix the first line and comment out a assertion.  Here is the code I put into our instance (we are not yet using portal but plan on releasing it soon)

 

 

//===============================

// If portal is enabled

// Change

// User existingPortalUser = [SELECT id, profileId, userRoleId FROM User WHERE UserRoleId <> null

// AND UserType='CustomerSuccess' LIMIT 1];

//

//to

// 

// User existingPortalUser = [SELECT id, profileId, userRoleId FROM User WHERE UserRoleId <> null

// AND isportalEnabled=True AND isActive=true LIMIT 1];

//

//===============================

// If portal is not enabled

// Change

// to

 

User existingPortalUser = [SELECT id, profileId, userRoleId FROM User WHERE UserRoleId <> null

AND isActive=true LIMIT 1];

 

//and comment out/delete the assert below.

// System.assert(existingPortalUser.Contact.fax == randFax)        


 

gwpgwp

Note the above fix doesn't work unless you have a sandbox populated with data. For those of us wihtout premium support here's another way:

 

 I wanted to avoid

1) Removing the class and page

2) Activating the customer portal and adding a fake user

3) Changing any code provided by SF

 

In the end I had to do (3). There is no option (SF fix your broken product!!!)

 

I altered the test method on the problem class to pass at 78% coverage. Note that it is now NOT a proper unit test, only a workaround, but it is better written than the original as it doens't require existing data.

 

Step 1: Backup & comment out the problem test method in MyProfilePageController.cls - You are altering SF provided code and who knows what may happen in the future....

Step 3: Replace with the following

 

 

static testMethod void testSave() {
    	//Note this has been hacked as the old test crashed and prevented Apex releases
        //This solution will work if you have the code deployed but no partner or customer portal users activated (=cause of the original test failure)
 	//Not perfect, but it works.... Please post improvements to this thread!
        
 	//An Account
	Account ac = new Account();
	ac.Name = 'Test Entity';
    	insert ac;
		
        //A Contact
        Contact c = new Contact();
        c.FirstName = 'unit';
        c.LastName = 'test';
        c.Email = 'me@myorg.org';
        c.AccountId = ac.Id;
        insert c;        
        
        String randFax = Math.rint(Math.random() * 1000) + '5551234';

MyProfilePageController controller = new MyProfilePageController(); controller.save(); controller.edit(); controller.cancel(); controller.getUser().Fax = randFax; controller.setContactFields(c); }

 

 

Step4: Run tests on this script after saving. Then save again and deploy (this step depends on which environment you are working in). If you are in sandbox, deploy to server.

 

Now you should be free to develop again as normal...