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
Dorian Kane 5Dorian Kane 5 

problem on test to pass and fix apex for deploy

hello i need help to fix this code to pass it on deploy

This is the apex clas i need to deploy MyProfilePage Controller

test Save

System.QueryException: List has no rows for assignment to SObject

Stack Trace: Class.MyProfilePage Controller.test Save: line 78, column 1 this thow a erron on : line 78, column 1

i have open dhe dev console and the problem shows

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

}
 

:Defining type for testMethod methods must be declared as IsTest

I dont have experient in apex and i need help

Thanks

Best Answer chosen by Dorian Kane 5
Raj VakatiRaj Vakati

You need to wrap your test method in the class like below and you cannot use method directly .. Every method must be inside the test class 

Past this code
@isTest
private class Test_DistanceClass{
  static testMethod void testSave() {         
        // Modify the test to query for a portal user that exists in your org
        
                Profile pf = [select Id,name from Profile where UserType ='CustomerSuccess' limit 1];

                
        
        UserRole obj=new UserRole(Name= 'ABC'); 
insert obj; 


Account acc1 = new Account (
        Name = 'newAcc1'
        );  
        insert acc1;
        
        Contact conCase = new Contact (
        AccountId = acc1.id,
        LastName = 'portalTestUserv1'
        );
        insert conCase;
        
        

        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User existingPortalUser=new User(firstname = 'ABC',UserRoleId = obj.Id, 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US',contactId = conCase.id

                         ProfileId = pf.Id
                        ); 
        
        
        insert existingPortalUser;
        
        
        
        String randFax = Math.rint(Math.random() * 1000) + '5551234';
        
        System.runAs(existingPortalUser) {
            MyProfilePageController controller = new MyProfilePageController();
            controller.edit();
            
            controller.cancel();
            
            controller.getUser().Fax = randFax;
            controller.save();
        }
        
        }

}



 

All Answers

Raj VakatiRaj Vakati
Change you test method like below
 
static testMethod void testSave() {         
        // Modify the test to query for a portal user that exists in your org
		
	            Profile pf = [select Id,name from Profile where UserType ='CustomerSuccess' limit 1];

				
		
		UserRole obj=new UserRole(Name= 'ABC'); 
insert obj; 


Account acc1 = new Account (
        Name = 'newAcc1'
        );  
        insert acc1;
        
		Contact conCase = new Contact (
        AccountId = acc1.id,
        LastName = 'portalTestUserv1'
        );
        insert conCase;
		
		

        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User existingPortalUser=new User(firstname = 'ABC',UserRoleId = obj.Id, 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US',contactId = conCase.id

                         ProfileId = pf.Id
                        ); 
        
        
        insert existingPortalUser;
		
		
        
        String randFax = Math.rint(Math.random() * 1000) + '5551234';
        
        System.runAs(existingPortalUser) {
            MyProfilePageController controller = new MyProfilePageController();
            controller.edit();
            
            controller.cancel();
            
            controller.getUser().Fax = randFax;
            controller.save();
        }
        
        }

 
Dorian Kane 5Dorian Kane 5

hello there 
Raj Vakati
im reciving some erron on the tes class  :Unexpected token 'void'. at Line 1

do you have any idea about this that can help me 

 

Raj VakatiRaj Vakati
Can u share the screenshot ?
Dorian Kane 5Dorian Kane 5
User-added image
Raj VakatiRaj Vakati
I need screenshot of the first few lines code ..and can u share the compplete class 
Raj VakatiRaj Vakati
You need to wrapp your test method in the class like below
@isTest
private class Test_DistanceClass{
  static testMethod void testSave() {         
        // Modify the test to query for a portal user that exists in your org
        
                Profile pf = [select Id,name from Profile where UserType ='CustomerSuccess' limit 1];

                
        
        UserRole obj=new UserRole(Name= 'ABC'); 
insert obj; 


Account acc1 = new Account (
        Name = 'newAcc1'
        );  
        insert acc1;
        
        Contact conCase = new Contact (
        AccountId = acc1.id,
        LastName = 'portalTestUserv1'
        );
        insert conCase;
        
        

        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User existingPortalUser=new User(firstname = 'ABC',UserRoleId = obj.Id, 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US',contactId = conCase.id

                         ProfileId = pf.Id
                        ); 
        
        
        insert existingPortalUser;
        
        
        
        String randFax = Math.rint(Math.random() * 1000) + '5551234';
        
        System.runAs(existingPortalUser) {
            MyProfilePageController controller = new MyProfilePageController();
            controller.edit();
            
            controller.cancel();
            
            controller.getUser().Fax = randFax;
            controller.save();
        }
        
        }

}

 
Dorian Kane 5Dorian Kane 5
User-added image
Raj VakatiRaj Vakati

You need to wrap your test method in the class like below and you cannot use method directly .. Every method must be inside the test class 

Past this code
@isTest
private class Test_DistanceClass{
  static testMethod void testSave() {         
        // Modify the test to query for a portal user that exists in your org
        
                Profile pf = [select Id,name from Profile where UserType ='CustomerSuccess' limit 1];

                
        
        UserRole obj=new UserRole(Name= 'ABC'); 
insert obj; 


Account acc1 = new Account (
        Name = 'newAcc1'
        );  
        insert acc1;
        
        Contact conCase = new Contact (
        AccountId = acc1.id,
        LastName = 'portalTestUserv1'
        );
        insert conCase;
        
        

        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User existingPortalUser=new User(firstname = 'ABC',UserRoleId = obj.Id, 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US',contactId = conCase.id

                         ProfileId = pf.Id
                        ); 
        
        
        insert existingPortalUser;
        
        
        
        String randFax = Math.rint(Math.random() * 1000) + '5551234';
        
        System.runAs(existingPortalUser) {
            MyProfilePageController controller = new MyProfilePageController();
            controller.edit();
            
            controller.cancel();
            
            controller.getUser().Fax = randFax;
            controller.save();
        }
        
        }

}



 
This was selected as the best answer