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
alex_from_parisalex_from_paris 

Can't get in custom controller value typed by user

Hi

I am a newbe on visualforce and controller so I hope an expert can answer me quickly

My issue is in class campaign_member_set,  mycontact.Email does not exist or mylead.Email

So how do I get new input from my user ?

I can only update mycampaignmember with no error

 

Here is my controller code

public class EventController {

    private String myid;
    private Contact mycontact;
    private Account myaccount;
    private Lead mylead;
    private CampaignMember mycampaignmember;
    private Campaign mycampaign;
    String itsacontact;
    String itsalead;
    String initialemail;

    // Extract ID of URL
    public EventController ()
    {
        myid= ApexPages.currentPage().getParameters().get('id');
        mycampaignmember= [select CampaignId, ContactId, LeadId from CampaignMember where id=:myid];
        if (mycampaignmember.ContactId != null)
        {
            itsacontact='true';
            itsalead='false';
        }
        else
        {
            itsacontact='false';
            itsalead='true';
        }
           
    }
   
    public String getitsacontact() {
              return itsacontact;
    }
   
    public String getitsalead() {
              return itsalead;
    }
 
   
    // Liste des options AM,PM,All day
    List<String> Event_selection= new List<String>();
    public void setEvent_selection(String[] Event_selection)
    {
        this.Event_selection = Event_selection;
    }
 
    public String[] getEvent_selection()
    {
        return Event_selection;
    }
   
    public List<SelectOption> getEvent_items()
    {
        List<SelectOption> options = new List<SelectOption>();
        if (mycampaign.Option_1_Name__c != null)
        {
        options.add(new SelectOption('1',mycampaign.Option_1_Name__c));
        }
        if (mycampaign.Option_2_Name__c != null)
        {
        options.add(new SelectOption('2',mycampaign.Option_2_Name__c));
        }
        return options;
    }
   
    public CampaignMember getMycampaignmember() {
      mycampaignmember=[select Option_2__c, Option_1__c, Nom_accompagnant__c, Accompagnant__c from CampaignMember where id=:myid];
      return mycampaignmember;
    }
   
    public Contact getMycontact() {
      if (itsacontact=='true')
      {   
          mycampaignmember=[select ContactId from CampaignMember where id=:myid];
          mycontact=[select Phone, LastName, FirstName, Email, AccountId From Contact where id=:mycampaignmember.ContactId];
          initialemail=mycontact.Email;
          return mycontact;
      }
      else
      {
          return null;
      }
     
     
    }
  
   public Account getMyaccount() {
       if (itsacontact=='true')
      {
          mycampaignmember=[select ContactId from CampaignMember where id=:myid];
          mycontact=[select AccountId From Contact where id=:mycampaignmember.ContactId];
          myaccount=[select Name from Account where id=:mycontact.AccountId];
          return myaccount;
      }
      else
      {
      return null;
      }
     
    }
   
    public Lead getMylead() {
       if (itsacontact=='false')
      {
      mycampaignmember=[select LeadId from CampaignMember where id=:myid];
      mylead=[select Phone, LastName, FirstName, Company, Email From Lead where id=:mycampaignmember.LeadId ];
      initialemail=mylead.Email;
      return mylead;
      }
      else
      {
      return null;
      }
     
    }

   
    // Update Campaign or create a lead
    public Pagereference campaign_member_set()
    {
         String newemail;
        
         try{
         update mycampaignmember;
        
         if (itsacontact=='true')
         {
            
newemail=mycontact.Email;
         }
         else
         {
             newemail=mylead.Email;
         }
        
         PageReference p = Page.Success_page;
         p.setRedirect(true);
         return p;
        }
        catch (Exception e)
        {
         PageReference p = Page.Failure_page;
         p.setRedirect(true);
         return p;
        }
        
     }

 

Here is my visualforce page

<apex:page showHeader="false" controller="EventController">
<script>
function setCheck (input) {
     //alert(input.checked);
     //alert(input.value);
     //myname=input.name;
     //document.getElementsByName(myname)[2].checked = true;
}

</script>

 

    <apex:pageBlock rendered="{!itsacontact}">
    <apex:form >
    <apex:panelGrid columns="2">
      Prénom<apex:inputText value="{!mycontact.FirstName}"/><p/>
      Nom <apex:inputText value="{!mycontact.LastName}"/><p/>
      Email <apex:inputText value="{!get .Email}"/><p/>
      Téléphone <apex:inputText value="{!mycontact.Phone}"/><p/>
      Société <apex:inputText value="{!myaccount.Name}"/><p/>
      </apex:panelGrid>
      </apex:form >
     </apex:pageBlock>
    
    <apex:pageBlock rendered="{!itsalead}">
    <apex:form >
    <apex:panelGrid columns="2">
      Prénom<apex:inputText value="{!mylead.FirstName}"/><p/>
      Nom <apex:inputText value="{!mylead.LastName}"/><p/>
      Email <apex:inputText value="{!mylead.Email}"/><p/>
      Téléphone <apex:inputText value="{!mylead.Phone}"/><p/>
      Société <apex:inputText value="{!mylead.Company}"/><p/>
      </apex:panelGrid>
      </apex:form >
     </apex:pageBlock>
     
     <apex:form >
       <apex:selectCheckboxes layout="lineDirection" value="{!Event_selection}" onclick="setCheck (this);">
            <apex:selectOptions value="{!Event_items}" />
       </apex:selectCheckboxes><p/>
      <p/>
      <apex:outputPanel >
        <label>Accompagnée de : </label><input id="option4" type="checkbox"/>
        <apex:inputText value="{!mycampaignmember.Nom_accompagnant__c}"/>
      </apex:outputPanel>
      <p/>
     <apex:commandButton value="Valider" action="{!campaign_member_set}"/>
    </apex:form>
</apex:page>

 

 

Best Answer chosen by Admin (Salesforce Developers) 
alex_from_parisalex_from_paris

Hi

I have modified my code and create get/set on every variable (instead of object) and everything is now working well

Thanks for your help and quick answer

Regards

Alex

 

NB : with debut, I have attempt to reference a null object but I don't know where it were coming from

All Answers

WesNolte__cWesNolte__c

Hi Alex,

 

It seems like there are 2 things that you have to change.

 

1. When you click a commandbutton or commandlink it only sends the data in the enclosing <apex:form> to the server. Your lead input text field are in a different form so they're not sent. Try wrapping ALL your fields in 1 form.

2. You have a "getMyLead" method but there's not method to "setMyLead" i.e. the variable myLead is read-only here so you need to write a setter method too. Have a look here: http://www.forcetree.com/2009/07/getter-and-setter-methods-what-are-they.html

 

Wes

alex_from_parisalex_from_paris

Hi We

alex_from_parisalex_from_paris

Hi Wes

Thank you for your quick answer

I have modified my apex page

 

<apex:page showHeader="false" controller="EventController">
    <p/>
    <apex:form>
        <apex:panelGrid columns="2" rendered="{!itsacontact}">
          Prénom<apex:inputText value="{!mycontact.FirstName}"/><p/>
          Nom <apex:inputText value="{!mycontact.LastName}"/><p/>
          Email <apex:inputText value="{!mycontact.Email}"/><p/>
          Téléphone <apex:inputText value="{!mycontact.Phone}"/><p/>
          Société <apex:inputText value="{!myaccount.Name}"/><p/>
        </apex:panelGrid>
     
        <apex:panelGrid columns="2" rendered="{!itsalead}">
          Prénom<apex:inputText value="{!mylead.FirstName}"/><p/>
          Nom <apex:inputText value="{!mylead.LastName}"/><p/>
          Email <apex:inputText value="{!mylead.Email}"/><p/>
          Téléphone <apex:inputText value="{!mylead.Phone}"/><p/>
          Société <apex:inputText value="{!mylead.Company}"/><p/>
         </apex:panelGrid>
   
         <apex:outputPanel >
            <label>Accompagnée de : </label><input id="option4" type="checkbox"/>
            <apex:inputText value="{!mycampaignmember.Nom_accompagnant__c}"/>
         </apex:outputPanel>
       
         <apex:commandButton value="Valider" action="{!campaign_member_set}"/>
    </apex:form>
</apex:page>

 

 

and now when I click on "valider" it does nothing : no redirection

Here is my new apex class (with set method)

 

public class EventController {

    private String myid;
    private Contact mycontact;
    private Account myaccount;
    private Lead mylead;
    private CampaignMember mycampaignmember;
    private Campaign mycampaign;
    String itsacontact;
    String itsalead;
    String initialemail;

    // Extract ID of URL
    public EventController ()
    {
        myid= ApexPages.currentPage().getParameters().get('id');
        mycampaignmember= [select CampaignId, ContactId, LeadId from CampaignMember where id=:myid];
        if (mycampaignmember.ContactId != null)
        {
            itsacontact='true';
            itsalead='false';
        }
        else
        {
            itsacontact='false';
            itsalead='true';
        }
           
    }
   
    public String getitsacontact() {
              return itsacontact;
    }
   
    public String getitsalead() {
              return itsalead;
    }
 
   
    // Liste des options AM,PM,All day
    List<String> Event_selection= new List<String>();
    public void setEvent_selection(String[] Event_selection)
    {
        this.Event_selection = Event_selection;
    }
 
    public String[] getEvent_selection()
    {
        return Event_selection;
    }
   
    public List<SelectOption> getEvent_items()
    {
        List<SelectOption> options = new List<SelectOption>();
        if (mycampaign.Option_1_Name__c != null)
        {
        options.add(new SelectOption('1',mycampaign.Option_1_Name__c));
        }
        if (mycampaign.Option_2_Name__c != null)
        {
        options.add(new SelectOption('2',mycampaign.Option_2_Name__c));
        }
        return options;
    }
   
    public CampaignMember getMycampaignmember() {
      mycampaignmember=[select Option_2__c, Option_1__c, Nom_accompagnant__c, Accompagnant__c from CampaignMember where id=:myid];
      return mycampaignmember;
    }
   
    public void setMycampaignmember (CampaignMember mycampaignmember) {
    this.mycampaignmember= mycampaignmember;
    }
   
    public void setMycontact (Contact mycontact) {
    this.mycontact= mycontact;
    }
   
    public void setMyaccount (Account myaccount) {
    this.myaccount= myaccount;
    }
   
    public void setMylead (Lead mylead) {
    this.mylead= mylead;
    }
   
    public Contact getMycontact() {
      if (itsacontact=='true')
      {   
          mycampaignmember=[select ContactId from CampaignMember where id=:myid];
          mycontact=[select Phone, LastName, FirstName, Email, AccountId From Contact where id=:mycampaignmember.ContactId];
          initialemail=mycontact.Email;
          return mycontact;
      }
      else
      {
          return null;
      }
     
     
    }
  
   public Account getMyaccount() {
       if (itsacontact=='true')
      {
          mycampaignmember=[select ContactId from CampaignMember where id=:myid];
          mycontact=[select AccountId From Contact where id=:mycampaignmember.ContactId];
          myaccount=[select Name from Account where id=:mycontact.AccountId];
          return myaccount;
      }
      else
      {
      return null;
      }
     
    }
   
    public Lead getMylead() {
       if (itsacontact=='false')
      {
      mycampaignmember=[select LeadId from CampaignMember where id=:myid];
      mylead=[select Phone, LastName, FirstName, Company, Email From Lead where id=:mycampaignmember.LeadId ];
      initialemail=mylead.Email;
      return mylead;
      }
      else
      {
      return null;
      }
     
    }

   
    // Update Campaign or create a lead
    public Pagereference campaign_member_set()
    {
         try{
                
         if (itsacontact=='true')
         {
             mycampaignmember.Nom_Accompagnant__c=mycontact.Email;
             update mycampaignmember;
             //Lead mynewlead=new Lead(LastName='Alex',Company='Alex');
             //insert mynewlead;
         }
         else
         {
             mycampaignmember.Nom_Accompagnant__c=mylead.Email;
             update mycampaignmember;
         }
        
         PageReference p = Page.EMV_Success_page;
         p.setRedirect(true);
         return p;
        }
        catch (Exception e)
        {
         PageReference p = Page.EMV_Failure_page;
         p.setRedirect(true);
         return p;
        }
    }
   
   
   
   
  
   
   
   
    // Test Class
    static testmethod void testEventController ()
    {
        EventController mynl = new EventController();
        mynl.myid='003A0000006zCiw';
       
        List<String> myfieldNameList =new List<String>();
        //myfieldNameList=mynl.getFieldList('Name','Lead');
        //myfieldNameList=mynl.getFieldList('Name','Contact');
        //myfieldNameList=mynl.getFieldList('Name','Account');
       
        List<SelectOption> myoption=new List<SelectOption>();
   
       
        PageReference mypage=new PageReference('emv');
        mypage=mynl.campaign_member_set();

       
      
        mynl.myid='00QA000000646xK';
        mypage=mynl.campaign_member_set();

       
        mynl.myid='001A0000007ZkfV';
        mypage=mynl.campaign_member_set();
 
    }
 }

 

Can you please help me ?

I am getting crazy...

WesNolte__cWesNolte__c

Hi Alex,

 

What does it say in the debug log? You should include some "System.debug()" messages in the action method and trace the path it's taking through the code. Without knowing that I can't help anymore.

 

Wes

alex_from_parisalex_from_paris

Hi

I have modified my code and create get/set on every variable (instead of object) and everything is now working well

Thanks for your help and quick answer

Regards

Alex

 

NB : with debut, I have attempt to reference a null object but I don't know where it were coming from

This was selected as the best answer