0

Need to count the no. of clicks on the image(in the public page) using the Remote action method.

Need some help in achieving this ?

VF Code:

<apex:actionregion >
        <apex:image id="edit" url="{!URLFOR($Resource.AYKE_ThumbsUP)}">
            <apex:actionSupport event="onclick" reRender="none" action="{!ThumbsUpCount}">
                <apex:param name="Language" value="{!Lang}"/>
                <apex:param name="country" value="{!country}"/>
                <apex:param name="faqid" value="{!faqid}"/>
            </apex:actionSupport>
        </apex:image>
    </apex:actionregion> 

Method :

@RemoteAction
    global static string ThumbsUpCount(string language,string country,String faqid) {
    String searchquery = '';
    if(  faqID != '' && language != '' && country!= '')  
    searchquery = 'select Description__c, ID, Title, Summary FROM FAQ__kav WHERE (PublishStatus=\'Online\' AND Language =\'' + language+ '\')' + ' AND id=\'' + faqID + '\'';
    FAQ__kav fqv = new FAQ__kav();
    fqv = database.query(searchquery);
    faqid = fqv.id;
    system.debug('########################' + fqv);
        FAQ_User_Rating_Summary__c fq = new FAQ_User_Rating_Summary__c();
        // Look for Existing FAQ Rating Summary
        list<FAQ_User_Rating_Summary__c> existingFAQ = new list<FAQ_User_Rating_Summary__c>([SELECT Id, Thumps_Up_Count__c FROM FAQ_User_Rating_Summary__c WHERE FAQ_Id__c=:faqid limit 1]);                                                         // Assuming there will be one Rating summary per FAQ.
        if( !existingFAQ.isEmpty() ){
            fq = existingFAQ.get(0);
            fq.Thumps_Up_Count__c = fq.Thumps_Up_Count__c != null ? fq.Thumps_Up_Count__c + 1 : 1; 
        }else{
            fq.Country__c = country;
            fq.FAQ_Id__c= fqv.id;
            fq.Language__c= language;
            fq.Thumps_Up_Count__c = 1;
        }
        upsert fq;
        return string.valueof(fq.Thumps_Up_Count__c);

}

Its not detecting the remote action method at all. Need your help in achieving this.

Thanks

4
  • You can't call remote action directly from <apex:actionSupport >
    – NITHESH K
    Sep 6 '17 at 6:08
  • Correct me if i can use Visualforce.remoting.Manager.invokeAction to call the remote action method ?
    – B1 sfdc
    Sep 6 '17 at 6:10
  • Yes correct ...
    – NITHESH K
    Sep 6 '17 at 6:11
  • Your welcome...
    – NITHESH K
    Sep 6 '17 at 6:36
1

You can't access @RemoteAction directly from <actionSupport/>. JavaScript remoting in Visualforce provides support for some methods in Apex controllers to be called via JavaScript.

JavaScript Remoting for Apex Controllers

JavaScript remoting is a tool that front-end developers can use to make an AJAX request from a Visualforce page directly to an Apex controller. JavaScript remoting allows you to run asynchronous actions by decoupling the page from the controller and to perform tasks on the page without having to reload the entire page.

Javascript

<script type="text/javascript">
function getThumbsUpCount(language,country,faqid) {
    Visualforce.remoting.Manager.invokeAction(
        '{!$RemoteAction.ClassName.ThumbsUpCount}',
        language,country,faqid,
        function(result, event){
            if (event.status) {
                // on success   
                alert(result);
            } else if (event.type === 'exception') {
                // on exception 
            } else {
                // on error
            }
        }, 
        {escape: true}
    );
}
</script>

apex:

    <apex:actionregion >
         <a href="#" title="ThumbsUp" onclick="getThumbsUpCount('{!Lang}','{!country}','{!faqid}')">    
         <apex:image id="edit" url="{!URLFOR($Resource.AYKE_ThumbsUP)}" />    
         </a> 
    </apex:actionregion> 

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.