2

Ok, I first want to give full credit for original code designer Shane McLaughlin for creating a bunch of very useful process builder bolt-ons. They can be found here: https://github.com/mshanemc/processBuilderBlocks

Now that I have given credit. I am trying to use his code to create an invocable class to handle public group membership. I was able to modify the main class but I am getting an error on the test class that I can't figure out. Can anyone help me out?

Main Class:

public with sharing class PB_AddToPublcGroup {

    @InvocableMethod(label='Add User to Public Group')
    public static void AddPublicGroup(list<AddGroupMember> requests) {

        list<string> output = new list<string>();

        for (AddGroupMember request:dedupe(requests)){
            //system.debug(request);
            //future methods can only accept primitives, so we serialize to a list of string
            output.add(JSON.serialize(request));
        }

        AddGroupsLater(output);

    }

    //what Process Builder/Flow Builder can reference for the input variables
    public class AddGroupMember {
        @InvocableVariable(required=true label='Public Group Name')
        public String PubGroupName;

        @InvocableVariable(required=true label='User Id')
        public Id UserId;
    }

    public static list<AddGroupMember> dedupe(list<AddGroupMember> input){
        set<AddGroupMember> temp = new set<AddGroupMember>(input);
        return new list<AddGroupMember>(temp);
    }

    //has to be handled as an @future transaction because the mixed DML issue!
    @future
    public static void AddGroupsLater (list<string> requests){
        list<GroupMember> gpMembers = new list<GroupMember>();

        for (string request:requests){

            //turn it back into the request object per Quinton Wall
            //https://developer.salesforce.com/blogs/developer-relations/2013/06/passing-objects-to-future-annotated-methods.html
            AddGroupMember APSR = (AddGroupMember)JSON.deserialize(request, AddGroupMember.class);
            Group groupId = [Select ID FROM Group WHERE Name = :APSR.PubGroupName];

            GroupMember groupMem = new GroupMember();
            groupMem.UserOrGroupId  = APSR.UserId;
            groupMem.GroupId        = groupId.Id;
            gpMembers.add(groupMem);
        }

        insert gpMembers;
    }

}

Test Class:

@istest
private class test_PB_Addons {

    //tests the class across a whole bunch of permissions being added.
    @isTest static void testPublicGroup() {
        integer userCount=5;
        //create users
        list<user> users = createUsers(userCount);
        //create a Public Group
        Group GP = createGroup();
        //create the inner class object and add to list
        list<PB_AddToPublicGroup.AddGroupMember> GMRs = new list<PB_AddToPublicGroup.AddGroupMember>();
        for (user u:users){
            PB_AddToPublicGroup.AddGroupMember GMR = new PB_AddToPublicGroup.AddGroupMember();
            GMR.UserId = u.id;
            GMR.PubGroupName = GP.Name;
            GMRs.add(GMR);
        }

        //call the invocable method
        test.startTest();
        PB_AddToPublicGroup.AddGroupMember(GMRs);
        test.stopTest();

        //asserts--check that user is in the group
        /*
list<PermissionSetAssignment> PSAs = [select Id, AssigneeId, PermissionSetId 
from PermissionSetAssignment
where AssigneeId in: users and PermissionSetId =: PS.Id];

system.assertEquals(userCount, PSAs.size()); 
*/
    }

    public static list<user> createUsers(integer numberOfUsers){
***Omitted****
    }

    private static Group createGroup(){
***Omitted***
    }
}

The error in test class is this line:

//create the inner class object and add to list

list<PB_AddToPublicGroup.AddGroupMember> GMRs = new list<PB_AddToPublicGroup.AddGroupMember>();

Error:

Invalid type: PB_AddToPublicGroup.AddGroupMember

3

This looks like a simple type to me ,

Try using the below

list<PB_AddToPublcGroup.AddGroupMember> GMRs = new list<PB_AddToPublcGroup.AddGroupMember>();

Observe your class name is "PB_AddToPublcGroup"

1
  • 1
    It would probably be better to fix the typo in the original class name than propagate it. You should probably also mention what the typo is, it's kind of hard to see (missing i in Public).
    – Adrian Larson
    Aug 27 '16 at 12:15

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.