You need to sign in to do that
Don't have an account?
How to get lookup field value from VF to Apex controller?
Hi everyone,
I am new to Salesforce. I can't figure out how to capture value from a lookup field in VF and save it into a varaible in Apex, can someone help please? I have an Custom_Test__c is my object and Custom_Name__c is the lookup field, below is the code:
---VF---
<apex:page standardController="Custom_Test__c" recordSetVar=""
extensions="myUserListHandler">
<br/>
<apex:form >
<apex:pageBlock title="Select project" id="selectionBlock2">
<apex:inputField value="{!Custom_Test__c.Custom_Name__c}" required="true">
</apex:inputField>
<apex:commandButton value="selected Users(" action="{!dodelete}" onclick="return confirm('Are you sure you?');" />
</apex:pageBlock>
</apex:form>
</apex:page>
----Apex---
public void docreate() {
Id Iduser;
Custom_Test__c myObject = (Custom_Test__c) controller.getRecord();
for (String s: selectedNames) {
if (s <> NULL ){
List<User> accs = [SELECT Id, Name FROM User WHERE Name =:s];
for(User a : accs){
Iduser =a.Id;
EntitySubscription newEN = new EntitySubscription(ParentID =myObject.id,SubscriberId=Iduser);
try{
insert newEN;
} catch (DmlException e) {}
}
}
}
}
Thanks,
ok, try this
public class myUserListHandler {
private final Custom_Test__c ct;
public Custom_Test__c testCustom {get;set;}
public myUserListHandler (ApexPages.StandardSetController controller) {
this.controller = controller;
loadFieldsWithVisibility();
this.ct = (Custom_Test__c) controller.getRecord();
testCustom = [select id, Custom_Name__c from Custom_Test__c where id=:ct.id];
}
public void docreate() {
// you should here reference testCustom.Custom_Name__c for the lookup value;
Id Iduser;
Custom_Test__c myObject = (Custom_Test__c) controller.getRecord();
for (String s: selectedNames) {
if (s <> NULL ){
List<User> accs = [SELECT Id, Name FROM User WHERE Name =:s];
for(User a : accs){
Iduser =a.Id;
EntitySubscription newEN = new EntitySubscription(ParentID =myObject.id,SubscriberId=Iduser);
try{
insert newEN;
} catch (DmlException e) {}
}
}
}
}
}
VF:
<apex:inputField value="{!testCustom.Custom_Name__c}" required="true">
All Answers
http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_extension.htm
Thanks for the reply. But i still can't get it to work, i added in what your link saids, but it is still giving me nothing. Can you tell me what i did wrong?
public class myUserListHandler {
private Custom_Test__c testCustom;
public myUserListHandler (ApexPages.StandardSetController controller) {
this.controller = controller;
loadFieldsWithVisibility();
this.testCustom = (Custom_Test__c) controller.getRecord();
}
public void docreate() {
Id Iduser;
Custom_Test__c myObject = (Custom_Test__c) controller.getRecord();
for (String s: selectedNames) {
if (s <> NULL ){
List<User> accs = [SELECT Id, Name FROM User WHERE Name =:s];
for(User a : accs){
Iduser =a.Id;
EntitySubscription newEN = new EntitySubscription(ParentID =myObject.id,SubscriberId=Iduser);
try{
insert newEN;
} catch (DmlException e) {}
}
}
}
}
}
Thanks
There will be lil changes. You are not doing in rightway. You are not binding any controller variable with the VF
ok, try this
public class myUserListHandler {
private final Custom_Test__c ct;
public Custom_Test__c testCustom {get;set;}
public myUserListHandler (ApexPages.StandardSetController controller) {
this.controller = controller;
loadFieldsWithVisibility();
this.ct = (Custom_Test__c) controller.getRecord();
testCustom = [select id, Custom_Name__c from Custom_Test__c where id=:ct.id];
}
public void docreate() {
// you should here reference testCustom.Custom_Name__c for the lookup value;
Id Iduser;
Custom_Test__c myObject = (Custom_Test__c) controller.getRecord();
for (String s: selectedNames) {
if (s <> NULL ){
List<User> accs = [SELECT Id, Name FROM User WHERE Name =:s];
for(User a : accs){
Iduser =a.Id;
EntitySubscription newEN = new EntitySubscription(ParentID =myObject.id,SubscriberId=Iduser);
try{
insert newEN;
} catch (DmlException e) {}
}
}
}
}
}
VF:
<apex:inputField value="{!testCustom.Custom_Name__c}" required="true">
Thanks Mark and Avi for the help but now i am getting an error after changing my code to the following:
public class myUserListHandler {
private final Custom_Test__c ct;
public Custom_Test__c testCustom {get;set;}
public myUserListHandler (ApexPages.StandardSetController controller) {
this.controller = controller;
loadFieldsWithVisibility();
this.ct = (Custom_Test__c) controller.getRecord();
testCustom = [select id, Custom_Name__c from Custom_Test__c where id=:ct.id];
}
public void docreate() {
// you should here reference testCustom.Custom_Name__c for the lookup value;
Id Iduser;
for (String s: selectedNames) {
if (s <> NULL ){
List<User> accs = [SELECT Id, Name FROM User WHERE Name =:s];
for(User a : accs){
Iduser =a.Id;
EntitySubscription newEN = new EntitySubscription(ParentID =testCustom.id,SubscriberId=Iduser);
try{
insert newEN;
} catch (DmlException e) {}
}
}
}
}
}
VF:
<apex:inputField value="{!testCustom.Custom_Name__c}" required="true">
The error:
List has no rows for assignment to SObject
An unexpected error has occurred. Your development organization has been notified.
Thanks,
I forgot to mention this VF and Apex is used on a button on the search layout of my custom object. does that matter?
hey create a object variable and bind your field with this varaibale your value will be stored in that varaiable..
for example;
Public account acc{get;set;}
Public yourConstructor(){
acc = new account();
}
your vf page;
<apex:inputfield value="{acc}" />
Thanks,
This page seems more like a page to edit record.!
So you have to pass an record Id for it
Please open the page like this
/apex/YOURPAGENAME?id=CUSTOMOBJECTRECORDId
YOURPAGENAME : Replace it by your vf page name
CUSTOMOBJECTRECORDId : Replace it with your one of the record Id for your custom object