Thursday, January 25, 2018

Finding and downloading Salesforce debug logs via API

I guess Salesforce's UI for viewing/downloading persistent debug logs is OK.  Nothing fancy, but usable.  However that's only true if you have a handful to browse through.  Recently I was in a situation that was way beyond that limited circumstance.  We were troubleshooting a sporadic API error with Salesforce Support, and they asked us to log against the API user for 24 hours.  That means 10s of thousands of logs (if not 100+).  Can you imagine paging through them from Salesforce UI trying to find the useful ones?  Here's where an API approach comes in.

First step is to find the logs you want.  Downloading all of them is much less efficient.  Fortunately that can (usually) solved by a SOQL query on ApexLog object, where the metadata on debug logs is stored.


For my recent use case, it's sufficient to find just the failed transactions, but there are other useful filtering fields in ApexLog such as timestamps and types.

Next comes the actual retrieval of the log text.  That needs to be done through Tooling API.  The resource URI for REST is /services/data/v39.0/tooling/sobjects/ApexLog/[log Id]/Body/.  Log Ids would come from the query in step one.  Obviously you can used other compatible API versions as well.


I'm using Workbench as the tool just because it's unbelievably convenient for one-off operations.  If downloading is needed for a bunch of logs, some CLI script would probably be needed, which can also enable automated downloading if so desired.

Monday, November 20, 2017

ScoreIntelligence object

This is the core object for the Einstein Lead Score feature, which one would need when programming trigger off (or for) it.  However I couldn't find official documentation on it (other than mentions in Release Notes).  It's not in the SOAP API Developer Guide, which is usually the (authoritative) place for standard object reference.  It does have ScratchOrgInfo, which belongs to Salesforce DX, a feature came out as GA in the same (or later) release (Summer '17) as Sales Cloud Einstein.  In terms of being "API first", that's strike one for Einsten.  Just saying...

So here's what I can glean from standard describe in Apex.

API NameLabel
ObjectScoreIntelligenceScore Intelligence
Field 1IdScore Intelligence Id
Field 2BaseIdLead Id
Field 3ScoreEinstein Score
All look pretty self-explanatory.  The interesting thing is that from the BaseId field, it looks like the object is a child object of Lead's, but through Apex/Process/SOQL the score can also be accessed as ScoreIntelligence.Score from the Lead object - something I know to be true (as we use it that way) but again cannot point to documentation - which means it's also a parent object of Lead's. 

Appendix: Apex script if you want to see the raw output.
        Schema.DescribeSObjectResult result = ScoreIntelligence.sObjectType.getDescribe();

        system.debug(result);

        system.debug(result.fields.getMap());

        for (String s : result.fields.getMap().keyset()) {

            system.debug(result.fields.getMap().get(s).getDescribe());
        }

Monday, May 15, 2017

Can't login after upgrading to Ubuntu 16.04

After entering password and pressing Enter, the screen just briefly flashed dark then back to the login screen.  Same went for Guest Session.  Looks like the desktop session is unable to start (or crashes when starting).  The system was upgraded from 14.04 and runs inside Virtualbox.

Following advice by Nuno here, I uninstalled nvidia driver and things are back to normal.  I had to drop into root shell at boot to do it though.
apt-get purge nvidia*

Friday, March 24, 2017

Dependencies for Dwarf Fortress

My son was trying to get Dwarf Fortress going on Ubuntu Precise running in Crouton on a Toshiba Chromebook.  Of course that's complicated - but it's not something you assess before starting when you're 12.  So here's some notes for resolving some dependencies for the wonderful little game.

libSDL2.0
Ubuntu's repo doesn't have it (for Precise anyway).  Add Xbmc PPA first.
sudo apt-add-repository ppa:team-xbmc/ppa 
sudo apt-get update
sudo apt-get install libsdl2
lidSDL-image
Once Xbmc PPA is there, it's as easy as adding
sudo apt-get install libsdl-image1.2

Monday, December 19, 2016

Thumbs Up/Down ("Solution Helpfulness Rating") Feature for Knowledge with Visualforce/Apex

This is a feature that came out without too much fanfare and was supposedly meant to address an idea.  The only place that mentioned how to use it in Salesforce with details was in Summer '15 Release Notes, when it came out as beta.  Then there's an article about using it with Community templates, in which it's referred as "Article Voting".  None of them gives any clue on how it can be used in a Sites based Public Knowledge Base, which happens to be the use case I need to handle. 

After some back and forth with Salesforce Support, they pointed me to this Developer Forum thread that talks about Vote object for Idea.  I did some simple code to verify, and I could indeed set parentId to __ka objects and programmatically manipulate up/down vote state.  That, was not mentioned at all in the official doc for the Vote object (as of Dec 2016, ver. 38.0).

With almost all the pieces in place, one hurdle remains for to a public KB Up/Down vote solution in VF/Apex, if the KB is used in an unauthenticated context.  Vote object doesn't allow vote on the same parentId more than once by the same user.  If the users are unauthenticated - basically always Public Site Guest User - then votes can't really be recorded for more than once.  That's hardly useful.  I have no workaround so far, other than building a custom solution without using Vote.  That would suck if you'd really want internal and external votes are counted and reported together.

Tuesday, April 5, 2016

Use Apex @InvocableMethod with Process Builder

It's one great way to marry the versatility of Apex to implement complex logic and the easy administration of Processes.  Say you want to auto-unfollow old Contracts a period of time after they're terminated for those who manage a lot of contracts.  It's pretty straightforward to do the unfollowing in Apex, then use Process to invoke the action and leave it to the administrators to control how long the wait period should be.  Bob Buzzard has a good piece on the general pattern, and info about @InvocableMethod can be found here.

The only thing I found a bit non-intuitive is how parameter is passed to the Invocable Method, which only takes List of arguments.  Process Builder, on the other hand, doesn't have an obvious way to create an array/list variable.  It turns out that all you need is to set a single value of the correct type (of the List element).  For instance if the method expects a list of IDs, just set a single ID, which is called reference in Process Builder.



"scids" is the argument expected by the Apex method.

    @InvocableMethod(label='Chatter unsub for Service Contracts' description='Unfollow terminated/cancelled service contracts for PMM team')
    public static List UnfollowSCs(List scIds) {

Monday, February 29, 2016

Apex code example for Kayako signature

Was looking at Kayako's API documentation - don't ask me why - and noticed there's no Apex example for their API request signature scheme (why would they?), so this might be useful for people who's in the same situation as I'm.

public class KayakoAPI {

    public static string getSignature(string secretKey) {
        string signature;

        if (secretKey!=null && secretKey.length()>0) {
            Blob salt = crypto.generateAesKey(128);  //can use other ways for random string generation

            // keyed hash
            Blob hmacsha256 = crypto.generateMac('HmacSHA256', salt, Blob.valueOf(secretKey));

            // Base64 and URL encoding
            signature = EncodingUtil.urlEncode(EncodingUtil.base64Encode(hmacsha256), 'UTF-8');
        }

        return signature;
    }
}