0

I am trying to query fee records in function check(). alert(cb.id);is returning correct id but query is not returning anything. alert('qr '+qr); function is not working.

catch is throwing Error Cannot read property 'query' of undefined vf page

<apex:page standardController="Fee__c" recordSetVar="c" id="p">
<apex:form id="f">


            <!---- FEES TAB ---->
            <apex:tabPanel>
            <apex:tab label="Items" name="fees">

                <!-- FEES -->
                <apex:pageblockSection columns="1" title="Fees"  >
                <apex:pageBlocktable  value="{!feeVOs}" var="fee" >
                    <apex:column headerValue="Pay" >
                     <input type="checkbox" onclick="check(this)"/>
                    </apex:column> 
                </apex:pageBlocktable>
                </apex:pageblockSection>

                <script>

   function check(cb){
    alert(cb.id);
    try{        
     var qr = sforce.connection.query('SELECT id,name FROM MUSW__Fee__c 
      where id IN (\'' + cb.id+ '\')');
    }catch(error){
    alert(error);
     }
                var records = qr.getArray("records");
                alert('qr '+qr);
                alert(records );
                    }
                }
                </script>
            </apex:tab>
                </apex:tabPanel>      
<apex:insert name="pageScript"/>

</apex:form>

4 Answers 4

1

Your query is asking Salesforce for records whose Ids are equal to "cb.id" - the literal text "cb.id". You need to populate the actual Id value into your query:

var qr = sforce.connection.query('SELECT id,name FROM MUSW__Fee__c where id IN (\'' + cb.id + '\')');

If appropriate for your browser support level, you could also use a template literal, but you can't use Apex style : binding or simply place the variable name in the string.

1
  • I have taken your comment and also using try catch.Catch is throwing error Cannot read property 'query' of undefined vf page. I have updated my question. Please have a look now.
    – devforce
    Commented Mar 30, 2018 at 11:00
0

Try to query like this:

var qr = sforce.connection.query('SELECT id,name FROM MUSW__Fee__c where id IN (\'' + cb.id+ '\')' );

You have used IN clause which accepts collection, so try providing it in single quotes.

See if this helps

1
  • I have taken your comment and also using try catch.Catch is throwing error Cannot read property 'query' of undefined vf page. I have updated my question. Please have a look now.
    – devforce
    Commented Mar 30, 2018 at 11:01
0

I missed sforce.connection.sessionId = '{!$Api.Session_ID}'; and <script src="/soap/ajax/33.0/connection.js" type="text/javascript"></script> . working code:

<apex:page standardController="Fee__c" recordSetVar="c" id="p">
    <apex:form id="f">
        <!---- FEES TAB ---->
        <apex:tabPanel>
        <apex:tab label="Items" name="fees">

            <!-- FEES -->
            <apex:pageblockSection columns="1" title="Fees"  >
            <apex:pageBlocktable  value="{!feeVOs}" var="fee" >
                <apex:column headerValue="Pay" >
                 <input type="checkbox" onclick="check(this)"/>
                </apex:column> 
            </apex:pageBlocktable>
            </apex:pageblockSection>

            <script>
            <script src="/soap/ajax/33.0/connection.js" type="text/javascript"></script>

      function check(cb){
     alert(cb.id);
        sforce.connection.sessionId = '{!$Api.Session_ID}';
    try{        
      var qr = sforce.connection.query('SELECT id,name FROM MUSW__Fee__c 
       where id IN (\'' + cb.id+ '\')');
       }catch(error){
      alert(error);
       }
            var records = qr.getArray("records");
            alert('qr '+qr);
            alert(records );
                }
            }
            </script>
        </apex:tab>
            </apex:tabPanel>      

0

sforce.connection is only available if you include the AJAX Toolkit. For Visualforce, the ideal situation is to use Remote Objects, which do not consume API calls.

<apex:remoteObjects jsNamespace="ObjectModel">
    <apex:remoteObjectModel name="MUSW__Fee__c" fields="Id,Name"/>
</apex:remoteObjects>

<script>
function check(cb){

   var model = new ObjectModel.MUSW__Fee__c();
   model.retrieve({ where: { Id: { eq: cb.id } } },
   function (err, records) {
     // process results
   });
 }
 </script>

You can read more here.

2
  • Thanks! I will check this concept but I have one query: which method gets executed faster. I have noticed sforce.connection.query takes 1-2 seconds. Do you think Remote objects can help in that.
    – devforce
    Commented Mar 31, 2018 at 13:08
  • @devforce Remote Objects is a tiny bit slower. I actually tested this. I'm not sure what's up with your query, as you'd have to query ~1k records before you reached 1 second delay to get the results. Ultimately, if your query is slow returning records, the mechanism you use doesn't really matter; but you'll burn through API calls needlessly if you use sforce.connection, so I would advise against it if you can help it.
    – sfdcfox
    Commented Mar 31, 2018 at 15:34

You must log in to answer this question.

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