4

I can't dynamically set it with javascript because the apex:param element isn't rendered in HTML, so I'm not sure what I should do to set apex:param's value. I need the value to be a javascript variable. Right now, I have the value hardcoded to be '123' but I want this to be the value of a javascript variable.

VF:

<apex:form>
  <apex:commandLink styleClass="slds-button slds-button--neutral" action="{!back}">
      <apex:param name="customRoles" id="customRoles" value="123" assignTo="{!custom_roles}"/>
    <svg aria-hidden="true" class="slds-button__icon slds-button__icon--left">
      <use xlink:href="{!URLFOR($Resource.lightning, '/assets/icons/utility-sprite/svg/symbols.svg#back')}"></use>
    </svg>
    Back
  </apex:commandLink>
</apex:form> 

Apex:

public String custom_roles {get;set;}
1
  • 1
    can u share your code and highlight what you are trying to do and where you need help.. it will help the users to answer better Dec 7, 2015 at 22:27

2 Answers 2

3

I added your code in a vf page and tried to get the apex:param variable, but it returned null.

This is how I used to do with typical VF pages,

  1. Create an actionfunction tag in your page and define the same param
  2. Add an onlick event for commandLink, and call a JS method

<apex:form>
 <apex:commandLink styleClass="slds-button slds-button--neutral" onclick="assignParamValue();">
    <svg aria-hidden="true" class="slds-button__icon slds-button__icon--left">
      <use xlink:href="{!URLFOR($Resource.lightning, '/assets/icons/utility-sprite/svg/symbols.svg#back')}"></use>
    </svg>
    Back
  </apex:commandLink>

  <apex:actionFunction name="callme" action="{!back}">
        <apex:param name="customRoles" id="customRoles" value="" assignTo="{!custom_roles}"/>
  </apex:actionFunction>
</apex:form>

and then call action function using JS

<script>
    function assignParamValue(){
        var abc ='123';
        callme(abc);
    }
</script>
2
  • I tried this, but there's a race condition (i suspect) between the action="{!back}" and callme(abc) because the apex param never gets set. the !back is an apex function that redirects you to another page
    – user22261
    Dec 8, 2015 at 0:52
  • Have you removed the back action from CommandLink and add it to ActionFunction? Dec 8, 2015 at 0:57
0

You can also use Remote Actions. They're nice because:

Your method can take Apex primitives, collections, typed and generic sObjects, and user-defined Apex classes and interfaces as arguments.

The down-side is that @RemoteAction Methods must be static, so they can't directly affect your View State.

Example:

Controller

public class Requestor {

    public class Response {
        public Id requestId {get;set;}
        public Boolean isSuccess {get;set;}
        public String errorMessage {get;set;}
    }


    @RemoteAction
    public static Response processRequest(Id requestId, String status, String comments){
        Response response = new Response();

        Boolean isValidRequest = requestId != null && String.isNotBlank(status) && String.isNotBlank(comments);

        response.errorMessage = isValidRequest ? null :  'Request ID, Status, and Comments are required to Process a Request.';

        Request__c request = new Request(
            Id = requestId,
            Status__c = status,
            Comments__c = comments
        );

        Database.SaveResult saveResult;
        try {
            saveResult = isValidRequest ? update request : null;
        } catch(Exception whoops){
            response.errorMessage = whoops.getMessage();
        }
        response.errorMessage = String.isBlank(errorMessage) && saveResult != null && !saveResult.isSuccess() ? String.join(saveResult.errors(), ',') : response.errorMessage;

        response.isSuccess = String.isBlank(response.errorMessage);
        return response;
    }

}

Visualforce Page

<apex:page controller="Requestor">

    <script>
        function processRequest(requestId, status, comments){
            Requestor.processRequest(
                requestId,
                status,
                comments,
                function(response){
                    // Handle Response
                } 
            );
        }
    </script>

</apex:page>

You must log in to answer this question.

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