I have to create lightning component on click of a button. This should happen in run time.

I tried to SForce connection for the same. But I have to create first AuraDefinitionBundle, then AuraDefinition for Component, controller, helper etc. I could successfully create a working Lightning component using sforce.

Is this right approach? Will there any different way to achieve this?

Below is the code which I used to create HELPER js.

<apex:page controller="TestApex">
<html xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" lang="en">
        <meta charset="utf-8" />
        <meta http-equiv="x-ua-compatible" content="ie=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <apex:slds />
        <apex:includeScript value="{!URLFOR($Resource.StaticResources, 'StaticResources/js/jquery-2.2.4.js')}"/>
        <script src="../../soap/ajax/39.0/connection.js" type="text/javascript"></script>

        <input type="hidden" id="Api_Session_ID" value="{!$Api.Session_ID}"/>
        <div class="slds-button-group" role="group">
            <button class="slds-button slds-button_neutral" id="toggleBtn" >Click Me</button>
    j$ = jQuery.noConflict();
        alert('helo' );
        var pagedata ="({ getMetho : function(cmp, helper){ console.log('Hello!'); } })";
        var connection = sforce.connection;
        connection.sessionId = jQuery('#Api_Session_ID').val();
        var newRecord= new sforce.SObject("AuraDefinition");
        newRecord.AuraDefinitionBundleId  ="0Ab6F000000g2QmSAI"
        newRecord.DefType   = "HELPER";
        newRecord.Format  = "JS";
        newRecord.Source  = pagedata;
        var result = sforce.connection.create([newRecord]);

        alert(result );


1 Answer 1


You cannot use sforce.connection.* methods to create metadata. This method only works for "normal" data, like Accounts, Contacts, and Custom Objects. More specifically, sforce.connection.* uses the SOAP API to perform updates, but you need the Tooling API or Metadata API. I would say that using the Tooling API is probably the easiest method. I suggest you check out the documentation, but in summary, you'll create a AuraDefinitionBundle followed by AuraDefinition objects. You can use a Composite Request in order to simplify deployment and reduce the API usage.

  • Thanks for your suggestion. But as I am sending this as REST API, I have to add endpoint in remote site settings. I am creating a managed package and want to install it in multiple org. In that case should this be action item to add remote site setting manually?
    – sf dev
    Oct 10, 2017 at 9:31
  • @sfdev See this question about adding a Remote Site in Visualforce/JavaScript. It should still work.
    – sfdcfox
    Oct 10, 2017 at 14:36

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.