Quantcast
Channel: Andy in the Cloud
Viewing all articles
Browse latest Browse all 119

Spring’14 Pre-Release : Updating Layouts in Apex

$
0
0

The upcoming Spring’14 Metadata API brings with it some great new architectural features.

  • Ability to create, update and delete immediately without having to callback for the result
  • Ability to read Metadata without having to use retrieve and unzip the response!
  • Ability to rename Metadata components
  • Plus a host of new Metadata types and extensions to existing

The most exciting for me is the first two, first up the ability to greatly simplify the use of the API, particularly in Apex. As per a previous blog post I covered the requirement to poll for the results of certain API operations via the checkSync and checkDeploy methods. Which involved the use of either Batch Apex or apex:actionPoller. Many posters on this blog and the GitHub repo found this awkward to code.

These original operations still exist and I’ll continue to support them, but we now have some much much easier variants that simply return immediately the result. Here is an example showing the new readMetadata operation in action!

		// Read the layout
		MetadataService.Layout layout =
			(MetadataService.Layout) service.readMetadata('Layout',
				new String[] { 'Test__c-Test Layout' }).getRecords()[0];

		// Add a button to the layout
		layout.customButtons = new List<String>{ 'googleButton' };

		// Update the layout
		List<MetadataService.SaveResult> result =
			service.updateMetadata(new MetadataService.Metadata[] { layout });

With a true read facility in the API, imagine the possibilities with the Layouts alone, such as merging layouts, dynamically adding or removing buttons, fields, sections from a selection of layouts the user selections. If your developing a packaged application, providing a means to upgrade your users layouts and picklists automatically!

No Batch Apex, no apex:actionPoller to be seen! Its still early days and the official docs have not been announced, but from what I’d expect from how the readMetadata operation is designed, you’re going to be able to retrieve other types as well, such as ListView, Reports etc. Suddenly creating Setup wizards has become much easier, as well as custom Setup reporting tools, for example retrieving all the layouts and dumping the fields used in a Custom Object for reporting!

No More Zip File Handling!

The other really exciting thing here is we finally don’t need the help of JSZip JavaScript library to extract Metadata information from a retrieve operation call. While I’m still quite pleased with how i managed to abstract this. i’m very pleased that we can now read Metadata in none Visualforce contexts, such as Batch Apex or Apex Triggers.

Automating the Changes to the Generated Code

As always the code generated by the platform via the WSDL2Apex ‘Generate Apex from WSDL‘ button didn’t quite generate the Apex needed. Each time I refresh the Apex wrapper I have had to manually apply the changes to get it working again. This time i invested some time to write a script, written itself in Apex, that will parse the generated code and patch it with the required changes. It uses a customer Iterator to read the lines of code and the Tooling API to update the Apex class when its done.

Working around xsi:type

Once again the use of xsd:extension in the Metadata API proved to be a challenge in getting the readMetadata operation to work. Since as I found before this XML Schema construct is not readily supported by WSDL2Apex or the Apex XML bindings at runtime. The main weakness is the lack of interpretation of the xsi:type attribute, which identifies which Metadata type (e.g. CustomObject, Layout etc) the XML represents. As soon as I tried the readMetadata operation it gave an error as it tried to de-serialise the Layout, into the base Metadata type (which only contains fullName).

To get things to work I had to create some new Apex classes representing the responses from the readMetadata operation for each of the Metadata types. Fortunately the new script i developed can be taught to generate these for me! I’ll eventually post more on the changes to get the readMetadata operation working on the GitHub repo Readme.

Spring’14 is around the corner!

I’ll formally update the library once Spring’14 has been rolled out to the production servers in February. Until then you can download the MetadataService.cls and MetadataServiceExamples.cls classes from this Gist here. You can try it out in your pre-release orgs if you like. I’ll also post a few more fancy examples as well.



Viewing all articles
Browse latest Browse all 119

Trending Articles