1

So I have created some code which essentially iterates through records on an object to map it to specific lightning components dependent on the recordtype. I have looked everywhere but can't seem to find anything about auto generating code.

Essentially what I am looking to do is when a record is inserted on my custom object I want it to automatically generate the lightning component code dependent on specific fields inserted on my object. With a view to release this on the AppExchange (Similar to DLRS).

Any ideas how I can accomplish this?

2
  • 2
    DLRS uses an Apex wrapper around the Metadata API to generate and dynamically deploy triggers. It's open source if you want to look through the code - I can't say I've ever dynamically deployed a component, but perhaps someone else can answer with more detail!
    – David Reed
    Apr 10, 2018 at 20:56
  • @DavidReed Thanks David Ill give that a look over now
    – Nebbyyy
    Apr 10, 2018 at 20:57

1 Answer 1

1

After must discussion internally it appears this 'Might' be possible through the Tooling API https://ashwanisoni.wordpress.com/2017/01/18/force-com-tooling-api-create-apex-class-apex-trigger-dynamically-and-updatedelete-apex-trigger-apex-class-using-tooling-api-with-rest-api/

Over the next couple of days I will be attempting to implement this solution and will update this answer with my findings.

UPDATE: So I have implemented a basic script to generate an auradefinationbundle to hold the component. We create this using the method below:

 webservice static String createBundle(String sbundleName)
    {
        JSONGenerator body = JSON.createGenerator(false);
        body.writeStartObject();
        body.writeStringField('DeveloperName', sBundleName);
        body.writeStringField('Description', 'Code Generated Bundle');
        body.writeStringField('ApiVersion', '41');
        body.writeStringField('MasterLabel', 'Test Label');
        body.writeEndObject();
        HttpRequest createReq = new HttpRequest();
        createReq.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v41.0/tooling/sobjects/AuraDefinitionBundle');
        createReq.setBody( body.getAsString() );
        System.debug('JSON BODY : ' +  body.getAsString());
        createReq.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
        createReq.setHeader('Content-Type', 'application/json');
        createReq.setMethod('POST');
        createReq.setTimeout(30000);
        Http h = new Http();   
        HttpResponse createRes = h.send(createReq);
        System.debug(createRes.getXmlStreamReader().toString());
        String returnId = createRes.getXmlStreamReader().toString().substringAfter('id":"').substringBefore('",');
        return returnId;
    }

Once created the aura bundle id is returned to be used in any other methods i.e to generate the component mark up like so.

 webservice static void createComponent(String sBundleId)
{
    JSONGenerator body = JSON.createGenerator(false);
    body.writeStartObject();
    body.writeStringField('AuraDefinitionBundleId', sBundleId);
    body.writeStringField('DefType', 'COMPONENT');
    body.writeStringField('Source','<aura:component implements="flexipage:availableForAllPageTypes,force:hasRecordId,force:lightningQuickAction" controller="CampaignUIController">MY COMPONENT</aura:component>');
    body.writeEndObject();
    HttpRequest createReq = new HttpRequest();
    createReq.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v41.0/tooling/sobjects/AuraDefinition');
    createReq.setBody( body.getAsString() );
    System.debug('JSON BODY : ' +  body.getAsString());
    createReq.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
    createReq.setHeader('Content-Type', 'application/json');
    createReq.setMethod('POST');
    createReq.setTimeout(30000);
    Http h = new Http();   
    HttpResponse createRes = h.send(createReq);
}

All in all this can be run from the Execute Anonymous window using the follow code.

String bundleId = createAuraDefBundle.createBundle('automaticCreateTest');
createAuraDefBundle.createComponent(bundleId);

Returning the aura bundle:

Created Bundle

Hope this helps Guys :)

4
  • 1
    Yes, AuraDefinitionBundle supports create() so we can do that by using tooling api...I've done similar...please let me know if you need any help! developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/…
    – Ayub
    Apr 19, 2018 at 19:49
  • ...have you developed the code to create Lightning component programmatically...if yes...would it be possible to share the code to create different bundles?
    – Ayub
    Apr 25, 2018 at 18:23
  • @AyubAnsari Not yet I have been juggling a few things here but I have this in the pipeline for early next week, so expect to be shared then.
    – Nebbyyy
    Apr 30, 2018 at 10:48
  • @AyubAnsari See Above, Vote up if this helps
    – Nebbyyy
    Apr 30, 2018 at 13:52

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.