npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

weblinkjs

v1.1.1

Published

An abstraction built around building, deploying, and invoking Javascript through custom buttons/links (weblinks) in Salesforce

Downloads

4

Readme

weblinkjs

Build, deploy, and invoke ES2015+ Javascript in custom buttons/links (weblinks) in Salesforce! Includes support for creating dialogs with ReactJS.

Use this project as a template for centralizing your org's custom weblink javascript. Either fork this repository through Github, or clone and push to your own public/private remote.

Usage

By convention, code for each weblink is defined inside an ES6 javascript module with paths that conform to modules/<SObjectType>/<Weblink API Name>/index.js. Note, this isn't strictly enforced, so you may adapt using your own naming conventions, as long as the modules reside in src/modules/.

Since these are self-contained modules, code stored on the weblink itself is fairly minimal:

{!REQUIRESCRIPT('/resource/weblinkjs/weblinks.js')}
weblinks.Account.My_Custom_Weblink();

Whereas its module would implement the logic:

// src/modules/Account/My_Custom_Weblink/index.js
export default function (params) {
  alert('hello world!');
};

Example

Implementation

See the included example: Contact/Say_Name

Button/link code

{!REQUIRESCRIPT('/resource/weblinkjs/weblinks.js')}
'use strict';
weblinks.Contact.Say_Name({
  sessionId: '{!API.Session_ID}',
  userDisplayName: '{!$User.FirstName} {!$User.LastName}',
  contactId: '{!Contact.Id}'
});

Parameters

The biggest drawback of moving code to static resources is that merge fields cannot be parsed. To get around this limitation, data can be passed into modules as named parameters for the module to parse; example:

Button code

{!REQUIRESCRIPT('/resource/weblinkjs')}
'use strict';

weblinks.Account.My_Custom_Weblink({
  sessionId: '{!API.Session_ID}',
  id: '{!Work_Order__c.Id}'
});

Module code

export default function ({ sessionId, id }) {
  // use parameters here
}

Be aware that other than the record's Id, most values cannot be relied on to be up-to-date. Merge fields are only evaluated once; i.e. the moment before the page is loaded.

One potentially common scenario where this would be problematic occurs when one user (Bob) opens a record's detail page, then a few moments later, the same user (or some other, doesn't matter) makes some field update that would result in the value differing from the value it was the moment the page was loaded by Bob. In this scenario, if Bob then clicks a javascript button coded with merge fields, the data he'd be using will now be out-of-date.

A solution to the above scenario is to pass only the record's Id and the user's session Id, then within the module, use Ajax to query the record every time the button is clicked (using Salesforce's connection.js or jsforce). Note that this has a downside of its own, which is that it counts against the limit of API calls your org can make in a single day.

Development

Requirements:

  • Node.js
  • Git
  • Some kind of shell prompt
    • Good options for Windows are Cygwin or Git BASH (included in the Windows Git installation).

Launch a Bash-like shell, clone this repo to where your projects live, then install:

npm install

Now you may either edit an existing module, located in modules, or create a new one.

To create a module for a weblink, create a folder for the module with the path modules/<SObjectType>/<Weblink API Name>/.

Create a folder if one doesn't already exist for the SObjectType. Also create an index.js file that will be used solely to import modules of this type and export them to modules/index.js (of which will also need to be edited to include the new SObjectType folder).

In the module's folder, create index.js, which will be the module's entry point.

An example template for index.js:

export default function (params) {
  // ... your weblink code goes here, parsing params as you see fit ...
};

Remember to also add code for invoking the module in the weblink itself, as shown in the Usage section above.

Once you're done making changes, build the library:

npm run build

Then deploy using your Salesforce credentials:

npm run deploy -- -u <username> -p <password> -t <security token> -l https://test.salesforce.com

To save time, you could build and deploy at the same time (and also set the environment):

Development

export NODE_ENV=development && npm run build && npm run deploy -- -u <username> -p <password> -t <security token> -l https://test.salesforce.com

Production

export NODE_ENV=production && npm run build && npm run deploy -- -u <username> -p <password> -t <security token> -l https://test.salesforce.com