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
KevSnellKevSnell 

Populate field with userid when user equals certain role

Hi all,

 

Hopefully this is a simple one but I have a visualforce form which users use to load a lead, however, I want to populate a lookup field with the userid only when the user comes under a certain role.

 

I have tried this query to create the string

 

String userbasedid = [select ID from user where (userRoleid = '00EE0000000gXTj' AND id = :UserInfo.getUserId()].id;   

 

Piece of Form Code:

IdentifyUser = userbasedid

      

So the form works fine when the users role equals the above userroleid, however, as soon as a different user with a different user role uses it I get the error:

 

List has no rows for assignment to SObject.

 

So I need to figure a way that when the query result is null it doesn't try to populate the identifyuser field.

 

Thanks

Kev 

 

shra1_devshra1_dev

Hi Kev,

 

 

Here you have not done the Exception handling. just write your code in try block and do nothing in the catch block.

 

try{

String userbasedid = [select ID from user where (userRoleid = '00EE0000000gXTj' AND id = :UserInfo.getUserId()].id;

}

catch(Exception e){

}

 

this solves your problem.

 

Regards,

Shravan

KevSnellKevSnell

Hi,

 

Thank you for your advice but I tried this once before and have just tried it again and for some reason I don't get the error but instead the userid doesn't get passed to the field.

 

Here's the code

 

Kev

 

Public with sharing class Reserve {

    public Lead lead {get; private set;}
    public String company {get; set;} 
    public String email {get; set;}
    public String phone {get; set;}
    public String firstName {get; set;}
    public String lastName {get; set;}
    public String title {get; set;} 
    public String Street {get; set;}
    public String Street2 {get; set;}    
    public String City {get; set;}
    public String Postcode {get; set;}
    public String Country {get; set;}
    public String user {get; set;}
    
    public string userbasedid {get; set;}
    
	public String getName() {
        return 'Reserve';
    }
   
    // COMMAND WHEN SAVE BUTTON IS CLICKED
    public PageReference save() {

try{
String userbasedid = [select ID from user where (userRoleid = '00EE0000000gXTj' or userRoleid = '00EE0000000gXUD' or userRoleid = '00EE0000000gXU3' or userRoleid ='00EE0000000gXU8' or userRoleid='00EE0000000gXNq') AND id = :UserInfo.getUserId()].id;
}
catch(Exception e){
}


        Lead l = new Lead(
        	OwnerId = '00GE0000000hCBH',
        	LeadSource = 'Reserve',
    	    FirstName = firstName,
   		 LastName = lastName,
        	Email = email,
        	Phone = phone,
        	Title = title,
        	Company = company,
        	Street = street + '\r\n' + street2,
        	City = city,
        	PostalCode = postcode,
        	Country = country,
        	Vistage_BDM__c = userbasedid 
        );
		insert l; // INSERT NEW CANDIDATE INTO SYSTEM

		return Page.New_Reserve_Entry_Confirmation; //RETURN CONFIRMATION PAGE
	}
}

 

Ispita_NavatarIspita_Navatar

Hi,


First of all you can invoke SOQL query directly on User and check the size of the records.
You can use getter and setter property on assign it with the id of user on the basis of condition.

 

List<User> usr = [select ID from user where (userRoleid = '00EE0000000gXTj' AND id = :UserInfo.getUserId()];
if(usr.size() >0){// your business logic here...}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

 

KevSnellKevSnell

Thanks, however, I tried the below and it didn't work:

 

List<User> usr = [select ID from user where (userRoleid = '00EE0000000gXTj' AND id = :UserInfo.getUserId()];
if(usr.size() >0)

{

userbasedid = usr.id;

}

 

I want to feed the above into the form here:

 

Vistage_BDM__c = userbasedid