1

I have 7 picklist values in the Region field on Contact and im trying to set with user roles. But user roles have 10 options. One of the values is <None Specified> and i get the error code:

ContactRegionTrigger: execution of BeforeUpdate caused by: System.QueryException: 
List has no rows for assignment to SObject Trigger.ContactRegionTrigger: 
line 10, column 1

The other two values that aren't in the Region field on Contact give me bad value error. Is there anyways to use if/else to set the values before the query

trigger ContactRegionTrigger on Contact (before insert, before update) 
{    
    //get the id of the current user
    String roleId = System.userInfo.getUserRoleId();

    //get the role name of the current user by using the roleId
**  UserRole singleRole = [SELECT Id FROM UserRole WHERE Id = :roleId]; //Line10
    System.debug('single role name' + singleRole.Name);

    for(contact c: trigger.new)
    {
        Set<String> contactRegionSet = new Set<String>();
        if(c.Region__c != null)
        {contactRegionSet.addAll(c.Region__c.split(';'));}
        contactRegionSet.add(singleRole.Name);
        List<String> newRegions = new List<String>(contactRegionSet);
        c.Region__c = String.join(newRegions, ';'); 
    }


}

Now I tried this, and i can get around the error but doesn't fix my problem

trigger ContactRegionTrigger on Contact (before insert, before update) 
{    
    //get the id of the current user
    String roleId = System.userInfo.getUserRoleId();

    try{

    //get the role name of the current user by using the roleId
    UserRole singleRole = [SELECT Id FROM UserRole WHERE Id = :roleId];
    System.debug('single role name' + singleRole.Name);

    for(contact c: trigger.new)
    {
        if(singlerole.Name == null)
        {
             singlerole.Name = 'USNE';
        }
            else if(singlerole.Name == 'Executive Management')
        {
             singlerole.Name = 'USNE';
        }
            else if(singlerole.Name == 'US')
        {
            singlerole.Name = 'USNE';
        }
        Set<String> contactRegionSet = new Set<String>();
        if(c.Region__c != null)
        {contactRegionSet.addAll(c.Region__c.split(';'));}
        contactRegionSet.add(singleRole.Name);
        List<String> newRegions = new List<String>(contactRegionSet);
        c.Region__c = String.join(newRegions, ';'); 
    }
    }
    catch(exception e)
    {
        system.debug('Error code ' + e.getMessage())
    }
}

1 Answer 1

3

I suspect the issue is that the user experiencing this issue has no role assigned.

That would cause System.userInfo.getUserRoleId(); to return null, and in turn cause the query on UserRole to return no rows and give you that particular error.

Like I mentioned on your other question, assigning the result of a query to a single object instead of a List is a bit dangerous.

You could adjust your code to use a List here, but unless you specifically want to add "this user has no role" to region__c, then you could simply detect if roleId is null before running the query, and exit from the trigger early

ex.

String roleId = System.userInfo.getUserRoleId();

if(roleId == null){
    return;
}

// the rest of your code is unchanged

Alternative example, if you do want to add a value if a user has no role

String roleId = System.userInfo.getUserRoleId();
String roleName;

if(roleId == null){
    roleName = 'none';
}else{
    roleName = [<query on UserRole>].Name;
}

// and later in your code, you would modify things to use roleName instead of singleRole.Name
7
  • Oh okay, I thought the RoleId was just the User Id cause when i debugged it, it gave me a long string that resembled an Id. I didn't know it could be null but i combined the singlerole query with the rolename variable
    – Tyler
    Sep 12, 2019 at 18:41
  • @Tyler each SObject in Salesforce (that's things like Account, UserRole, Contact, etc...) has their own Id field. Ids are always either 15 or 18 characters long. The first 3 characters of an Id tell us (well, Salesforce) what SObject type the Id is for. Account Ids start with 001, Case Ids with 500, Opportunities with 006, and so on. This is a strict separation, which lets us say things like "the Id for a UserRole will never be used as the Id of a User record".
    – Derek F
    Sep 12, 2019 at 18:50
  • You can (and do) find Ids for a given object on other objects (UserRoleId on User is a fine example), this is how records are related to one another, but the UserRoleId on a User record will never appear as the Id of a User record.
    – Derek F
    Sep 12, 2019 at 18:50
  • Ah, that makes a lot more sense now. Thank you!
    – Tyler
    Sep 12, 2019 at 19:00
  • how would you set a user's role name in a test class/
    – Tyler
    Sep 12, 2019 at 19:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .