1

I have a class that accepts an integer and runs it through a very simple "decode" algorithim and spits back out a string. I just need some help on how to input the integer to an field and return on rerender on CommandButton to an output field.

Here is what I have so far:

Page:

<apex:page controller="Decode">
  <!-- Pass parameters from visualforce page to controller -->
    <apex:form >
            <apex:pageblock >
                  Input Here = <apex:inputText value="{!inputValue}"/>
                 <apex:commandButton value="Submit" reRender="outputID"/>
            </apex:pageblock>
            <apex:pageblock >
                 <b>Output here = </b>
                 <apex:outputText value="{!sPassowrd}" id="outputID">
                 </apex:outputText>
            </apex:pageblock>
    </apex:form>
</apex:page>

Controller:

public with sharing class Decode {

    public static String Decode(Integer inputValue) {
        String sPassword;
        Integer iTemp;
        Integer iChar;
        String sChar;
        Map<Integer,String> codeValues = new Map<Integer,String>();
            codeValues.put(12,'A');
            codeValues.put(28,'B');
            codeValues.put(19,'C');
            codeValues.put(5, 'D');
            codeValues.put(24,'E');
            codeValues.put(10,'F');
            codeValues.put(1, 'G');
            codeValues.put(17,'H');
            codeValues.put(30,'I');
            codeValues.put(27,'J');
            codeValues.put(4, 'K');
            codeValues.put(31,'L');
            codeValues.put(8, 'M');
            codeValues.put(33,'N');
            codeValues.put(13,'O');
            codeValues.put(36,'P');
            codeValues.put(6, 'Q');
            codeValues.put(21,'R');
            codeValues.put(2, 'S');
            codeValues.put(32,'T');
            codeValues.put(15,'U');
            codeValues.put(11,'V');
            codeValues.put(22,'W');
            codeValues.put(7, 'X');
            codeValues.put(25,'Y');
            codeValues.put(34,'Z');
            codeValues.put(14,'1');
            codeValues.put(23,'2');
            codeValues.put(9, '3');
            codeValues.put(16,'4');
            codeValues.put(18,'5');
            codeValues.put(3, '6');
            codeValues.put(29,'7');
            codeValues.put(26,'8');
            codeValues.put(20,'9');
            codeValues.put(35,'0');


        For (Integer i = 0; i < 5 ; i++){
            iTemp = inputValue/73;
            iChar = (inputValue - (iTemp*73));
            inputValue = iTemp;
            sChar = codeValues.get(iChar);

            if (String.isNotEmpty(sChar)){
                if (String.isNotEmpty(sPassword)){

                    sPassword = (sChar + sPassword);
                }
                else {
                    sPassword = sChar;
                }
            }           
        }
        return sPassword;
    }

}

Working code for anyone interested:

    public with sharing class Decode {

        public String inputValue { get; set; }
        public String sPassword { get; set; }

        public void getDecode()
        {
            if (inputValue == null) return;
            try
            {
                sPassword = decode(Integer.valueOf(inputValue));
            }
            catch (TypeException t)
            {
                ApexPages.addMessages(t);
            }
        }
        public static String Decode(Integer inputValue) {
            String sPassword;
            Integer iTemp;
            Integer iChar;
            String sChar;
            Map<Integer,String> codeValues = new Map<Integer,String>();
            codeValues.put(12,'A');
            codeValues.put(28,'B');
            codeValues.put(19,'C');
            codeValues.put(5, 'D');
            codeValues.put(24,'E');
            codeValues.put(10,'F');
            codeValues.put(1, 'G');
            codeValues.put(17,'H');
            codeValues.put(30,'I');
            codeValues.put(27,'J');
            codeValues.put(4, 'K');
            codeValues.put(31,'L');
            codeValues.put(8, 'M');
            codeValues.put(33,'N');
            codeValues.put(13,'O');
            codeValues.put(36,'P');
            codeValues.put(6, 'Q');
            codeValues.put(21,'R');
            codeValues.put(2, 'S');
            codeValues.put(32,'T');
            codeValues.put(15,'U');
            codeValues.put(11,'V');
            codeValues.put(22,'W');
            codeValues.put(7, 'X');
            codeValues.put(25,'Y');
            codeValues.put(34,'Z');
            codeValues.put(14,'1');
            codeValues.put(23,'2');
            codeValues.put(9, '3');
            codeValues.put(16,'4');
            codeValues.put(18,'5');
            codeValues.put(3, '6');
            codeValues.put(29,'7');
            codeValues.put(26,'8');
            codeValues.put(20,'9');
            codeValues.put(35,'0');


            For (Integer i = 0; i < 5 ; i++){
                iTemp = inputValue/73;
                iChar = (inputValue - (iTemp*73));
                inputValue = iTemp;
                sChar = codeValues.get(iChar);

                if (String.isNotEmpty(sChar)){
                    if (String.isNotEmpty(sPassword)){

                        sPassword = (sChar + sPassword);
                    }
                    else {
                        sPassword = sChar;
                    }
                }           
            }
            return sPassword;
        }



}
<apex:page controller="Decode">
  <!-- Pass parameters from visualforce page to controller -->
    <apex:form >
            <apex:pageblock >
                  Input Here = <apex:inputText value="{!inputValue}"/>
                 <apex:commandButton value="Submit" reRender="outputID" action="{!getDecode}"/>
            </apex:pageblock>
            <apex:pageblock >
                 <b>Output here = </b>
                 <apex:outputText value="{!sPassword}" id="outputID">
                 </apex:outputText>
            </apex:pageblock>
    </apex:form>
</apex:page>

2 Answers 2

1

You need to surface the value as a property. You also can't reference anything static on the page.

public with sharing class MyController
{
    public String inputValue { get; set; }
    public String sPassword { get; private set; }
    public void myMethod()
    {
        if (inputValue == null) return;
        try
        {
            sPassword = decode(Integer.valueOf(inputValue));
        }
        catch (TypeException t)
        {
            ApexPages.addMessages(t);
        }
    }
}

Then you would simply add myMethod as an action attribute on your Submit button:

<apex:commandButton value="Submit" action="{!myMethod}" reRender="outputID"/>
2
  • Are you showing this method as how you recommend I refactor the class?
    – Allen Mann
    Commented Nov 4, 2016 at 14:42
  • This is code you would need to add to the Decode class. I just put MyController to make it more generic to the concept.
    – Adrian Larson
    Commented Nov 4, 2016 at 14:43
0

declare one more field "decodedValue" and put decoding logic into getter

<apex:page controller="Decode">
  <!-- Pass parameters from visualforce page to controller -->
    <apex:form >
            <apex:pageblock >
                  Input Here = <apex:inputText value="{!inputValue}"/>
                 <apex:commandButton value="Submit" reRender="outputID"/>
            </apex:pageblock>
            <apex:pageblock >
                 <b>Output here = </b>
                 <apex:outputText value="{!decodedValue}" id="outputID">
                 </apex:outputText>
            </apex:pageblock>
    </apex:form>
</apex:page>

controller code

public with sharing class Decode {
    public Integer inputValue{get;set;}
    public String decodedValue{get{
        String sPassword;
        Integer iTemp;
        Integer iChar;
        String sChar;
        if(this.inputValue==null)inputValue=0;
        Map<Integer,String> codeValues = new Map<Integer,String>();
            codeValues.put(12,'A');
            codeValues.put(28,'B');
            codeValues.put(19,'C');
            codeValues.put(5, 'D');
            codeValues.put(24,'E');
            codeValues.put(10,'F');
            codeValues.put(1, 'G');
            codeValues.put(17,'H');
            codeValues.put(30,'I');
            codeValues.put(27,'J');
            codeValues.put(4, 'K');
            codeValues.put(31,'L');
            codeValues.put(8, 'M');
            codeValues.put(33,'N');
            codeValues.put(13,'O');
            codeValues.put(36,'P');
            codeValues.put(6, 'Q');
            codeValues.put(21,'R');
            codeValues.put(2, 'S');
            codeValues.put(32,'T');
            codeValues.put(15,'U');
            codeValues.put(11,'V');
            codeValues.put(22,'W');
            codeValues.put(7, 'X');
            codeValues.put(25,'Y');
            codeValues.put(34,'Z');
            codeValues.put(14,'1');
            codeValues.put(23,'2');
            codeValues.put(9, '3');
            codeValues.put(16,'4');
            codeValues.put(18,'5');
            codeValues.put(3, '6');
            codeValues.put(29,'7');
            codeValues.put(26,'8');
            codeValues.put(20,'9');
            codeValues.put(35,'0');


        for (Integer i = 0; i < 5 ; i++){
            iTemp = this.inputValue/73;
            iChar = (this.inputValue - (iTemp*73));
            this.inputValue = iTemp;
            sChar = codeValues.get(iChar);

            if (String.isNotEmpty(sChar)){
                if (String.isNotEmpty(sPassword)){

                    sPassword = (sChar + sPassword);
                }
                else {
                    sPassword = sChar;
                }
            }           
        }
        return sPassword;
    }set;}

    public Decode() {
    }

}
2
  • Just fyi the empty constructor is defined by default. You do not need to define one yourself unless you define other parameterized constructors.
    – Adrian Larson
    Commented Nov 4, 2016 at 14:51
  • @Adrian Larson In this particular case the constructor is left on purpose to show that it was here and now it is empty.
    – Pavlonator
    Commented Nov 4, 2016 at 14:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .