4

Is it possible to create a canvas app using Apex?

We're trying to distribute a managed package that has an easy install process for the user. Our back-end server requires that each customer has a different Canvas App Url. Ideally we want to be able to create a canvas app in a post-install script and then instruct the user to change the Canvas App Url to the one we have provided them.

2

I think instead of making your users to change Canvas App Url manually, why don't you make a visualforce page that will intake canvas app url from user and create the canvas app on click of a button. Canvas app using Apex can be created using Metadata API. You can refer this link, that presents connected app configurations via metadata API. https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_connectedapp.htm

This code can be used to create canvas app via Metadata API.

public void createCanvasApp (String yourEmail, String canvasAppUrl) {
    MetadataService.MetadataPort service = new MetadataService.MetadataPort();
    service.SessionHeader = new MetadataService.SessionHeader_element();
    service.SessionHeader.sessionId = UserInfo.getSessionId();
    MetadataService.ConnectedApp connectedApp = new MetadataService.ConnectedApp();
    connectedApp.fullName = 'Your_CanvasApp_Name';
    connectedApp.label = 'Your CanvasApp Label';
    connectedApp.contactEmail = yourEmail;
    connectedApp.canvasConfig.canvasUrl = canvasAppUrl; // make sure that this has https
    connectedApp.canvasConfig.accessMethod = 'Post';
    connectedApp.canvasConfig.locations = new List<String>{'Visualforce', 'Aura', 'PageLayout'}; // you can add even more options here
    connectedApp.oauthConfig.callbackUrl = canvasAppUrl; // or whatever user wants to
    connectedApp.oauthConfig.scopes = new List<String>{'Full'}; // you can add even more options here
    List<MetadataService.SaveResult> results = service.createMetadata(new  MetadataService.Metadata[] { connectedApp });
}

But before going for this just make sure that you have MetadataService class with you in your package. You can take it from here.

https://github.com/financialforcedev/apex-mdapi/blob/master/apex-mdapi/src/classes/MetadataService.cls

Hope this helps.

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.