• Jason Carrigan 9
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Hi All, I created a visualforce page for Communities users to update their username and email. In real life the page works fine, logging in as a Community user I can update my username and email. However, I can't get the test to pass, because it is throwing the following error:

INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []

Here is the test method:
@isTest static void testChangeUserName() {
        User portalUser = getPortalUser();
        
        System.runAs(portalUser) {    
            mportal_PortalAccountController controller = new mportal_PortalAccountController();
            controller.userAccount.Username = 'TEST_NAME1231241@TEST.COM';
            Test.startTest();
            controller.saveEmailUsername();
            Test.stopTest();
        }
        
        portalUser = [SELECT Id, Username FROM User WHERE ID = :portalUser.Id];
        System.assertEquals('TEST_NAME1231241@TEST.COM', portalUser.UserName.toUpperCase()); //username not changed
    }

Test user setup:
    private static User getPortalUser() {
        Contact c = new Contact(LastName = 'Test');
        Insert c;
        
        Profile p           = [SELECT Id FROM Profile  WHERE Name       = 'CRCPortal' LIMIT 1];
        
        User u = new User(Alias             = 'standt', 
                          Email             = 'standardudsfaser@testorg.com', 
                          EmailEncodingKey  = 'UTF-8', 
                          LastName          = 'Testing', 
                          LanguageLocaleKey = 'en_US', 
                          LocaleSidKey      = 'en_US', 
                          ProfileId         =  p.Id, 
                          TimeZoneSidKey    = 'America/Los_Angeles', 
                          UserName          = 'standarduser1234@testorg.com', 
                          ContactId         =  c.id
                          );        
        insert u;
        return u;
    }

Controller save method:
public void saveEmailUserName() {
            try {
                update userAccount;
                showChangeEmail = false;
            } catch (Exception e) {
                ApexPages.addMessages(e);
                System.debug(LoggingLevel.ERROR, e.getMessage());
            }   
    }


Giving the test user a System Administrator profile lets the test pass, but obviously I want to test as a communities user.

Thanks!
Hi and thanks in advance.

I have several trigger and a several tests written.  The code coverage is good, 77% and higher per test, but I want to get them higher.  The tests don't cover my .addError code.  the 2 lines not covered are in bold.

My trigger:
trigger checkAccountCreditStatusisRejected on Opportunity (before insert) {
       //Create list to store data in  
       list<id>AccountidLst=new list<id>(); 
      
       for(Opportunity opp : System.trigger.new)
      { // store all Opportunity Account Id's
       AccountidLst.add(opp.AccountId);
      }
      // Create map that stores the id and credit status of Active Opportunity Account
      map<id,account>accountMap=new map<id,account>([select id,Credit_Status__c from account where id In : AccountidLst]);
      
      for (Opportunity opp : System.trigger.new) {          
          if(accountMap.containskey(opp.AccountId))
            {
            if (accountMap.get(opp.AccountId).Credit_Status__c == 'Rejected')
              {
                opp.addError('WARNING: New Opportunity cannot be created when Account Credit Status = Rejected');

              }// end if 
            }
                           
       }//end for  
        
    }// end trigger

My test:
@isTest 
public class testCheckAccountCreditStatusisRejected{
   static testMethod void checkAccountCreditStatusisRejected () {
 
         // create mock user
User mockUser = new User(alias = 'newUser', email='newuser@pbsnow.com',
emailencodingkey='UTF-8', lastname='Testing',
languagelocalekey='en_US', localesidkey='en_US', profileid = UserInfo.getProfileId(),
timezonesidkey='America/Los_Angeles', CommunityNickname='test1', username='testAddDefaultTeam@pbsnow.com');
insert mockUser;  
        
        //Define list
        list<id>AccountidLst=new list<id>();  
        
       //create Account and Opportunity to add default team member
        Account acc = new Account ( Name = 'testAccount', BillingStreet = '123 Main Street', BillingCity = 'Dallas' ,
         BILLINGCOUNTRYCODE='US', BillingStateCode = 'TX', Industry = 'Healthcare', Company__c = 'Pinnacle Business Systems', BillingPostalCode = '75749' );
         insert acc;                     
      
       // store all Opportunity Account Id's
       AccountidLst.add('001J000001DYN5I');     
        
        // Create map that stores the id and credit status of Active Opportunity Account
      map<id,account>accountMap=new map<id,account>([select id,Credit_Status__c from account where Name = 'testAccount']);      
     
       
        Opportunity opp = new Opportunity( Name = 'testOpportunity', Account = acc,/* ToBeSync__c = false,*/ StageName ='Prospecting', Is_this_a_Maintenance_Renewal_Op__c = 'No',Description = 'Test', CloseDate = Date.today(),
         OwnerId = mockUser.id);
                 
        try{
            insert opp;
         
         if(accountMap.containskey('001J000001DYN5I'))
            {
            if (accountMap.get('001J000001DYN5I').Credit_Status__c == 'Rejected')
              {
                opp.addError('WARNING: New Opportunity cannot be created when Account Credit Status = Rejected');
              }// end if 
            }
            
         }//end try
         catch (ListException e) {
         
         }
       
    }

}// end public class