Tag Archives: User

Check Case Owner is a User or Queue

Check Case Owner in Apex Class.

//Check object Id in Apex 
if(string.valueOf(c.OwnerId).startsWith('005')){
    //Owner is User       
}

if(string.valueOf(c.OwnerId).startsWith('00G')){
    //Owner is Queue
}

Check Case Owner in Apex Trigger.

//In Apex Trigger
for (Case objCase : Trigger.new) { 
    If (objCase.OwnerID.getsobjecttype() == User.sobjecttype) {
        /*Code if Owner is User*/
    }
    else{
        /*Code if Owner is Queue*/ 
    }
}

Check Case Owner by SOQL query.

//By Query Owner.Type Field
List<Case> caseList = [SELECT Id, CaseNumber, OwnerId, Owner.Name, Owner.Type FROM Case];

for (Case objCase : caseList){
    If (objCase.Owner.Type == User.sobjecttype) {
        /*Code if Owner is User*/
    }
    else{
        /*Code if Owner is Queue*/ 
    }
}

Check Case Owner in Process Builder.

//Check in Process Builder
BEGINS([Case].OwnerId, "005") //Check Owner is User
BEGINS([Case].OwnerId, "00G") //Check Owner is Queue

Reset User Password in Salesforce Using Apex

In Salesforce there are two methods to set password and to reset password for a user.

resetPassword(Id userId, Boolean sendUserEmail): This method resets the password for the specified user. When the user logs in with the new password, they are prompted to enter a new password, and to select a security question and answer if they haven’t already. If you specify true for sendUserEmail, the user is sent an email notifying them that their password was reset. A link to sign onto Salesforce using the new password is included in the email.

setPassword(Id userId, String password): Sets the password for the specified user. When the user logs in with this password, they are not prompted to create a new password.

Sample Code:

List<User> users = new List<User>();
String username = 'test@test.com';
users = Database.Query('SELECT Id, Name FROM User WHERE UserName  =: username');

for(User u : users){
    //For reset User password
    System.resetPassword(u.Id, true);
    
    //For set User password
    //System.setPassword(u.Id, 'Test@1234');
}