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
Dan1126Dan1126 

Return Visualforce Component from JavaScript remoting?

Hey everyone,

I am trying to use JavaScript Remoting to return a dynamic Visualforce component. My use case is that a user can select from a variety of actions, each corresponding to a pre-defined Visualforce Component. Once they select an action the component should be rendered dynamically. When I attempt this, nothing is returned and I receive the following error in the browser's console: Visualforce Remoting Exception: Internal Service Error: java.lang.NullPointerException (342168595-56884 (1246936158)). I've also attempted to return the Component directly from the JS Remoting method, which generated the same error. 

Does anyone know what I might be doing wrong? Or if there is an alternative way of accomplishing this task (generating a specific Visualforce component from a user click)?

Here is my code: 

Visualforce Page:
<apex:page controller="PageCtrl">
    <script type="text/javascript">
        var openComponent = '{!$RemoteAction.PageCtrl.openComponent}';

        function open() {
            Visualforce.remoting.Manager.invokeAction(
                openComponent,
                function(result, event) {
                    console.log(result);
                    console.log(event);
                }
            );
        }
    </script>

    <apex:dynamicComponent componentValue="{!selectedComponent}" />
</apex:page>

Page Controller:
public with sharing class PageCtrl
{
    public static ApexPages.Component serviceActionComponent {get; set;}

    @RemoteAction
    public static void openComponent()
    {
        selectedComponent = new Component.ExampleComponent();
    }
}

Thanks in advance for any help. 
 
LBKLBK
Hi Dan,

It may be that I am not seeing the entire code.

But, selectedComponent property is not seemed to be defined anywhere.

Can you confirm that?
Dan1126Dan1126
Hi LBK,

Thanks for your response. I changed the variable names for clarity and missed that one, it's actually the serviceActionComponent variable defined in the controlller.