You need to sign in to do that
Don't have an account?
John 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); } }
All Answers
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.
Thanks,
Himanshu