1

I have an apex controller and I would like to pass the variable I have stored using JS and HTML to the controller.

Attached below is the code I am using.

function userInfo() {
var txt;


 var person = prompt("Please enter your user:", "");
txt = "Hello " + person;
document.getElementById("usrID").innerHTML = txt;
<apex:form>
<apex:actionFunction name="setUSERID" action="{!setUserID}" rerender = "blank">
<apex:param name="userID" value

}

The controller code is below as follows:

public class userIDStore {
public String userID {get;set;}
public PageReference setUserID() {
userID = Apexpages.currentPage().getParameters().get('id');
return null;
}
}

I have been through many of the walk throughs and questions online, however, I am still confused.

I would like to use the value (in apex:param) and document.getElementById("usrID") to set the value for the variable but I am confused on how to do that with document.getElementById.

RROR MESSAGE BEING SHOWN:

Source map error: SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data Resource URL: https://menabev--mbpoc--c.cs89.visual.force.com/jslibrary/1526059412000/sfdc/VFState.js Source Map URL: /javascript/1530100944000/sfdc/source/VFState.js.map[Learn More]

Thank you in advance

6
  • you can set the value to apex:inputhidden field and access in apex controller
    – Annappa
    Jul 1, 2018 at 13:56
  • would the code look like <apex:inputHidden id="userID" value="{!usrID}"/>?
    – R83nLK82
    Jul 1, 2018 at 14:01
  • yes..you have to access that user id in apex controller
    – Annappa
    Jul 1, 2018 at 14:07
  • so the {!usrID} should reference the one in the controller or in the script?
    – R83nLK82
    Jul 1, 2018 at 14:10
  • apex controller. public string userId{get;set;}
    – Annappa
    Jul 1, 2018 at 14:15

1 Answer 1

1

Here is a simple solution using the <apex:inputHidden /> which still uses your prompt for the users name ( not necessary to really have this except to show something happening on the page )

Page:

    <apex:page controller="userIDStore" action="{!setUserID}" >
        <script>
            document.addEventListener( 'load', function() {
                var userIdentity = prompt( 'Enter your username: ', "" );

                if( userIdentity !== null || userIdentity !== undefined ){
                    document.getElementById( 'userMessage' ).innerHTML = "Hello " + userIdentity;
                }

            });
        </script>
        <apex:form>
            <apex:inputHidden id="usrId" value="{!userId}" />
            <apex:outputText id="userMessage" value="" />
        </apex:form>
    </apex:page>

Controller:

    public class userIDStore {
        public String userID {get;set;}

        // can use this for action to set the value on load using action
        public PageReference setUserID() {
            Map<String, String> parms = ApexPages.currentPage().getParameters();

            if( parms.containsKey( 'id' ) ){
                userID = parms.get( 'id' );
            }

            return null;
        }
    }
4
  • Hi thank you for your response, I am trying to use the code given above however I am stuck because I am checking the debugger and I am getting the error code Source Map Error: Syntax Error: JSON Parse. Not sure what I am doing wrong
    – R83nLK82
    Jul 3, 2018 at 8:00
  • can you update your example to where this is happening? nowhere in your post are you showing that you're parsing a JSON anywhere here.
    – Ronnie
    Jul 3, 2018 at 20:43
  • I agree with you completely, I do not see where JSON is being parsed. I added the error message for further reference.
    – R83nLK82
    Jul 4, 2018 at 6:41
  • i am not getting this error - granted, my code is only a small piece of what you probably have on your end. there may be other code on your side that is running that is not included in this post.
    – Ronnie
    Jul 4, 2018 at 15:02

You must log in to answer this question.

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