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
Ed055Ed055 

help with custom controller and Visualforce page

Hi All -  I need help coding custom VF page.  
Users will come to this VF page(AssignContact) with url something like apex/AssignContact?ID=066540000000XzK
The ID that is passed in the URL is one of  the record  in my custom object(Team__c)
On this custom VF page I need a Contact lookup field which the users can lookup a particular contact , select and click save button
when users click save I would like the contact record that user selected be added to my  custom object(Team) 
Thanks for your help.  




 
LBKLBK
Hi Ed,

Try this code.

APEX class
public class clsAssignContact {
    public Team__c team {get; set;}
    public String sTeamId {get; set;}
    public clsAssignContact(ApexPages.StandardController controller) {
        sTeamId = ApexPages.currentPage().getParameters().get('Id');
        team = [SELECT Id,Name, Contact__c from Team__c where Id =: sTeamId LIMIT 1];
    }
    
    public PageReference save()
    {
        update team;
        PageReference pageRef = new PageReference('/' + sTeamId);
        pageRef.setRedirect(true);
        return pageRef;
    }    
}
VF Page
<apex:page standardController="Team__c" extensions="clsAssignContact" >
  <apex:form >
      <apex:pageBlock >
          <apex:pageblockButtons >
              <apex:commandButton value="Save" action="{!save}"/>
          </apex:pageblockButtons>
          <apex:pageblockSection >
              <apex:pageblockSectionItem >
                  Name: <apex:outputField value="{!team.Name}" />              
              </apex:pageblockSectionItem>
              <apex:pageblockSectionItem >
                  Contact: <apex:inputField value="{!team.Contact__c}"/>  
              </apex:pageblockSectionItem>
          </apex:pageblockSection>
      </apex:pageBlock>
  </apex:form>
</apex:page>
Let me know if this helps.

 
Ed055Ed055
Thanks LBK for the code. What I'm trying to do is assign a team to contact.
TEAM object does not have contact lookup field so your query on line 6 will not work. Let me try to clarify this further..
TEAM object only has ID and Name field. Contact object has a TEAM lookup field. So the way it is supposed to work is 
1. user will get to the custom apex page which has team ID in the querystring/url. 
2. on this CUSTOM apex page there will be ..
              a. output field which will display team name
              b. contact lookup input field and save button.
3. once the user clicks save .. it should update the contact record (selected in input lookup field on the custom apex page) with that team ID
All the user is trying to do is assign contact to this team.


Thanks again. 
LBKLBK
hi Ed,

You cannot show a Contact lookup field (using apex:inputField), unless there is a Lookup relationship exists.

You have two choices here.

You can swap the Contact and Team in the above code and use it as a Contact page.

Your code will look like this.
public class clsAssignTeam {
    public Contact cnct {get; set;}
    public String sContactId {get; set;}
    public clsAssignTeam(ApexPages.StandardController controller) {
        sContactId = ApexPages.currentPage().getParameters().get('Id');
        cnct = [SELECT Id,Name, Team__c from Contact where Id =: sContactId LIMIT 1];
    }
    
    public PageReference save()
    {
        update cnct;
        PageReference pageRef = new PageReference('/' + sContactId);
        pageRef.setRedirect(true);
        return pageRef;
    }    
}

<apex:page standardController="Contact" extensions="clsAssignTeam" >
  <apex:form >
      <apex:pageBlock >
          <apex:pageblockButtons >
              <apex:commandButton value="Save" action="{!save}"/>
          </apex:pageblockButtons>
          <apex:pageblockSection >
              <apex:pageblockSectionItem >
                  Name: <apex:outputField value="{!cnct.Name}" />              
              </apex:pageblockSectionItem>
              <apex:pageblockSectionItem >
                  Contact: <apex:inputField value="{!cnct.Team__c}"/>  
              </apex:pageblockSectionItem>
          </apex:pageblockSection>
      </apex:pageBlock>
  </apex:form>
</apex:page>

Or, you can have Team information (in output fields) and a grid showing all the available contacts to choose from (with a checkbox probably).

So that the user can choose all the contacts he / she needs and save it.

Let me know if this helps.