LIGHTNING COMPONENT DEV TIPS 3

It’s been a while since I’ve posted anything on Lightning but now that I’m back in the thick of it, here are some additional tips I’ve learned from doing some more advanced, for me at least, Lightning work.

Checkout my Lightning Component Dev Tips and Lightning Component Dev Tips 2 for other tips.

Component.getReference For Dynamically Building References

getReference returns an object that can resolve itself at runtime or be able to bind to attributes. For example, you can dynamically attach event handlers to a component at run time.

Another example is dynamically binding to fields at run time.  The use case is you have a field set or other metadata that defines a list of fields to display on a lightning component. If an admin wants  to update the list of fields, they can easily do so without a developer updating the code. Unfortunately, this is not as straight forward in Lightning as it was in Visualforce. The following is NOT allowed to dynamically show a record’s fields.

getReference to the Rescue

When the list of fields are returned from an Apex callout, where the callout gets them from a field set or some other metadata, one can use the getReference function on the component to dynamically create a reference that will resolve to retrieving the field’s value from the record. Below is an example of that.

Now that the field value has been bound to the FieldValue on the Field, it’s a simple matter of iterating over it in the markup. Of course, you’d probably want to format the output further depending on the field’s type. For example, localizing a datetime or date field.

Another place where this was helpful for me was dynamically creating an input component and binding it to the component’s value field. This allowed for the two-way binding to still work for dynamically created components so that when the input value’s changed, it would also update the record’s value to easily allow the record to be upserted via an action.

Using the above helped in the creation of a FieldSetForm component that builds a dynamic set of inputs for a given SObject record driven by a field set.

Use “and” and “or” keywords in place of “&&” and “||”

The && and || operators are now allowed in the {!  } component markup for some reason. Instead, use the “and” and “or” functions that take a list of predicates and evaluate as expected.

Event Parameters Are Pass-By-Value

When passing a Javascript object through a fired event in one component as an event parameter and it’s modified on another listening component’s event handler, the object’s modified state isn’t visible on the original component that fired the event.

In other words, an event’s parameters are pass-by-value and are copied instead of pass-by-reference.

Can’t Create div, span & Other HTML elements in CreateElement or CreateElements

The ‘Type’ parameter to CreateElement and CreateElements must be an Aura or Lightning component. It can’t be a simple HTML element such as “div” or “span”. Even though they get created successfully, they don’t render on the page.

What other tips do you have? Let me know in the comments.

Coming Soon to Boostr: Viewing Other Org Limits

Have you ever wondered what your daily email limit is and how many emails can still be sent? Ever wanted it to show on the System Overview page in setup? Yea me too… so … I added another enhancement to Boostr for Salesforce that shows that and all the other Org limits on the system overview page that are returned via the Limits REST API.  This is coming in the next Boostr release… Stay tuned.

Technical Bits

High-Level Algorithm

  1. When on the system overview page, grab the current user’s session id from their Salesforce cookie. This is needed because the Salesforce APIs only allow an authorized user to make API calls.
  2. Using the session id, make a callout to the Limits REST API at /services/data/v40.0/limits/
  3. If successful and the limits are returned, show the limits on the system overview in a new “Other Org Limits” section that looks and feels like the other limit sections.

Full Code

Wanna see the full code? View the Pull Request.

Inspiration

Not so long ago, Matt Addy asked how to find out what the Saleforce daily email limit is on Slackforce. We basically said that has always been a tricky question to answer because the formula Salesforce uses is complicated and there’s neither an easy way to find out using the Apex Limits object nor through the Salesforce setup area either.

The Limits object has a getEmailInvocations method and a getLimitsEmailInvocations method but those tell the currently executing code how many emails have been sent in this context and how many are still allowed. At the time of this writing, 10 emails can be sent programmatically per execution context.

After some more digging, it turns out that the Limits REST API returns the number of daily emails limit and how many are remaining along with many other limits.

Have a feature you’d like to have in Boostr? Submit a feature request issue in GitHub and a contributor may implement it. Or you can implement it yourself by following the Contribution instructions.

Related Posts

Boostr Posts

Coming Soon to Boostr: Classic Listview Row (De)Selecting

Nick Snyder recently added changeset row clicking (de)selecting support to Boostr which is very nice. It helped me immediately after release and saved a bunch of time.

He inspired me to add that capability to object listviews and setup listviews in Salesforce Classic. I implemented this via this pull request and it’ll be added to Boostr soon.

Object Listview Row (De)Selecting

When one opens an Object Listview that has the row checkbox, as seen below, clicking the row will either check or uncheck the checkbox.

Setup Listview Row (De)Selecting

Support has also been added to setup listviews such as the Users area. When the row is clicked, the checkbox is either checked or unchecked.

Technical Details

High-Level Algorithm

  1. If in Salesforce Classic, attach row click event handler to setup listviews. Also, attach a MutationObserver looking for object listview creation.
  2. If an object listview is dynamically added to the page, attach row click event handler.
  3. If an object or setup listview row is clicked, invoke the row’s checkbox’s click event to either check or uncheck it.

Why No Lightning Support?

I’m actually working on this as we speak. To see the progress made so far, see the lightning-listview-row-clicking Boostr branch. However, I’ve run into a roadblock where clicking the row selects or unselects the checkbox but then some other piece of code clicks it again making it appear as though it wasn’t clicked.

My preference is to release something that helps rather than release nothing that doesn’t help so that’s why Classic support is out first. (Hopefully, I can figure out the lightning fix and get it out in the same release).

Have a feature you’d like to have in Boostr? Submit a feature request issue in GitHub and a contributor may implement it. Or you can implement it yourself by following the Contribution instructions.

Related Posts

Boostr Posts

Reflecting on 1 year in business

It doesn’t feel like it’s been a year since Taking the Freelance Plunge was published but time flies.

Overall Thoughts

Overall, the experience has been positive. I can’t remember being happier and less stressed, professionally or personally, despite the occasional “where’s my next client coming from” setbacks. My only regret wish is that I had started sooner. A few years ago, my employer had kindly arranged a career coach to interview me to figure out what I’d like to do. While speaking to her, I said “start my own business” and it just felt right. I didn’t get into freelancing until a few years later though. Oh well.
Want tips on getting started yourself? Check out Tips Getting Started in Freelancing. Also, feel free to Contact Me privately and I’ll give ya feedback.
Want tips starting your own Salesforce Blog? Check out Bob Buzzard’s Starting Your Salesforce Blog by the one and only Bob Buzzard.

What’s New

Awesome Client Still

Still have an awesome client. They are great to work with, the work is interesting and varied, and I’ll keep working with them as long as they’ll have me. I hope I get to be onsite again soon.

If I ever felt like being a full-timer again, I’d seriously consider these guys too.

Part-Time Work Halted

For a while, I was doing part-time as needed work for a client but they haven’t needed my services for a while. It’s a bummer because they had some very interesting and challenging technical work and are also great to work with. That’s ok though because the work I can’t take on, I usually forward to them along with potential job candidates because if full-time appeals to me again, I’d seriously consider working for them.

More Lightning

I’m now feeling comfortable with Lightning aka over the hump. It still feels like a beta to me because of the amount of developer plumbing needed to make simple things work but that’s ok. Also, I’m starting to like Javascript again but I’ll save that rant for possibly another blog post.

Contributing to Open Source

I’ve always contributed to open source projects occasionally. My focus lately has been contributing to Boostr for Salesforce, which is a Chrome Extension that enhances the Salesforce experience in various ways. “View Offending Process Builder or Flow” Link in Lightning and Field History Tracking Counter Configurability are two features I’ve implemented that are now released. It feels good to know that I’m contributing to the Salesforce world and helping others out. Getting some recognition and thanks is icing on the cake.

What’s Next

Aspiring To Becoming a Salesforce MVP

Recently, I started seriously researching what it takes to become a Salesforce MVP. I even reached out to two of them that I know and they gave me some really good feedback. Essentially, they said to contribute to the community and be very active in it too through whatever means you like such as blogging, social media, user groups, open source, or some combination.

This is a long-term goal and may take years but I think I’ll eventually get there. To help accomplish this, my plan is

  • Increase blogging frequency
  • More active in the community with social media, other blogs, user groups, etc.
  • More open source projects

Through these activites, I’ll be also accomplishing the “Keep Expanding the Network” that I’ve been writing about for a while.

If you have other tips on becoming an MVP, please let me know.

Also, if you feel like nominating me, please do so at Salesforce MVP Nominations which are open through August 7th.

More Open Source Contributions

I’ve been having a blast contributing to Boostr and am going to focus on some other projects too. Stay tuned…

How’s your business or professional goals going for 2017 and how will you reach them? What’s your experience with freelancing been like?

Related Posts