0
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="/soap/ajax/33.0/connection.js" type="text/javascript"></script>
<script src="/soap/ajax/33.0/apex.js" type="text/javascript"></script>

<script type="text/javascript">  

 function soqlQuery(){          
      try{
          var currentRecId="{!$CurrentPage.parameters.Id}";
          console.log(currentRecId);
          var result = sforce.connection.query("SELECT StageName FROM Opportunity WHERE Id:currentRecId",{
              onSuccess : function(result){
                  alert('Get the stagename');
                  console.log(result);
              },
              onFailure : function(error){
                    alert('No');
              }
          });
      }catch(e){
          alert(e);
      }
  }
  window.onload=function(){
      soqlQuery();
  }
 </script>

I try to fetch one field value (stageName) from Opportunity sobject using currentPage's record Id. But This will return "No(alert)"

How can i solve it? Also I prefer to fetch API name ...not field label name

Here is the screenshots from Chrome's Developer console (console and network tabs)

enter image description here

enter image description here

7
  • Please post errors from Chrome's Developer console (network tabs) to help us out.
    – dzh
    Jul 5 '18 at 9:12
  • I uploaded screenshots Please check it
    – user56111
    Jul 5 '18 at 17:53
  • What message are you getting from the error parameter that is passed to the onFailure method?
    – Kyle
    Jul 5 '18 at 21:26
  • Im sorry I dont understand you. After I saved this VF, I got " No" which is from the onFailure method. What I expect is to get "'Get the stagename'" which is from the onSuccess method
    – user56111
    Jul 6 '18 at 19:53
  • @KazuSuzuki You have onFailure : function(error){ alert('No'); }. Instead of alerting 'No', instead output what is in error, like onFailure : function(error){ alert(error); } You may need to check other parameters on the error object to see that actual error message.
    – Kyle
    Jul 6 '18 at 20:39
0

Use this way..

var currentRecId="{!$CurrentPage.parameters.Id}";

var result = sforce.connection.query("SELECT StageName FROM Opportunity WHERE Id='" + currentRecId +"'",{....
3
  • I tried but it doesnt work
    – user56111
    Jul 4 '18 at 10:12
  • Check the single quote Jul 4 '18 at 10:12
  • i tried it but it doesnt work too
    – user56111
    Jul 4 '18 at 10:14
0
<script type="text/javascript">  ***sforce.connection.sessionId='{!GETSESSIONID()}'***;function soqlQuery(){          
  try{
      var currentRecId="{!$CurrentPage.parameters.Id}";
      console.log(currentRecId);

      var result = sforce.connection.query("SELECT StageName FROM Opportunity WHERE Id =\'"+currentRecId+"\'" ,{
          onSuccess : function(result){
              alert('Get the stagename');
              console.log(result);
          },
          onFailure : function(error){
                alert('No');
          }
      });
  }catch(e){
      alert(e);
  }  window.onload=function(){
  soqlQuery();}</script>

@dzh Thank you very much I checked 500 (43.0, innitiator connetion.js). Then I understand one of problems was about sessionId.

And one more problem was about SOQL.

Your Answer

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