function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
LB_DDLB_DD 

Different controllers to VF page and RemoteAction, is possible?

Hello,

 

Have a VF page associated to a controller and want use another controller to receive @RemoteAction calls, is possible?

 

Example:

 

Visuaforce page

<apex:page standardStylesheets="false" docType="html-5.0" sidebar="false" showHeader="false" contentType="text/html" cache="true" controller="test_ControllerRA">
  <h1>Congratulations</h1>
  This is your new Page
 <hr />
 <p><a href="#" onclick="showMessage();">Press Me</a></p>
 <div id="zoneShowMsg">zone to show message</div>
 
 <script>
 function showMessage()
{
        var str = 'name=TheName&info=Info+Tag';

        document.getElementById('zoneShowMsg').innerHTML = 'Processing... please wait.';
   try{        
        test_ControllerGlobalRA.processRA(str , function(Result, event)
        {
        console.log('Result:',Result);
        console.log('event',event);
            if (event.status)
            {
                console.log('Result=',Result);                
                if(Result!= '')
                {                      
                      document.getElementById('zoneShowMsg').innerHTML = 'Information:'+Result;
                }
                else
                {
                      document.getElementById('zoneShowMsg').innerHTML = 'Problems with information:'+Result;
                }
            } 

        }, {escape:true}); 
    }catch(ex){
        document.getElementById('zoneShowMsg').innerHTML = 'ERROR:'+ex;
    }       
}
 </script>
</apex:page>

 

The VF Page Controller:

public with sharing class test_ControllerRA {

    public String getTheString(String str){
    
        String[] arrStr = str.split('&');
        String theText = 'Name: '+arrStr[0]+' / ';
        theText +='Info: '+arrStr[1]+' /// (finished)';
        
        return theText;
    }
}

 

The @RemoteAction controller:

global class test_ControllerGlobalRA{
    @RemoteAction
    global static String processRA(string Str){
        test_ControllerRA tcra = new test_ControllerRA();
        String theStr = tcra.getTheString(Str);
        return theStr;
    }
}

 

I want to use the VF page inside a iframe.

 

 

If anyone can help me, I thank.

 

Best Regards,

LB

 

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

Add the extension attribute in apex page

Try the below modified code snippet

<apex:page standardStylesheets="false" docType="html-5.0"  contentType="text/html" cache="true" controller="test_ControllerRA" extensions="test_ControllerGlobalRA">

  <h1>Congratulations</h1>

  This is your new Page

 <hr />

 <p><a href="#" onclick="showMessage();">Press Me</a></p>

 <div id="zoneShowMsg">zone to show message</div>

 

 <script>

 function showMessage()

{

        var str = 'name=TheName&info=Info+Tag';

 

        document.getElementById('zoneShowMsg').innerHTML = 'Processing... please wait.';

   try{       

        test_ControllerGlobalRA.processRA(str , function(Result, event)

        {

            if (event.status)

            {

                console.log('Result=',Result);               

                if(Result!= '')

                {                     

                      document.getElementById('zoneShowMsg').innerHTML = 'Information:'+Result;

                }

                else

                {

                      document.getElementById('zoneShowMsg').innerHTML = 'Problems with information:'+Result;

                }

            }

 

        }, {escape:true});

    }catch(ex){

        document.getElementById('zoneShowMsg').innerHTML = 'ERROR:'+ex;

    }      

}

 </script>

</apex:page>

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

Add the extension attribute in apex page

Try the below modified code snippet

<apex:page standardStylesheets="false" docType="html-5.0"  contentType="text/html" cache="true" controller="test_ControllerRA" extensions="test_ControllerGlobalRA">

  <h1>Congratulations</h1>

  This is your new Page

 <hr />

 <p><a href="#" onclick="showMessage();">Press Me</a></p>

 <div id="zoneShowMsg">zone to show message</div>

 

 <script>

 function showMessage()

{

        var str = 'name=TheName&info=Info+Tag';

 

        document.getElementById('zoneShowMsg').innerHTML = 'Processing... please wait.';

   try{       

        test_ControllerGlobalRA.processRA(str , function(Result, event)

        {

            if (event.status)

            {

                console.log('Result=',Result);               

                if(Result!= '')

                {                     

                      document.getElementById('zoneShowMsg').innerHTML = 'Information:'+Result;

                }

                else

                {

                      document.getElementById('zoneShowMsg').innerHTML = 'Problems with information:'+Result;

                }

            }

 

        }, {escape:true});

    }catch(ex){

        document.getElementById('zoneShowMsg').innerHTML = 'ERROR:'+ex;

    }      

}

 </script>

</apex:page>

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

 

This was selected as the best answer
LB_DDLB_DD

Hello Navatar_DbSup,

 

Thanks a lot, works :)

 

To all users with same problem, I post the final code to testing:


Visualforce Page:

<apex:page standardStylesheets="false" docType="html-5.0" sidebar="false" showHeader="false" contentType="text/html" cache="true" controller="test_ControllerRA" extensions="test_ControllerGlobalRA">
  <h1>Congratulations</h1>
  This is your new Page
 <hr />
 <p><a href="#" onclick="showMessage();">Press Me</a></p>
 <div id="zoneShowMsg">zone to show message</div>
 
 <script>
 function showMessage()
{
        var str = 'name=TheName&info=Info+Tag';

        document.getElementById('zoneShowMsg').innerHTML = 'Processing update... please wait.';
   try{        
        test_ControllerGlobalRA.processRA(str , function(Result, event)
        {
        console.log('Result:',Result);
        console.log('event',event);
            if (event.status)
            {
                 console.log('Result=',Result);
                
                if(Result!= '')
                {                      
                      document.getElementById('zoneShowMsg').innerHTML = 'Information:'+Result;
                }
                else
                {
                      document.getElementById('zoneShowMsg').innerHTML = 'Problems with information:'+Result;
                }
            } 

        }, {escape:true}); 
    }catch(ex){
        document.getElementById('zoneShowMsg').innerHTML = 'ERROR:'+ex;
    }       
}
 </script>
</apex:page>

 

The VF PageController:

public with sharing class test_ControllerRA {

    public String getTheString(String str){
    
        String[] arrStr = str.split('&');
        String theText = 'Name: '+arrStr[0]+'<br />';
        theText +='Info: '+arrStr[1]+'<br />(finished)';
        
        return theText;
    }
}

 

 

 

The @RemoteAction Controller:

global class test_ControllerGlobalRA{

    public test_ControllerGlobalRA(test_ControllerRA controller) {

    }

    @RemoteAction
    global static String processRA(string Str){
        test_ControllerRA tcra = new test_ControllerRA();
        String theStr = tcra.getTheString(Str);
        return theStr;
    }

}

 

 

Best Regards,

LB