1

I want to access a custom field of my administrator profile in a Visualforce page which has a standard controller i.e. "Company__c". I want to access the custom field in javascript portion of that visualforce page. How can I do this?

my example Code is :

<apex:page standardController="Company__c" extensions="CompaniesHouseExtension" 
showHeader="true" sidebar="true">

    <script>
        var myOpenWindow = function() {
            var URL = "";
            var data_server = "{!Data_Service_Provider__c}";
            if (data_server == "Website1.com"){
                URL = "/apex/Website1";
            }else if (data_server == "Website2.com"){
                URL = "/apex/Website2";
            }

            var myOpenedWindow = myOpenedWindow || window.open(URL);
            myOpenedWindow.location.href= URL;
            myOpenedWindow.focus();
        }   
    </script>

<apex:page>

Here I have a picklist saved in my profile. Here I select a value , then according to the selection, I want to get data from a website. Now I want the selection in javascript code written above.. I want to assign the selected value from profile to Variable "data_server" and want to make condition as above.

By using this method, I am getting error:

"Previous load of class failed: Class3: line 24, column 54: Constructor not defined: [Class1].<Constructor>()" etc....

How can I solve this?

2
  • By using this method, I am getting error, "Previous load of class failed: Class3: line 24, column 54: Constructor not defined: [Class1].<Constructor>()" etc.... Commented Dec 14, 2015 at 15:03
  • You said you want to access a "custom field of my administrator profile". What SObject does this field reside on in Salesforce? To my knowledge, custom fields are not supported on the Profile SObject.
    – dsharrison
    Commented Dec 14, 2015 at 17:33

1 Answer 1

1

Here I myself got answer to the question. :) Here we can use apex class as extensions in visualforce page and in the class, we can use SOQL query to get User profile field value and can use it in javascript portion of the page to use it conditionally according to our need.

The running code is :

<-------- Apex Class Extension--------->

public class MyApexClassExtension { public string DataServiceProvider; public User currentUser;

public CompaniesHouseExtension(ApexPages.StandardController standardController){
    currentUser = [SELECT Id, Data_Service_Provider__c FROM User WHERE Id = :UserInfo.getUserId()];
    DataServiceProvider = currentUser.Data_Service_Provider__c;
}

public void setDataServiceProvider(string apikey){
    DataServiceProvider = apikey;    
}

public string getDataServiceProvider(){
    return currentUser.Data_Service_Provider__c;
}

}

And The visualforce page is :

<------- Visualforce Page ------->

<(apex:page standardController="Company__c" extensions="MyApexClassExtension" showHeader="true" sidebar="true">)

<(script>
    var myOpenWindow = function() {
        var URL = "";

        var data_server = "{!DataServiceProvider}";
        if (data_server == "Website1.com"){
            URL = "/apex/Website1";
        }else if (data_server == "Website2.com"){
            URL = "/apex/Website2";
        }
        var myOpenedWindow = myOpenedWindow || window.open(URL);
        myOpenedWindow.location.href= URL;
        myOpenedWindow.focus();
    }

</script>)

(/apex:page)

Here you can see that I am using variable of apex class extension in javascript portion of visualforce page in the format "{!VariableName}".

Hence I have achieved my task. Thanks for help stack exchange..

Hope It will help many others too..

You must log in to answer this question.

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