Tuesday, November 1, 2016

Debugging an Outbound Message using a public facing Apex REST web service

I needed a way to capture the SOAP Requests that were being sent by an Outbound Message.

One option would be to host something on my local development machine. However, at work this would necessitate getting a network admin to open a port from our public IP address through to my machine.

Another suggestion, by Daniel Hoechst, was to use an existing online service like http://requestb.in/ to capture the SOAP message.

Instead I thought I'd explore using a public Salesforce Site that exposes a REST web service accepting all POST requests. One advantage of this approach is that the request doesn't need to go off to a third parties server. It's also simple to implement and is mostly just putting a few known parts together.

The first step is to add a public RESTful webservice on the Force.com Site.

The debug log would have been a good option to capture the request, but in Winter '17 a requirement was added to use a cookie to enable the logging - Set a Browser Cookie to Enable Debug Logging for Guest Users. It isn't possible to add a cookie to the outbound message. So, yeah cool. Thanks for that Winter '17 Winking Snowman.

Instead I've needed to create a basic custom object to store the RestRequest Body.

@RestResource(urlMapping='/SoapMessage')
global with sharing class RESTController {

  @HttpPost  
  global static String inboundSoap() {     
      RestRequest req = RestContext.request;
      
      DataCapture__c dc = new DataCapture__c();
      dc.Data__c = req.requestBody.toString();
      insert dc;
      
      return 'Done ' + dc.Id;
  }
}

Once the Apex class is ready a quick smoke test from POSTMAN to make sure it is working as expected.

Then configure the outbound message to POST to the REST services URL.

Example of a captured outbound message:

Saturday, October 22, 2016

Experiments in 3D printing

I've been experimenting with 3D printing recently after initially having similar opinions to Scott Hanselman on the subject:

The first 3D printed thing I ever saw was a florescent green Yoda head. First you're like "HOW DID YOU DO THIS?" but quickly you're more like "OK, how many brightly colored plastic pieces of crap do I really need in my life?" - Scott Hanselman

My previous impression was that the majority of things people printed were replacement parts for the printer itself.

But... then I got access to a printer to use for a few days and printed a few bits and pieces. I think the first thing I printed was a micro quadcopter frame to carry an equally small FPV camera.

Once I realized I could print things that could fly it sort of snowballed from there.

And fly they do...

Dreamforce

At the Chicago MVP Summit event and Midwest Dreamin' conference I'd taken along a handful of Deployment Fish to hand out to people. They were well received, which left me wondering if I could refine the idea for Dreamforce.

At first I thought I could do a 3D version of the Salesforce logo with a flash drive or Raspberry PI embedded.

The idea kind of worked, but I wasn't sure I wanted something that was the company's logo. Plus, I could never figure out how to correctly fill the middle of the cloud between all the defined spheres so it looked right.

I then tried some variations of the deployment fish concept. I really liked the idea of a punch out deployment fish, but the printer didn't have the resolution to do text for a business card. The other idea was a fish that would clip over a conventional business card.

In the end, keeping it simple seemed like the best idea. So I got some basic stickers printed with my details and put them on the back of the original designs. For good measure I did some Trailhead logos as well.

I also really wanted to get my Twitter handle on my conference badge. I had a number of people recognize me first by my handle. The fish was supposed to slot into the sharks mouth, but I didn't have time to refine it the day before departing for the conference.

Einstein

Shortly before Dreamforce the Einstein announcements started coming out. Putting Einstein in a plane just seemed like the thing to do.

I also played around with having the cloud be Einstein's brain.

Apex Hammer Test

Rumor has it that if you can catch one of each of the Seven Dwarf's they'll forge you a might Apex Test Hammer™ like the one that is used to check every orgs test cases during a seasonal release.

It may or may not bear a striking resemblance to Mjolnir with custom rune plates.

Quadcopter travel frame

I wanted to travel with my quadcopter but didn't want to draw attention to it strapped on the outside of a backpack. I made a basic case for it to slide into with the props removed. This was as much to protect the contents of the bag from it as it was to protect the quad.

Stuff for the kids

There is no end of things to print for the kids. I like the simplicity and usefulness of printing new train track connectors.

Sometimes things don't always go to plan

Sometimes the model isn't correct and you try and print a surface with no depth.

Sometimes the filament feed jams mid print.

Sometimes the entire print bed moves mid print.

If nothing else, 3D printing helps teach you perseverance. Happy printing!

Monday, October 17, 2016

Salesforce Tooling API Winter '17 (v38.0)

My saga continues. As the seasonal releases pass I'm beginning to suspect more and more that I'm the sole occupant of SOAP Tooling API town.

Previous encounters with the Tooling API include:

Today's post was encountered when updating to the Winter`17 (v38.0) Tooling API.
.NET would failed to update from the new WSDL with the error message:

Custom tool error: Unable to import WebService/Schema. Unable to import binding 'SforceServiceBinding' from namespace 'urn:tooling.soap.sforce.com'. Unable to import operation 'create'. The datatype 'urn:metadata.tooling.soap.sforce.com:FTestGeneratedEntityComplexValueType' is missing.

The useful bit is hiding at the end - "The datatype 'urn:metadata.tooling.soap.sforce.com:FTestGeneratedEntityComplexValueType' is missing.".

v38.0 of the Tooling API WSDL contains two new and really odd complex types. FTestGeneratedEntityFtest and FTestGeneratedEntityWithLongishNameFtest. The first of these refers to the second via the namespace prefix mns, which is "urn:metadata.tooling.soap.sforce.com". That's fine, except they are both defined within the "urn:sobject.tooling.soap.sforce.com" namespace.

Commenting these extra complex types out of the WSDL seems to be sufficient to get it going again.

Raised as support case 14900660 for those who want to play along at home.

Update: Now a known issue The datatype urn:metadata.tooling.soap.sforce.com:FTestGeneratedEntityComplexValueType is missing


Bonus Round!

The Tooling API now includes the dreaded QuickActionLayout complex type and the corresponding generated quickActionLayoutColumns multidimensional array. As such, the generated C# class will need the same fix that the Metadata API does.


Double Bonus Round!

The describeGlobal method on the Tooling API is returning 122 new sObjects in the autogen__ namespace. They all have the label "Metadata Facade Template" and the keyPrefix "0Dw". If you attempt to do a describeSObject or describeSObjects API call for any of these autogen__ types you get a GACK response.

Raised as support case 14909168.

Friday, October 14, 2016

Dreamforce 2016 Round-up / Summary

This was my third year attending Salesforce's annual Dreamforce conference in San Francisco.

On the first day of the conference, within the first minutes of entering Moscone West, I walked into a both demoing Microsoft HoloLens units. That sort of set the pace for the rest of the conference. I certainly didn't get to as many sessions as I would have liked to. Managing about a dozen over the 4 days. I'd planned to attend many interesting sessions. According to agenda builder I had a laughably optimistic 58 sessions bookmarked. Instead I focused on the activities that could only be done while there in person. The backlog of 40 odd sessions will have to wait for the recordings to become available.

Salesforce DX

The clues I'd been seeing about "artifacts" turned out to be the internal name for a replacement to packaging that formed part of the larger Salesforce DX (Developer Experience) changes. While many of the conference headlines were for Salesforce Einstein, as a developer the Salesforce DX changes will have a more immediate and significant impact to my day to day operations over the coming years (unless they can get a Salesforce Einstein type product to do intelligent debug log parsing - hint, hint.).

Salesforce DX can mostly be thought of as a catch all name for a number of changes coming for developers (and admins). There are a number of areas affected and numerous changes will need to come together over the next few releases to make it a reality. You might be looking at piloting in Summer '17 and GA in Winter '18 (the usual disclaimer applies). You can register your interest to be in the pilot test when it opens up.

You might like to take a detour to the Salesforce DX posts by Peter Knolle or Matt Lacey

Session videos:

Source-driven development

The current status quo for development, especially of a managed package, is that a single packaging org stands as a source of truth for the current release. With Salesforce DX you will be able to drive deployments from a source control system of your choosing.

To make this possible, we're enabling you to export more of your metadata, define data import files, and easily specify the edition, features, and configuration options of your development, staging, and production environments. [Source]

The metadata for a single custom object can be split over multiple files. E.g. Split out each individual field.

Scratch Orgs

Scratch Orgs will be shorted lived and provisioned via the new CLI (and in theory a backing API call that the CLI uses). The most appealing part of these orgs is that a JSON file provided to the CLI tool will configure which features are enabled or disabled for the org (Org Shape by declaration). You will no longer need to wait 4+ days for support to enable multi-currency in your development org at some random inopportune moment. It will be interesting to see how flexible the APIs will need to become to access things there were previously only accessible via the UI. E.g. Remote Site Settings.

I recall hearing that scratch orgs would last around 2 days, but don't hold me to that. Also, they likely wouldn't be hosted on the production instances, which might open up the options for using the interactive debugger.

In another session it was suggested that instead you would have a finite number of scratch orgs and would need to explicitly delete them.

They will have a combination of parts from DE and the Branch Org pilot (giving support for multi orgs with the same global namespace). The Accounts etc... that get created with a standard DE org won't exist.

SourceMetadataMember with RevisionNum field will be used to keep local file system source control in sync with any changes made directly in a scratch org. Might be enhanced in the future with a StreamingAPI topic that can be monitored for direct changes in the org.

force.com CLI interface ++

The extended force cli appears to be a number of additional commands added to the existing force.com CLI, or something like it if you start to merge in the Force.com Migration Tool. I mostly view this as a command line friendly wrapper over the functionality provided by the Tooling and Metadata APIs. The big advantage here is that the command line access to these APIs provides an interface that many other developer tools can integrate with. The increased API and CLI support will move many of the developer tasks away for the UI.

Implemented in Node.js using the Heroku CLI pluggable architecture. Support Web and JWT Bearer Token OAuth flows.

Salesforce Environment Manager

[...] a tool we’ve created to make it easier to manage the orgs you use as part of the development process. Most of these orgs will be scratch orgs, but it also allows you to manage your sandbox and production orgs. Furthermore, the Salesforce Environment Manager/Hub makes it easy to attach your orgs to Heroku so that they can participate inside of Heroku Pipelines, our continuous delivery tool. [Source]

Packaging / Artifacts

Some of the key themes (for the future):

  • Moving from UI centric to an end-to-end API and CLI support
  • Modules & Namespaces help in name isolation and metadata organization
  • Multiple packages share the same namespace. Removes the need to extension packages and global Apex classes.
  • One App Exchange listing that can install multiple packages.

The Tooling API will be extended (GA Winter `17) to allow for package creation. There will also be an API (Enterprise API?) (GA Winter `17) to create Push Requests.

FMA (Feature Management Activation?) to sit besides the LMA in the LMO to allow features to be toggled in the package. It will include an Activation API and Activation Metrics. For admins in the subscriber orgs it will appear similar to how existing Salesforce features are activated.

Register to Learn More about Salesforce DX

Peek Under the Hood of the New Apex Compiler

This was a really interesting session.

Firstly, it covered how Apex classes are compiled to byte code and then to an interpreted form for execution on the app servers. There are various levels of caching involved at each step.

The latter part of the talk was on how the new Apex compiler was being regression tested to ensure it produced the same output as the existing compiler.

Coming Attractions: Change the Game with Event-Driven Computing on Salesforce

Another really interesting talk about providing a mechanism to raise and consume events. It would be accessible in Apex for event publishing and in Triggers for consuming events. (__e suffix.) They could be described via the Metadata, but won't support queries or updates.

Platform Events (Winter 17 Beta) - PDF

Roadmap is:

  • Winter '17 Beta of Platform Events, Apex Subscriber, External Pub/Sub Platform Events
  • Spring '17 GA for Platform Events, Pilot for High Volume Platform Events
  • Summer '17 Beta for High Volume Platform Events. Additional Messaging protocols.

The Dark Art Of CPU Benchmarking

Conclusions...

  • Assignment from static field reference = 20x slower than variable assignment
  • Assignment from dynamic field reference = 30x slower than static reference
  • Use temporary variables instead of referencing a field multiple times!
  • Doing lots of math? Use doubles instead of decimals
    Doubles are 200 times faster (.5 us vs 100 us for decimals)
  • Serializing data can eat up CPU time, depending on the amount of data being serialized.
  • Processes are much less efficient than the equivalent workflow rules.
  • Use Limits.getCPUTime() to find out if you’re getting into trouble, and exit (or go async) if you’re getting close.

True to the core

Meet the developers

Random

Friday, September 30, 2016

Starting related test cases for a given Apex test class

Way back in the day (v27.0 and earlier) it was possible to inline the test cases directly with the apex class. This wasn't always ideal, but it did have one big advantage. Finding the test cases for a class was super easy - they were right there in the class!

This functionality was torn away from us in Summer '13 (v28.0) with the Apex Test Code Segregation act. Maybe that phrasing was a bit melodramatic. The change was actually for the better and heralded in improvements to Apex performance. It did leave a gap with finding the applicable test classes for a given class. This was partially acknowledged with the following planned improvement:

"Test-to-Parent class linking – You will be able to have a test class explicitly related to regular class or trigger. Currently, many of you have implicit connections by adding a suffix or prefix to a class name, but this doesn’t make managing or interacting with your codebase all that much easier. The explicit linkage will allow us to do better UI treatments for related classes. A direct mapping will also make the upcoming Fast Deploy feature much easier to utilize."

Unfortunately for us developers I think the safe harbour this one pulled up in is a bit too comfortable. Since 2013 it's been sitting on a golden beach sipping a cold drink without a care in the world. Maybe we will see it one day.

Back to the now, I've been tinkering with the APIs to try and find an interim solution. There are a couple of challenges with this.

  1. How do you find the Apex classes that directly call a given class?
  2. How do you identify which of those classes is a test class?

The SymbolTable that is exposed by the Tooling API can be useful for the first problem. The ExternalReferences can be used to find all the Apex Classes (and Triggers) that have a direct reference to the given class. It isn't ideal as we really want to find the inbound rather than outbound references. I.e. what calls me, not what do I call. Also, the ExternalReferences returns the class name and namespace, but not the Id. Ugh, so I need to do an extra query to resolve the Id for each class.

There are also less savory ways of finding the reference information if you are willing to pull apart the developer console. Do so at your own risk. One of these is to borrow an unofficial API from the developer console. The short version is that you POST to /_ui/common/apex/debug/ApexCSIAPI and pull out the JSON response. That will give you the list of Apex classes that depend on a given class.

This brings us back to the second problem. How to determine if an Apex class contains Test cases?
My current approach is to do a SOSL query looking for Apex classes that contain @IsTest or testMethod. It can get a few false positives, but at least narrows down the referenced Apex class Ids to those that are likely to contain test methods.

I've put the accumulation of these ideas into the latest release of the FuseIT SFDC Explorer. If you are viewing an individual Apex class you can used the double green arrow to start all the associated test classes.

See also:

FuseIT SFDC Explorer 3.3.16274.7

The latest v3.3 release of the FuseIT SFDC Explorer is out and contains a number of new features. Just in time for Dreamforce '16.

Metadata Deploy

Select a folder to monitor for changes (1). Anything that changes in that folder and is a supported metadata type is added directly to the package.

Select a folder to scan for files modified today (2). Finds any supported metadata types that were added today and adds them to the package.

Redeploy the most recent package (3). If you last deployed from a collection of selected files it will rebuild the package from those and deploy them.

SOSL query support

The SOQL Query tab now doubles as a SOSL query tab (no syntax highlighting yet). You can also export the results to a CSV or the clipboard as with SOQL.

Code Coverage

On the code coverage tab the Percent column has some color coding to indicate if you are hitting the minimum 75% for that class. The individual selected classes is also shown plus an org wide coverage at the top.

The green double arrow to the right of the classes code coverage will attempt to find all the test cases that contribute the the coverage of that class. Then it queues those tests up to run.

Note that as of the time of writing there is a bug in the SOAP version of the Tooling API that is preventing individual line coverage results from being returned.

Apex test execution

If Max Failures is set to a number greater than -1 it will stop after the specified number of test case failures in an asynchronous run.

Individual Entity Viewer

When viewing an individual record by ID the field metadata will be used to expand the property grid and show all the accessible fields. Note that I'm seeing some oddities with the Entity Viewer and sometimes need to toggle between the Categorized and Alphabetical views to get it to render. It might just be something with my display drivers.

Other changes

  • Detect and highlight a "Skipped X bytes of detailed log" message in the debug log
  • WSDL2Apex: Handle class name conflicts within a namespace.
  • WSDL2Apex: Add warning for "INVALID_FIELD_FOR_INSERT_UPDATE - Number of parameters exceeds maximum: 32" - Include names for elements in warnings.
  • WSDL2Apex: Improve error handling if WSDL schema element is missing type attribute
  • WSDL2Apex: ensure enum name starts with a letter. It is not allowed to start with the number in Apex.

Saturday, September 10, 2016

Getting involved in the Salesforce Developer Community

I'll let you in on a little secret. Salesforce as a platform is really big, and it is only getting bigger. See all those colored circles, those are the Sales, Service, Marketing, Community, Analytics, Platform & Apps products. Then there is IoT in the middle integrating with many of those areas. Plus at Dreamforce '16 we are going to learn more about Salesforce Einstein, the new AI product and where it will fit in.

I'll go out on a limb here and say there probably isn't anyone with really in depth knowledge of all these products. And by that I mean deep vertical knowledge that would only come from working with it day in and day out solving real world problems. The sort of person you turn to late on a Friday when the deployment fails with some newly discovered error that even Google has never heard of.

How are you, as a developer, supposed to work with any number of these products at any given time?

No developer is an Island

Thankfully, it isn't all doom and gloom.

There are a number of existing Salesforce communities to help fill in the gaps with your Salesforce knowledge. Best of all, they are all free.

Salesforce StackExchange (SFSE)

This is my personal favorite, so it goes first in the list. Think Stack Overflow specifically for Salesforce. It's a well tuned site for asking and answering questions specific to working with Salesforce. It isn't a discussion forum, so you avoid the back and forward chit chat and associated noise that you get in other places. Plus content gets moderated and refined over time by the community.

An example of community moderation in the StackExchange format is the drive towards removing duplicates and having one conical answer where possible. We don't really need one question per person who encounters a System.QueryException: List has no rows for assignment to SObject exception. One is sufficient to outline how to check if a SOQL query returns at least one result.

SFSE is definitely my first stop for any Salesforce related questions I might have. More often than not the system will suggest a related question to the one you are forming, saving you from creating a new one.

If there is a downside to the SFSE system, it is that it can be a bit unfriendly/daunting to new users. Approaching it like any other forum system likely won't get you the results you want. Before asking a new question, have a read of How do I ask a good question? You want the question to be on topic to Salesforce, about a specific problem, and with the right amount of relevant details that someone could answer it.

Do

  • Include the applicable lines of code.
    If your asking a question about an exception, include the code that caused it. More importantly, indicate which of those lines of code caused the exception. It isn't always obvious when dealing with a snippet of code as line numbers aren't meaningful.
  • Put some effort into formatting your question. Code blocks in particular.
    Use the built in code formatting syntax. A bit of clean formatting makes it much easier for anyone attempting to answer your question to focus on the actual problem rather than misaligned code samples.
  • Vote for the constructive benefit of the site.
    Use your votes to encourage good questions and useful answers.
  • Acknowledge and link to any resources you used.
  • Use the tags.
    These help users identify questions they can potentially answer faster.

Don't

  • Post without showing any effort on your part to solve the problem.
    Someone else is giving up their time to try and help you. Include some details about what you have already tried and why it didn't work.
  • Post comments as an answer.
    It will get removed. Instead try and gain sufficient reputation so you can use the commenting system.
  • Post answers as comments.
    Comments are considered transient. They can drop off the page if there are several other interactions occurring. Also, voting on comments only serves to reorder them, nothing else.

Salesforce Developer forums

The Salesforce Developer Forum predates the SFSE. It still offers a broad trove of knowledge in the various threads.

The difficulty here is finding the nuggets of current information. It suffers from a number of problems that Stack Overflow was designed to address with Q&A sites. Lots of duplicates, stale posts that can't be edited, a limited notification system for interacting on multiple threads, painfully bad hyperlinks, etc....

Twitter

If you've got problems that can be expressing in 140 character then the #askforce hash tag can provide 140 character solutions.

Also follow and interact with MVPs and UG leaders.

Local User Groups

Check if there is already a local user group that you could attend. This is a great way to network, attend presentations, and share ideas.

Salesforce blogs

Many members of the Salesforce Developer community also run blogs that run the gamut of products. Rather than trying to compile such a list here I'll refer you to Johan Yu's complied a list of Salesforce Blogs.

Trailhead

First off, you got me, this isn't really a community where you can interact with other developers directly. At least not on the face of it. Initially it is an excellent learning resource for Salesforce. You can take modules on topics of interest and then complete some challenges to help cement what you have just learned. However, the more time you spend doing it the more you find there is a whole community of people behind it as well. See #Trailhead. Also, it is not uncommon to see Trailhead specific events at usergroups.

Ideas

Suggest new enhancements or vote on existing ideas to see the products changed.