0

emphasized text

<apex:page >
<script>
var result = sforce.connection.query("Select Name,Id from User", {
onSuccess : success, onFailure : failure});
function success(result) {
var it = new sforce.QueryResultIterator(result);
while(it.hasNext()){
var record = it.next();
log(record.Name + " -- " + record.Id);
}
}
function failure(error) {
log("An error has occurred " + error);
}

</script>
  <apex:form >
  <apex:pageBlock >
  <apex:commandButton value="get user info" onclick="chechDevMode();"/>
  </apex:pageBlock>
  </apex:form>
</apex:page>

this is my vf page,,am trying to get user info,by clcking the button get user info,,pls suggest how to do it.thanks in advance.

7
  • do you want to get current user details ? if yes no need use soql. you can use visualforce global variable $user ....... developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/…
    – Ratan Paul
    Commented Jun 29, 2016 at 6:15
  • using Ajax i hav to get the user info
    – Satya
    Commented Jun 29, 2016 at 6:17
  • try like this "Select Name,Id from User WHERE Id = '{!$user.Id}' "
    – Ratan Paul
    Commented Jun 29, 2016 at 6:19
  • not working sir
    – Satya
    Commented Jun 29, 2016 at 6:20
  • what is not working.. are you using above same code.. I can't see you are using connection.js in your page
    – Ratan Paul
    Commented Jun 29, 2016 at 6:27

1 Answer 1

0

try this code..

You need to add connection.js else you will get error like sforce is not define..

or you can use $user global variable to get the current user info..

Since you need an example to get the sforce.query result

Example

<apex:page >
    <script src="../../soap/ajax/37.0/connection.js" type="text/javascript"></script>

    <script>
    sforce.connection.sessionId='{!$Api.Session_ID}';
    var result = sforce.connection.query("Select Name,Id from User WHERE Id = '{!$User.Id}'", {
    onSuccess : success, onFailure : failure});
    function success(result) {
        var it = new sforce.QueryResultIterator(result);
        while(it.hasNext()){
        var record = it.next();
        console.log(record.Name + " -- " + record.Id);
        }
    }
    function failure(error) {
        console.log("An error has occurred " + error);
    }

    </script>
  <apex:form >
      <apex:pageBlock >
        <apex:commandButton value="get user info" onclick="chechDevMode();"/>
      </apex:pageBlock>
  </apex:form>
</apex:page>

one more simple solution that I like most using $user global variable

<apex:page>
<apex:form >
      <apex:pageBlock >
        <apex:commandButton value="get user info" onclick="chechDevMode();"/><br/>

        <h1 >{!$User.FirstName} {!$User.LastName}</h1> <br/>
        <h1 >{!$User.Id}</h1>
      </apex:pageBlock>
    </apex:form>
</apex:page>
10
  • I did copy paste yur code,iam not getting whether the info is getting or not,,can u please tel how to bind the result in the same page
    – Satya
    Commented Jun 29, 2016 at 6:33
  • @sahisfdc use javascript bind the value into element
    – Ratan Paul
    Commented Jun 29, 2016 at 6:34
  • in the {!result} format
    – Satya
    Commented Jun 29, 2016 at 6:36
  • @sahisfdc I didn't get you.. what is this {!result} ?
    – Ratan Paul
    Commented Jun 29, 2016 at 6:37
  • am not getting how to bind user info
    – Satya
    Commented Jun 29, 2016 at 6:42

You must log in to answer this question.

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