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
TusarTusar 

Apex for loop string concanate

Hello - I am trying to loop through the permissionset object and trying to store the value in a variable, and trying to concanate for each loop.
However it seems, the underlined syntax is not working,
Any thoughts?

for (PermissionSetAssignment psa : [SELECT PermissionSet.Label  FROM PermissionSetAssignment WHERE 
    PermissionSet.ISOWNEDBYPROFILE = false AND AssigneeId = :u.Id]) 
{ //query for the permission set Users that are already assigned
                //usersWithAccess.add(psa.AssigneeId); //add the Id of each assigned User to our set
      
          Userfunctiontype  += Userfunctiontype;
          System.debug(Userfunctiontype);
          if(permissionsetname.indexOf(Opportunity) !=-1)
          {
          
              Userfunctiontype = 'OpportunityMgmt';
          
          }
          else if(permissionsetname.indexOf(Tracker) !=-1)
          {
          
          Userfunctiontype = 'Demo1';
          
          }
          
          
          else if(permissionsetname.indexOf(Connect) !=-1)
          {
          

          Userfunctiontype = 'Demo2';
          
          }
          


   
            } 
            s.put(usertype,Userfunctiontype);
            update scope;
pconpcon
What are you trying to accomplish? You don't define the variable "Userfunctiontype" anywhere in your code.  Also, based on your code you are overwriting this value every time.
TusarTusar
Ok..userfunctiontype is defined as a string.. Not pasted here.. What is needed is userfunctiontype should return a concanated string eg..opportunitymgmtdemo2connect As it loops through..
pconpcon
You would instead want to update your if statements to say
 
userFunctionType += 'OpportunityMgmt';

However, might I suggest a better solution.  Instead of keeping them all in a running and concatinating them like that, instead use a List of Strings and then combine the list at the end
 
List<String> functionTypes = new List<String>();

for (PermissionSetAssignment psa : [
    select PermissionSet.Label
    from PermissionSetAssignment
    where PermissionSet.IsOwnedByProfile = false and
        AssigneeId = :u.Id
]) {
    String permName = psa.PermissionSet.Label;

    if (permName.contains('Opportunity')) {
        functionTypes.add('OpportunityMgmt');
    } else if (permName.contains('Tracker')) {
        functionTypes.add('Demo1');
    } else if (permName.contains('Connect')) {
        functionTypes.add('Demo2');
    }
}

scope.put(usertypes, String.join(functionTypes, ''));
update scope;

This would also allow you to create a Set<String> to make it so that each type only shows up in your concatenated list once.