When Salesforce is life!

Tag: NodeJs

Automatic export tool for Salesforce Data Export backups

TL;DR
Jump to GitHub for the complete repository: https://github.com/enreeco/sf-automatic-data-export-script/

Have you ever had a close relation with the Salesforce Data Export feature?

It’s a way to periodically export all Salesforce data set in zipped CSV files, including files and attachments.

You can do a one-shot export or schedule it on monthly (available on Developer Edition orgs) or weekly (available on EnterprisePerformance, and Unlimited Editions only).

The one-shot and periodic export configuration is straightforward:

  • Select the file encoding
  • Select which data you want to export (including files and content can increase export size)
  • Select a schedule (for monthyl or weekly export schedule only)
  • Select all or a subset of the available Salesforce objects
Monthly Data Export configuration schedule

What’s the outcome?

You’ll come up with a set of zipped files with a size up to 512 MB, containing Salesforce extracted files (if checked in configuration) or CSVs grouped by Salesforce objects, as shown below:

The struggle of downloading

What if you have plenty of files and want to automatically download them one-shot without having to click link by link?

Unfortunately there are no Salesforce standard APIs that you can use to automate the export and the only way was to go by script by getting all download links and triggering each download on a local folder (or remote storage if you are brave enough).

I thought there was already a solution out there but as far as I know there wasn’t anything.

The script

I decided to implement a script in NodeJS that:

  1. logs in to Salesforce with a full powered user
  2. opens the Data Export page
  3. looks for the download links (if any)
  4. triggers downloads one by one, putting them on a local folder

This way you can continue doing other tasks while the scripts runs.

DISCLAIMER: the script has been written in a quick & dirty style, so please don’t tell me it’s ugly, it gets you to the point!

Download it from GitHub: https://github.com/enreeco/sf-automatic-data-export-script

These are the simple steps:

  1. Install NodeJS and NPM if haven’t already (you just have do donwload the installers, follow this guide but you’ll find tons online)
  2. Open a console and install Foreman with:
    npm install -g foreman
    An alternative is to use the Heroku command line with:
    npm install -g heroku
  3. Install all required packages with command line npm install
  4. Rename the .env-local into .env and replace the environmental variables with a local path (where the files will be stored), the login URL, your username and the password+token
  5. Run your script with alternatively:
    nf start
    or
    heroku local

You’ll see the script running and the files magically will drop on the selected folder:

Automatica Data Export script execution

Have a nice Salesforce day!

[NodeJS] My Simple Static Webserver

a{color:red !important;}

When developing pure HTML5 mockups I usually don’t want to install a webserver or host my static pages on a remote webserver…
More over I sometimes like to create my own tools even if they are already on the net or they are super duper simple.

That’s why I created a simple NodeJS static webserver.

You can find that simple app in this Github repo.

To use it just type:

$ git clone https://github.com/enreeco/simple-static-web-server

$ node server

This will create a local webserver @ http://localhost:3000/index.html: all the files must be kept on the root folder.

If you want to specify another port simply type:

$ node server 1234

And the magic will be @ http://localhost:1234/index.html.

Before leaving remember my motto:

[Chrome Extension] “Pack your link” extension on the store by enree.co (yes, thet’s me…)

a{ color: red !important;}

I’m not a marketing guy, I neither have requested advices from my marketing guys friends…I did it all my way and that’s why my brand new Chrome Extension will be forgotten in days!

Let’s start from the beginning…

Once upon a time there was a simple Chrome Extension I made at work for WebResults, my Company: you will hear about it in weeks, but not now.

After weeks of testing we decided to put it in the market but I didn’t now how to do it (I know, there si plenty of stuff online, but I don’t trust myself till I see things done!…that’s why because sometimes I behave like Goofy when dealing with simple tasks!!).

So I thought, “How can I do a clean job for my company without making foolish mistakes”?

The answer was “Create your own extension”.

That’s why I created url.enree.co (I love this domain name), a simple Url packing utility hosted on Heroku, written in NodeJS.

Why not creating a simple chrome extension to use this service?

Here you are Pack your link by url.enree.co.

Publishing a Chrome Extension on the store is really easy.

  1. Create your extension (start here if you don’t know what to do, it’s easy and fun)
  2. Go to the Chrome Store Developer dashboard: you have to pay 5$ to be able to publish your creations on the store, once in a lifetime! In few minutes you’ll be allowed to publish extensions and apps.
  3. ZIP your local extension’s folder and upload it: it seems that you cannot remove a product once uploaded (read more details here, but I’m not quite sure about it), but you can still unpublish to hide it.

See ya!

[NodeJS + Salesforce SOAP WS] How to consume a Salesforce SOAP WSDL

I was wondering how to consume Salesforce WSDLs with nodejs.
I found Node Soap package (see npm) and I tried to consume a Partner WSDL.
Then I saved the WSDL in the “sf-partner.wsdl” file and played with the methods to get nodeJS speak SOAP with Salesforce.

var soap = require('soap');
var url = './sf-partner.wsdl';
soap.createClient(url, function(err, client) {
   console.log('Client created');
   console.log(client.SforceService.Soap); //all methods usable in the stub
});

If you try to console.log(client) you will see too much data

This is the output:

{ login: [Function],
  describeSObject: [Function],
  describeSObjects: [Function],
  describeGlobal: [Function],
  describeDataCategoryGroups: [Function],
  describeDataCategoryGroupStructures: [Function],
  describeFlexiPages: [Function],
  describeAppMenu: [Function],
  describeGlobalTheme: [Function],
  describeTheme: [Function],
  describeLayout: [Function],
  describeSoftphoneLayout: [Function],
  describeSearchLayouts: [Function],
  describeSearchScopeOrder: [Function],
  describeCompactLayouts: [Function],
  describeTabs: [Function],
  create: [Function],
  update: [Function],
  upsert: [Function],
  merge: [Function],
  delete: [Function],
  undelete: [Function],
  emptyRecycleBin: [Function],
  retrieve: [Function],
  process: [Function],
  convertLead: [Function],
  logout: [Function],
  invalidateSessions: [Function],
  getDeleted: [Function],
  getUpdated: [Function],
  query: [Function],
  queryAll: [Function],
  queryMore: [Function],
  search: [Function],
  getServerTimestamp: [Function],
  setPassword: [Function],
  resetPassword: [Function],
  getUserInfo: [Function],
  sendEmailMessage: [Function],
  sendEmail: [Function],
  performQuickActions: [Function],
  describeQuickActions: [Function],
  describeAvailableQuickActions: [Function] }

There is a quicker way to obtain this using the console.log(client.describe()) function, but this seems not to work with big WSDL like Salesforce ones (Maximum stack error)
The first move was to login to obtain a valid session id (using SOAP login action):

soap.createClient(url, function(err, client) {
    client.login({username: '[email protected]',password: 'FreakPasswordWithTkenIfNeeded'},function(err,result,raw){
      if(err)console.log(err);
      if(result){
          console.log(result.result);
    });
});

And this is the result

{ metadataServerUrl: 'https://na15.salesforce.com/services/Soap/m/29.0/00Di0000000Hxxx',
  passwordExpired: false,
  sandbox: false,
  serverUrl: 'https://na15.salesforce.com/services/Soap/u/29.0/00Di0000000Hxxx',
  sessionId: 'XXXXXXXXXX',
  userId: '005i0000000MXXXAAC',
  userInfo: 
   { accessibilityMode: false,
     currencySymbol: '€',
     orgAttachmentFileSizeLimit: 5242880,
     orgDefaultCurrencyIsoCode: 'EUR',
     orgDisallowHtmlAttachments: false,
     orgHasPersonAccounts: false,
     organizationId: '00Di0000000HxxxXXX',
     organizationMultiCurrency: false,
     organizationName: 'Challenges Co.',
     profileId: '00ei0000000UM6PAAW',
     roleId: {},
     sessionSecondsValid: 7200,
     userDefaultCurrencyIsoCode: {},
     userEmail: '[email protected]',
     userFullName: 'Admin',
     userId: '005i0000000MxxxXXX',
     userLanguage: 'en_US',
     userLocale: 'en_US',
     userName: '[email protected]',
     userTimeZone: 'Europe/Rome',
     userType: 'Standard',
     userUiSkin: 'Theme3' } }

Now the problem was to put the new endpoint and the session id or the next call, and this is the solution:

  //sets new soap endpoint and session id
  client.setEndpoint(result.result.serverUrl);
  var sheader = {SessionHeader:{sessionId: result.result.sessionId}};
  client.addSoapHeader(sheader,"","tns","");

And after that you can make wathever call you want:

      client.query({queryString:"Select Id,CaseNumber From Case"},function(err,result,raw){
          if(err){
            //console.log(err);
            console.log(err);
          }
          if(!err && result){
            console.log(result);
          }
      });

The result var will have all the data you expect from the SOAP response:

{ result: 
   { done: true,
     queryLocator: {},
     records: 
      [ [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object] ],
     size: 26 } }

The fact that you cannot call the client.describe() force us to read the WSDL to know which parameters send to the call.

[Salesforce / Canvas / NodeJS] Set up a minimum Canvas App (NodeJS style)

Following the old post [Salesforce / Canvas] Set up a minimum Canvas App (Java style) I’ve decided to put togheter the code I’ve used to talk to a Force.com Canvas App in NodeJS.

For those who can’t wait the whole article, this is the repo (CanvasApp-NodeJS-Base).

Follow this steps to configure an App (something is changed in the last SF releases):

  • Setup -> Create -> Apps -> (Connected Apps) New
  • Fill Conntected App Name, API Name and Contact Email
  • Check the Enable OAuth Settings in the API (Enable OAuth Settings) section
    • Callback URL: put a valid URL (you don’t need it for the scope of this example)
    • Selected OAuth Scopes: select the scopes you want for the NodeJS app acess token to grant
  • In the section Canvas App Settings type:
  • Canvas App URL: the url of the POST action to be called by Salesforce when injecting the session data (e.g. https://my-canvas-app.herokuapp.com/canvas/callback)
  • Access Method: use Signed Request (POST)
  • Locations: choose Chatter Tab (the app will be shown in the Chatter page)

Than you need to enable this app:

  • Setup -> Manage Apps -> Connected Apps -> Your App Name
  • In the OAuth policies select Admin approved users are pre-authorized for the Permitted Users field
  • In the Profiles related list select your profile (all user of that profile are automatically allowed to consume this app)

There are more ways to configure the Canvas App usage, this is the quicker.
Now clone this github repo CanvasApp-NodeJS-Base and create your own app:

$ cd [your test folder]
$ git clone https://github.com/enreeco/CanvasApp-NodeJS-Base
$ heroku create my-canvas-app
$ git push heroku master

The app will be pushed to Heroku (if you are using Heroku 🙂 ) and you will find it listening @ https://my-canvas-app.herokuapp.com.
Remember to use the “https” protocol in the callback URL setting, otherwise the canvas app won’t work correctly when called from Salesforce (using iframes with different protocols makes the browser go creazy).

To make it work you have to set the secret as an Environmental variable (the secret is the Consumer secret field in the App settings).
On Heroku simply type:

$ heroku config:set SF_CANVASAPP_CLIENT_SECRET=XXXXXXXXX

If you access the app outside the canvas this is what you see:

This is what happens in case of error:

This is what happens if all goes well:

The core of this app is the sf-tools/index.js file which handles the decoding of the POST request that contains all the canvas info (such as token, urls, …): have a look at the verifyAndDecode(input, secret) function.
You can then use this token and urls to make whatever call you want to your instance (as long as your user has access to it), but its outside of this post!

I leave you with a picture of a cute cat, this sould bring here a lot more readers.

Powered by WordPress & Theme by Anders Norén