+ Start a Discussion
John Neilan 2John Neilan 2 

User Role with IF Statement

I am trying to populate a field in a Visualforce page for custom object using a controller extension. I would like to populate the field with one value if the User Role of the person creating the record is Sales and another value if they are Account Management. I'm not quite sure of the syntax I need to use to accomplish this in my controller. Can anyone assist?
 
public class VF_CampaignCaseCreateController{
public List<Campaign_Create_Request__c> CCR {get; set;}

    private final Opportunity opp;
    public VF_CampaignCaseCreateController(ApexPages.StandardController myController){
        CCR = new List<Campaign_Create_Request__c>();
            if (!Test.isRunningTest())
            {
            myController.addFields(new List<String>{'Id', 'OwnerId'});
            }
        opp=(Opportunity)myController.getrecord();
    }

    public Campaign_Create_Request__c CCR2 = new Campaign_Create_Request__c();
        public void CampaignCaseCreate(){

            CCR2.Opportunity__c = opp.Id;

            IF($UserRole.Name StartsWith('Sales')){
                CCR2.Sales_Rep__c = $UserRole.Id;
            }
            ELSE{
                CCR2.Sales_Rep__c = null;
            }   

            Opportunity o = [SELECT (SELECT Id, contactId
                                       FROM OpportunityContactRoles
                                       WHERE role = 'Signatory')
                            FROM Opportunity
                            WHERE id = :opp.id];
            CCR2.Primary_User__c = o.opportunityContactRoles.size() != 0 
                                    ? o.opportunityContactRoles[0].contactId  
                                    : null;

            CCR.add(CCR2);
        }
}

 
Best Answer chosen by John Neilan 2
John Neilan 2John Neilan 2
Thanks Himashu!  I actually went a slightly different route and just added the below to my controller extension:
 
String roleId = UserInfo.getUserRoleId();
            UserRole usrRole = [SELECT Name, Id 
                                FROM UserRole 
                                WHERE Id = :roleId LIMIT 1];
            String userRoleName = usrRole.Name;
            
            IF(usrRole.Name.startsWith('Sales')){
                CCR2.Sales_Rep__c = UserInfo.getUserId();
            }
            ELSE{
                CCR2.Sales_Rep__c = null;
            }

All Answers

sfdcsushilsfdcsushil
I think you will have to get Role id from UserInfo.getUserRoleId() and then query userrole object for fetching role details. 
Himanshu ParasharHimanshu Parashar
Hi John,
 
UserInfo.getUserRoleId()

return the current user role id which you can use to query the name of the role so your code will be modified as shown below. I am not sure where you are using CampaignCaseCreate() method so i have added code in constructor so that you can have prepopulated value on page load.
 
public class VF_CampaignCaseCreateController{
public List<Campaign_Create_Request__c> CCR {get; set;}
public Campaign_Create_Request__c CCR2{get;set;}

    private final Opportunity opp;
    public VF_CampaignCaseCreateController(ApexPages.StandardController myController){
        CCR = new List<Campaign_Create_Request__c>();
            if (!Test.isRunningTest())
            {
            myController.addFields(new List<String>{'Id', 'OwnerId'});
            }
        opp=(Opportunity)myController.getrecord();
        CCR2 = new Campaign_Create_Request__c();
        
        CCR2.Opportunity__c = opp.Id;
        
        /*******************Get Role*********************************************************/
        List<Role> rolelist  = [SELECT Name FROM UserRole where id=: UserInfo.getUserRoleId()];

        String uRoleName = rolelist[0].Name;

            IF(uRoleName.startwith('Sales')){
                CCR2.Sales_Rep__c = $UserRole.Id;
            }
            ELSE{
                CCR2.Sales_Rep__c = null;
            }   

            Opportunity o = [SELECT (SELECT Id, contactId
                                       FROM OpportunityContactRoles
                                       WHERE role = 'Signatory')
                            FROM Opportunity
                            WHERE id = :opp.id];
            CCR2.Primary_User__c = o.opportunityContactRoles.size() != 0 
                                    ? o.opportunityContactRoles[0].contactId  
                                    : null;

            CCR.add(CCR2);
        
        
        
    }
}

Thanks,
Himanshu
John Neilan 2John Neilan 2
Thanks Himashu!  I actually went a slightly different route and just added the below to my controller extension:
 
String roleId = UserInfo.getUserRoleId();
            UserRole usrRole = [SELECT Name, Id 
                                FROM UserRole 
                                WHERE Id = :roleId LIMIT 1];
            String userRoleName = usrRole.Name;
            
            IF(usrRole.Name.startsWith('Sales')){
                CCR2.Sales_Rep__c = UserInfo.getUserId();
            }
            ELSE{
                CCR2.Sales_Rep__c = null;
            }
This was selected as the best answer