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
sfdc_ninjasfdc_ninja 

Creating Idea Votes from portal user in Test code. DML Exception

Wrote a very simple VF Page and Controller to show the Accounts that voted on certain Ideas. Here is the controller. Just aggregates the Up and Down votes on the Idea excluded internal votes. Pretty straight forward.

public class IdeaVotesByAccountController {

    public Idea thisIdea                                            {get;set;}
    public list<AggregateResult> upResults                          {get;set;}
    public list<AggregateResult> downResults                        {get;set;}

    public IdeaVotesByAccountController() {

        thisIdea = [Select Id, Title 
                    From Idea 
                    Where Id =: system.currentPageReference().getParameters().get('ideaid')];

        upResults = [SELECT Count(Id) Votes, CreatedBy.Contact.Account.Name Name
                    From Vote Where ParentId =: thisIdea.Id And Type = 'up' And CreatedBy.Contact.Account.Name != 'My Company Name' And CreatedBy.Contact.Account.Name != null
                    Group By CreatedBy.Contact.Account.Name 
                    Order By Count(Id) desc, CreatedBy.Contact.Account.Name];

        downResults = [SELECT Count(Id) Votes, CreatedBy.Contact.Account.Name Name
                    From Vote Where ParentId =: thisIdea.Id And Type = 'down' And CreatedBy.Contact.Account.Name != 'My Company Name' And CreatedBy.Contact.Account.Name != null
                    Group By CreatedBy.Contact.Account.Name 
                    Order By Count(Id) desc, CreatedBy.Contact.Account.Name];

    }

    public PageReference returnToIdea() {

        PageReference pg = new PageReference('/' + thisIdea.Id);
        pg.setRedirect(true);
        return pg;

    }
}

My problem is coming when trying to write a unit test. When I say that we are excluding internal votes, basically, we are only counting portal user votes, from portal users that do not have a company name of my company name. So in order to test, I need to be able to test all 3 scenarios scenarios
  1. a vote from within the SF UI, not through the portal (No Issue)
  2. a vote by a portal user outside of my company (ISSUE)
  3. a vote by a portal user inside of my company (ISSUE)

The issue comes with trying to create a vote from a portal user. Here is my test code that I have. The first test method works fine, the second method is throwing an error

@isTest
private class IdeaVotesByAccountControllerTest {

    private static final Profile nonPortalProf = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];
    private static final Profile portalProf = [SELECT Id FROM Profile WHERE Name Like '%portal%' limit 1];
    private static final Account a = TestClassObjectUtility2.accountCreator('Test Account');
    private static final Contact con = TestClassObjectUtility2.contactCreator(a.Id, 'Joe', 'Schmoe');
    public static final Community zone = [Select Id From Community Limit 1];
    public static final PageReference pg = Page.IdeaVotesByAccount;
    public static final User nonPortalUser = TestClassObjectUtility2.userCreator(true, nonPortalProf);
    public static final User portalUser;
    public static IdeaVotesByAccountController controller;

    static {
        portalUser = TestClassObjectUtility2.userCreator(false, portalProf);
        portalUser.ContactId = con.Id;
        insert portalUser;
    }
    //WORKS FINE
    static testmethod void BaseTestNonPortalUser() {

        Idea myIdea = TestClassObjectUtility2.ideaCreator(true, 'Title', 'Body', zone.Id);
        myIdea = [Select Id From Idea Where Id =: myIdea.Id];

        pg.getParameters().put('ideaid', myIdea.id);
        Test.setCurrentPage(pg);

        controller = new IdeaVotesByAccountController();

        system.assertEquals(0, controller.upResults.size());
        system.assertEquals(0, controller.downResults.size());

        system.runAs(nonPortalUser){
            Vote v = TestClassObjectUtility2.voteCreator(true, myIdea.Id, 'up');
        }

        controller = new IdeaVotesByAccountController();
        system.assertEquals(0, controller.upResults.size());

    }
    //FAILS
    static testmethod void BaseTestPortalUser() {

        Idea myIdea = TestClassObjectUtility2.ideaCreator(true, 'Title', 'Body', zone.Id);

        pg.getParameters().put('ideaid', myIdea.id);
        Test.setCurrentPage(pg);

        controller = new IdeaVotesByAccountController();

        system.assertEquals(0, controller.upResults.size());
        system.assertEquals(0, controller.downResults.size());

        system.runAs(portalUser){
             //THIS LINE IS FAILING
             Vote v = TestClassObjectUtility2.voteCreator(true, myIdea.Id, 'up');
        }

        controller = new IdeaVotesByAccountController();
        system.assertEquals(1, controller.upResults.size());

    }
}

It throws the error on the line where I am trying to insert the Vote object. The error is shown below

User-added image

System.DmlException: Insert failed. First exception on row 0; first error: COMMUNITY_NOT_ACCESSIBLE, You do not have permission to access the zone that this entity belongs to. You must be given permission to the zone before you can access this entity.: [ParentId]

So apparently the portal user needs permission to the community. The issue is I have searched around for documentation but I can't seem to find out how to do this. Has anyone seen this before. How can I overcome this error???

Any help is greatly appreciated.
sfdc-admin-smsfdc-admin-sm
@sfdc_ninja

Did you ever get this issue resolved? Can you post the VF code for your provided controller?

Thanks!