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
Mia PMia P 

Need help on my First VF Page and Class!

Hi All,
I am trying to create my first VF page and Controller class. I need help for further enhancements please.

User-added image

I need help with the following:
  1. When I hit that "Back" command button - it should redirect to a custom object page, lets say - "Employee__c".
  2. Also, a custom page message should come up if any of the numbers/ text not filled (please see screenshot).
  • where and how to add the following:
     if(firstNumber == '' || firstNumber == null || secondNumber == '' || secondNumber == '')
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter First and Second numbers'));
Please look into the below code for VF Page and Class and help me with the code. Thanks.

VF Page:
<apex:page controller="firstcls">
  <apex:form >
      <apex:pageBlock >
          <apex:pageBlockButtons location="top">
              <apex:commandButton value="Add" action="{!Addnumbers}"/>
              <apex:commandButton value="Subtract" action="{!SubtractNumbers}"/>
              <apex:commandButton value="Multiply" action="{!Multiplynumbers}"/>
              <apex:commandButton value="Divide" action="{!Dividenumbers}"/>
              <apex:commandButton value="Back" action="{!getBack}"/> //Not sure if, its right or wrong?
             
          </apex:pageBlockButtons>
          <apex:pageBlockSection >
              <apex:outputLabel value="Enter First Number"/>
              <apex:inputtext value="{!firstNumber}"/>
              <apex:outputLabel value="Enter Second Number"/>
              <apex:inputtext value="{!secondNumber}"/>
              
              <apex:outputLabel value="Result"/>
              <apex:inputText value="{!result}"/>
          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
</apex:page>

CLASS:
public class firstcls {
public string firstNumber {get;set;}
    public string secondNumber {get;set;}
    public string temp;
    public string result {get;set;}
    
    public firstcls ()
    {
        //firstNumber = '0';
        //secondNumber = '0';
        result = '0';
    }
   public void Addnumbers()
    {
        
        result = string.valueof(Integer.valueof(firstNumber) + Integer.valueof(secondNumber));
    }
    
    public void SubtractNumbers()
    {
        result = string.valueof(Integer.valueof(firstNumber) - Integer.valueof(secondNumber));
    }
    public void Multiplynumbers()
    {
        
        result = string.valueof(Integer.valueof(firstNumber) * Integer.valueof(secondNumber));
    }
    public void Dividenumbers()
    {
        
        result = string.valueof(Integer.valueof(firstNumber) / Integer.valueof(secondNumber));
    }
    public PageReference getBack()
    {
    PageReference ref= new PageReference('/'+00B61000002AkLf);
    ref.setredirect(true);
    return ref;
    }
}

When I am trying to save the above class, I am getting this error-
Error: firstcls Compile Error: expecting a right parentheses, found 'B61000002AkLf' at line 39 column 47

I need guidance on this. Look forward for your helpful replies.

Thanks.
 

 
Best Answer chosen by Mia P
Mudasir WaniMudasir Wani
PageReference  accepts string as parameter.
Here you need to make your number as sting and then you can successfully pass it 
Use the below code
 public PageReference getBack()
    {
    PageReference ref= new PageReference('/'+'00B61000002AkLf');
    ref.setredirect(true);
    return ref;
    }

And you are done!!!!!!!!!!

Mark it as best answer if it solves your problem

All Answers

Mudasir WaniMudasir Wani
PageReference  accepts string as parameter.
Here you need to make your number as sting and then you can successfully pass it 
Use the below code
 public PageReference getBack()
    {
    PageReference ref= new PageReference('/'+'00B61000002AkLf');
    ref.setredirect(true);
    return ref;
    }

And you are done!!!!!!!!!!

Mark it as best answer if it solves your problem
This was selected as the best answer
Mia PMia P
Thanks Mudasir, that was a quick response. Yes, it worked!

How about the 2nd requirement about the page message?
2)a custom page message should come up if any of the numbers/ text not filled (please see screenshot in the main question).

where and how to add the following?
 if(firstNumber == '' || firstNumber == null || secondNumber == '' || secondNumber == '')
      ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter First and Second numbers'));
Mudasir WaniMudasir Wani
Apex:-
 
if(firstNumber == '' || firstNumber == null || secondNumber == '' || secondNumber == '')
{
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter First and Second numbers'));
}
 
Insert the below tag inside the visualforce page within page block
<apex:pageMessages ></apex:pageMessages>

 
Mia PMia P
Thanks Mudasir for the help!