0

Is there a javascript equivalent to the .getParameters method of the PageReference Class?

Elaboration of what I'm trying to do:

I am have a VFP search page and a CTI adapter. The CTI adpater pops call info into the VFP in the form of query string parameters.

Instead of using a getter to get the parameters from the page, I would like to get the parameters from within the VFP so I can add a conditional to check if those parameters have values.

If they do, that means the CTI has provided us some values which I can search on and I can invoke that search directly from the VFP like a normal search which will rerender my results table.

If they do not have values, are blank, or are null, I know that the CTI has not passed me anything so I don't need to do any search.

Specifically my concern with having the controller get the parameters is that if there is values and I do perform a search, I will be unable to rerender the search page with the results.

  • 1
    better explain what it is you are trying to achieve and in what context =) – glls Aug 14 '18 at 21:19
  • @glls It has been added! – S.B. Aug 14 '18 at 21:38
  • 1
    There is $CurrentPage.parameters.[param name ] that could be used. – Eric Aug 15 '18 at 12:49
1

Here is a simple method in JS:

function getParameterByName(name){
    var url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)");
    var results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

Just call it like getParameterByName("filterName"); and it should return the value of a param.

Are you using lighting for this or not for this?

  • I am using classic, but the above makes sense. I might give this a shot. – S.B. Aug 15 '18 at 14:02
  • Unfortunately, this does not work. The script is placed at the bottom of the VFP after an onLoad function, and it functions, but does not find the params whereas the .getCurrentParameters does. – S.B. Aug 15 '18 at 14:19
  • @S.B. You do not have to add it to the onLoad part since this is a plain JS code. You can put it on the top of the page in script tags and should work. – Paul Aug 17 '18 at 13:13
0

As a solution to the problem (not the question), I ended up using ApexPages.currentpage.getparameters anyways. See example below where the CTI adapter is popping the VFP with the phoneNumberParameter.

VFP:

<apex:actionfunction action="{!updateSearchVariable}" rerender="thePageBlock name="updateCheckParameters">

<script>
        if (hasSearchBeenPerformed != 'true') {
        var hasSearchBeenPerformed = "{!hasSearchBeenPerformed}";
        document.onload = grabAndSetParameters();
        function grabAndSetParameters() {
             var phoneNumber = "{!phoneNumberFromClass}";
             var otherCallInfo = "{!otherCallInfoFromClass}";

             if (phoneNumber != null && phoneNumber != '') {
            ...DO WHATEVER YOU WANT TO DO WITH THE PASSED VARIABLES SUCH AS SET THE VALUE OF FIELDS ON THE SCREEN TO THESE VALUES...
             }
        updateCheckParameters();
        }

</script>

CLASS:

public class searchController {
     public string phoneNumberFromClass {get;set;}
     public string otherClassInfoFromClass {get;set;}
     public boolean hasSearchBeenPerformed {get;set;}

     public searchController() {
     parametersPassed = ApexPages.currentPage().getParameters();
     if(parametersPassed.get('phoneNumberParameter')!= null && String.isNotBlank(parametersPassed.get('phoneNumberParameter')) ){
            phoneNumberFromClass = parametersPassed.get('phoneNumberParameter');
     }

     public void updateSearchVariable() {
     hasSearchBeenPerformed = true;
     }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.