Transitioning to #Lightning from Visualforce

You’ve heard me say it before: I was jaded. I was looking back at 20+ years in I.T most of that as a developer in various languages and “frameworks of the day.” I was tired of the same thing, and even my transition to the Salesforce platform brought very little actual excitement for me. The community helped me see past all of that, which is why the community is still my favorite thing about the ecosystem, but as a developer, I was still…well I was cranky!

Then Salesforce introduces #Lightning. Suddenly I was invigorated again, something new, something that was in line with what all the “cool kids” were doing. Something forward-looking, and I got excited again. I dove in and started working, which brings me to my first tip for making the leap from Apex/VF to Lightning.

#1. Jump Right In — One of my favorite bands today “The Zac Brown Band” has a song called “Jump Right In” and you could say I took that to heart. I closed my eyes, picked a random deity, said a prayer and leapt from the lion’s head smack-dab into writing some lightning components. I suppose I’ve always been a learn by necessity/trial by fire kinda person and that seems to work for me. So if it works for me, it might work for you as well. Spin up a developer org, turn on lighting, and start writing something, anything! My first foray was a streaming application that ran via the web and in Salesforce1 that allowed users to vote on what they wanted for lunch. As they voted, the counts went up in real time. Meaning person A voted for sushi from their phone, and persons B,C, and D all saw those updates happen in real time on their screens. I was jazzed, and I was hooked! Was it useful? Not really, but that’s not what matters. What matters was that I learned something. So just “Jump Right In” and do something.

#2. Ask “What If” — since your jumping right in with both feet anyway, start asking yourself “what if?” What if I try to add something like a streaming (see above) result to a lightning component? What if I change this line to that? What if instead of creating a component on the record detail page to modify a field on that same object, (because you could do that), I created a reusable form to create child components directly from the parent’s detail page without having to leave that page? Ya know, like this:

#3. Check out Lightning Design System — When thinking about moving some functionality from Visualforce to Lightning, think about your options. Maybe your first option isn’t a component at all, maybe its simply re-styling your Visualforce page using the Lightning Design System. (This thing is awesome…just awesome). If you’re at all like me at hate front end work, the perhaps the Lightning Design System can ease your pain somewhat. They’ve taken all the things I hate about CSS and put them together in a not so blackbox “blackbox” that just works. All you have to do is apply the appropriate styles to your HTML elements and the system takes care of making it look like the page is inside LEX. Yea, I’m gonna say it…it’s sexy.

#4. Lightning Out — Maybe you’re not quite ready to go full on lightning components. Maybe you could instead replace pieces of functionality with a simple component and Lightning Out and throw that into a Visualforce Page. Lightning Out allows you to run lightning components right inside of a Visualforce page or even off platform if you so desire. (Something I’m really wanting to try but just haven’t been able to find that time yet, but mind you, I will indeed find the time). This would allow you to start small and work your way up.

#5. Get Involved — No surprise here. Me, the guy always raving about the community, is asking you to get involved with the community. Join your local developer group, go to user groups, participate in the Success Community, or the Salesforce Stack Exchange. Start helping other’s solve their problems. I promise, you will learn more than you ever thought you could, and there’s so much to learn. I’m barely scratching the surface and if it weren’t for those in the community already doing the things I’ve mentioned above, I’d be nowhere. Ya hear me? No. Where. The community is the secret and we are all here to help. The blogs, the forums, the user groups, the conferences, all of it. Just do it.

:wq!

Time For Giving Thanks

I know, I know…I KNOW — its been awhile since I last blogged. Ever since June I’ve pretty much been a one-man consulting shop and one has to pay the bills. Throw a couple of talks and a Dreamforce performance with Apex and the Limits in there as well as numerous gigs with a local cover band, and of course family duties, you have a recipe for disaster when it comes to keeping to a tight blogging schedule. (It may be too early for New Year’s resolutions, but I suppose one of mine will be to tighten this schedule up!)

But I digress — now is NOT the time for that. Now is the time to sit back and think about all of the things that are going right in my life. All of the things that I am grateful to have in my life. So that’s what I intend to do today…well not entirely, it will have a “mostly” Salesforce bend to it…somehow (I don’t plan these things, I write when I’m moved to do so…)

I was sitting at my desk this morning working on some validation rules and had our Apex and the Limits Dreampark performance going in the background. I was reminded of a moment during the last tune where I looked down at the gathering of community folks swaying back and forth in the front of the stage. I remember looking down at them all and thinking just how lucky I am to be able to share my strongest passion (music in case you didn’t know) with this incredible community. At that moment, on the stage — I was slightly….overcome…

img_3510

(no I didn’t cry, I was just sweating from my eyes…). That memory is what has sparked this entry, so not in any particular order, save for the first — THIS is my Salesforce Thanksgiving list:

1) The Community — I’ve said it again, and again, and again, and again and will keep saying it as long as I’m here, I LOVE THIS COMMUNITY. There’s nothing like it in the tech world that I’ve ever seen. Its why I keep coming back.

2) Apex and the Limits — as eluded to above, being a member of the band allows me to marry my passions together: Music, Technology, and this wonderful community (see above). I also get to share the stage with some incredibly awesome people: Rachel Rogers, Peter Chalmers, Mark Ross (and when we are lucky, Benjamin Bolopue, and Brian Kwong). Sharing the stage with these people — well, lets just say it feels like home :)

3) The MVPs, and the Salesforce MVP team that wrangles us all — for years…many years I’ve never really felt at home amongst other techies. Yes I was a techie myself, mostly self-taught and with that comes imposter syndrome like you wouldn’t believe. I was never an “academic” developer. I just got shit done, and apparently did it well, but not having that “academic” rapport with my peers pre-Salesforce, I always felt like I just didn’t belong. Then somehow I found myself getting to interact with some very smart, very giving, incredibly approachable and friendly people. I felt welcomed for the first time in my career. I still feel like an imposter, but nobody “makes me feel” that way. I’m so thankful to be able to call these folks friends! #Ohana for the win!

4) The Platform — I was a very jaded developer. Very cranky — tired of “the latest and great framework” type of mentality. Just barely feeling like you’ve mastered one before the next hot thing comes along only to jump in and find it half-baked. (Forget jumping into those communities to learn — again, see #1 above). With Salesforce, while they do innovate and come up with new features all the time, the whole thing still always feels very familiar. Yes Lightning was a big jump from Apex and VF but somehow it was so easy for me to accept this time, probably because I still have the rest platform goodies to lean on whenever I need to.

5) Trailhead — I mean COME. ON. Really? Really? You’re going to put all of this wonderful training material together, throw it up on the web, for free? For anyone to begin learning? For anyone to add to their existing skill set? To give people an option to learn something new and change their career, their LIFE!?! Oh and you’re going to make it fun to boot? Abso-effin-lutely! Sign me up. I may only have 32 badges (again #consulting) but Trailhead gives me a way to jumpstart my knowledge absorption when presented with a new task. I’ve even passed the link on to my sister in hopes that she can take it and learn. We’ll see — tech really isn’t “her thing” but ya never know. It’s zero barrier to entry, its fun, and its full of valuable information. If you haven’t yet signed in, do it! Today!

6) My local Salesforce peeps! — Salesforce lunches, User Group meetings, Dev Meetups, and hopefully I can make a #SalesforceSaturday or two after my daughter’s swim season is over. The group of folks are the heart of the community here in Wisconsin. I look forward to all the time I can possibly spend with them!!

With that — its time to retire for the week. To spend time with my wife and kids, watch them as their eyes light up when the snow starts to fall (those of you that know me, know that I HATE cold weather) but I’m thankful that my girls enjoy it so much. Their individual laughs are so infectious and if something as simple as snow brings them joy, then I can live with it.

Happy Thanksgiving to you all!!

:wq!

Dreamforce 2016 is Upon Us

As I sit in my home office preparing to start the day, I can feel my mind wander towards what lies ahead for me in just a few short days. I’m reminded of the fact that I’ve been the three Dreamforce events in the past three years and how each has been a completely different experience. It has me looking forward to my fourth Dreamforce and wondering what is going to be different this year.

Lets review for those that don’t know me:

Dreamforce 2013 — I was very new to the platform, just six months in as developer and had met my goal of becoming certified before the event. It was all very new, but I hadn’t been “plugged in” long enough to know anything about what to expect. I was overwhelmed by the sheer scale of it all and like most newbies, I tried to do too much. My first impression, in all honest was: “I don’t get what the big deal is” but the electricity at Marc’s keynote was definitely palpable and when he introduced one of my all time favorite bands — Huey Lewis and the News — the stock of this whole thing went up a notch. While still not completely sold, I traveled home knowing that somehow I’d landed in the right place.

Dreamforce 2014 — shortly before Dreamforce 2014 I started getting plugged into the community. I started to become more and more active on twitter and interacting with the community and discovered a hash tag “ThatsWhySFDCAdminsDrink” and it inspired me to write a song about it. With a sigh of apprehension, I submitted it out to the world and it was met with positive excitement. A few short weeks before Dreamforce I received a phone call from Erica Kuhl asking me if I’d sing my song to open her keynote session. I’d never really done anything like that before on such a grand scale and stage and was scared absolutely sh*tless, but pushing myself out of my comfort zone, I accepted her request. I was thrust into the heart of the community. Opening Erica’s keynote would soon prove to be the catalyst for a life-changing experience. Adding to this already incredible experience, I wound up getting to hang out with my developer twin for most of that week and it was experience Dreamforce (and the community) with her that led me to write what has been my most visited blog entry: “It’s the Community Stupid” and I still believe every word whole-heartedly.

Dreamforce 2015 — in spring of 2015 I found myself invited to join the Salesforce MVP program. I had continued writing more Salesforce based songs, stepped up my blogging, started interacting more on the Success community, etc. I was getting involved. I was volunteering for time in the Admin zone, I was meeting more developers, interacting with anyone that I could — and you’ve all heard me say at one time or another that I am not a people person really but this community really brings it out. Here I was, on my third Dreamforce and my third completely unique Dreamforce experience. I got to open the Dreampark karaoke show with fellow MVP Sarah Deutsch where I played guitar and she sang a couple parodies I’d helped her write. I got to attend the MVP event, and met so many more awesome people that I could never replace in my life. It was here at Dreamforce 2015 that Apex & the Limits was officially formed. We got to play a number of songs for the MVP party and while they were actual covers and not parodies, (that part comes later), we found ourselves becoming fast friends. I also got to be part of the admin comedy hour and sang my “Stop Doing Sh*t In Production” song on that stage. I had a horrible case of Dreamforce flu and things didn’t go exactly as planned, but it was still an experience I’ll never forget.

Dreamforce 2016 — This year will be yet again an entirely new experience for me. I’m speaking on Wednesday at 8:30 in the morning about custom lightning components. I am NOT a public speaker, sure I’ll play guitar and sing, but speaking publicly, as a source of some sort of knowledge to a room full of very knowledgable folks is very intimidating for me. It’d be like me playing and singing before a group of Rock ‘n’ Roll legends — lets face it, I’m nervous as hell. Aside from speaking at Dreamforce this year, I will also be opening the Dreampark once again, this time with Apex & the Limits (and special guest stars) and we will be performing our parodies — something we’ve done before sure, but this time we are playing with live instruments! Seeing as how we are all from different parts of the country — and world — when you hear us at the Dreampark, will be the first time that we’ve actually heard ourselves all together in this manner. It’s been an adventure getting this far and in a few short days it will finally become a reality.

So what am I looking forward to most? Opening the Dreampark, speaking at my session — these are all very cool things and I am both humbled and honored to be selected for doing either. What I’m most excited about however is getting to spend time interacting with the community that I’ve become so passionate about. Hanging out with my band family, my fellow devs and admins, and all of the amazing people in this community. I used to be a wallflower and to some extent I still am. Its VERY hard for me to strike up conversations with people I’ve never met, even when we have something in common. If I’m honest, I’m selfishly hoping that I’m somewhat recognizable now and that other will initiate said conversations just to make it easier, maybe I will wear the cowboy hat the whole time? Anyway regardless of who initiates what, I hope to meet more people from this awesome community this year, so if you wanna meet up at some point, find me on Twitter (it’s where I’m most active)! And with that, my DF16 experience begin very early Sunday morning so I’ve got some resting to do!

:wq!

Change is Good

Okay, I’d like to tell you this is more about my lightning stuff — and its not but I promise to get that stuff wrapped up, but this past month has been a whirlwind of change and all for the good, (albeit a bit stressful).

I have changed employers. Due to circumstances beyond control, my former employer had to close its doors. This was indeed for the better and will provide much less stress for the three of us. We all agreed it was the best option forward. However, I won’t lie — I was scared shitless. My MVP family was so great with numerous options and interviews but in the end I wound up convincing my old employer (of 10 years) to allow me to help them build a Salesforce practice. It took some dancing but everything fell into place….at the very last minute :)

That put a ton of stress on my family, and on myself. But patience won the day and I am now the Salesforce Practice Lead at Flexion, Inc.

While all of this stress was flying around, another little event that started as a simple tweet from fellow Apex & the Limits member Rachel Rogers to Marc Benioff himself, turned into a whirlwind of excitement, nervousness, and again — a bit of stress. We got asked to be the keynote opener at TrailheaDX in San Francisco. We had two weeks to put it all together and I must say, we couldn’t be happier! At one point shortly before we took the stage, we heard one of Marc’s staff say: “We currently have 383,000 live viewers” — lets just say I could have done well without ever knowing that little factoid!

We took the stage and couldn’t have been happier with the result. All the nerves seem to go away when you go out there and see your fellow community members cheering you on. It’s really why we do what we do, for the community, so thank you all so much for that. We look forward to being able to do this more as long as you’ll have us.

In closing — I just want to apologize to anyone that actually follows this blog for my recent absence. Now that I’ve safely landed and the preparation for our TrailheaDX performance is behind me, I hope to get back on a regular schedule and finish my Lightning components blog. In case I don’t get to that, be sure to come to Midwest Dreamin’ in July and catch the live version! And I want to thank my new/old employer Flexion, Inc for understanding the value in being part of this incredible community and giving me the chance to merge the two things I love most.

Thanks again!

:wq!

9904 — Is it an Illuminati Symbol? Or Just Good Fun?

Amber

Do you know this woman? She seems innocent enough, but stick with me my friends as I take you down the rabbit hole, behind the looking glass, make you swallow the red pill (or was it blue, I never remember). I’m about to blow your mind!

Today was Chris Duarte day and I can’t think of a more deserving person to have their very own day. However — something is amiss. Something is not quite what we thought it was. You may know Amber Boaz, or “Amber9904” as she likes to call herself but I’m on to her shenanigans — yes shenanigans! Suspicious, what is this “9904” that shrouded in such mystery? (To be honest I *still* don’t know that answer), BUT as you’ll soon see, today she pulled off a caper right underneath our very noses! She had to have had help from the illuminati…but that’s for a different day.

Let’s examine her twitter timeline from today shall we?

Never

Gonna

Give

You

Not that I need to continue — we know where this is heading!! But here we go nonetheless.

Up

What treachery? What debauchery is this? Did we actually just get Rick Rolled? Not once, but TWICE? Well Played Miss 9904, Well played, but we’re watching you now…we’re watching you.

P.S. I reached out to Miss 9904 for her thoughts on this scheme to which she replied: “No Comment!”

P.P.S. And I still have no idea what 9904 means…but I’m still thinking illuminati.

:wq!

Anyone Can Code

Last week I had the opportunity to help someone who was learning to code through one of the many different programs available to us, the Salesforce community. Whether you’re learning from the RAD Women program, Trailhead, one of the Salesforce provided classes, or simply teaching yourself, you will find at times that you hit a wall where things start to not make sense. Or maybe you start to fall a bit behind when the concepts start getting heavier.

One of the resounding things I hear from people that I’ve helped in this situation is actually rather alarming. What I’m hearing are people saying they feel dumb, or slow, or feeling like an idiot. Please, for the love of all that is good STOP thinking this way. What you are doing is hard, and its new and likely something you’ve never done before. Think of it like learning a new language or learning to play the piano. You wouldn’t sit down after a few short weeks and expect to crank out the Bach, Mozart, Beethoven symphonies would you? It’s all very much the same idea: practice, practice, practice.

I posted a quick Periscope about this over the weekend but seeing as how those things disappear after 24 hours, I thought perhaps instead of posting about my usual stuff, (don’t worry more lightning coming soon), that I’d just post these words of encouragement for those that may need it.

Just keep doing what you’re doing and remember: I’m always here to help.

:wq!

Trailhead on the Road

Today is the Salesforce World Tour Chicago event. Sadly, I’m here…writing this…whilst many of my colleagues are in Chicago, but I digress. I’m here for a purpose (besides billable hours). At this event, there is a flyer being circulated talking about this wonderful thing called “Trailhead on the Road” and while I didn’t create this thing, there are numerous questions being asked and the person who can answer those questions is nowhere near a laptop at the moment and is enjoying a sunny spring break in Florida. Therefore, hopefully you’ll allow me to fill that gap momentarily.

Like many great ideas…it was born whilst sitting in the airport coming down from another week at Dreamforce. For the background, you can read this, go ahead, I’ll wait…

All caught up? Good. As we were wrapping up that event, someone mentioned that we need to take this “on the road” which started another run of excitement. What would that look like? Where would we go? Could we even pull it off? The answer to those questions are: Pretty freakin’ awesome, wherever we can, dear God I hope so. :)

We are attempting to pull off our first “On the Road” event in Chicago on May 7th, 2016. The final content is still being produced but it will be lightning heavy on both the admin and dev sides. We will have several Salesforce.com MVPs in attendance, and will have two tracks. Each track may even go “off road” just a bit to assist in getting a bigger picture and a more “real world” example.

The event is not sponsored by any entity in particular so no sales pitches, just like minded devs and admins getting together to crack a few modules, and do some off-roading with Trailhead (and beyond).

If you’ll recall, I believe back in July of 2015 there was a “Trailhead Live” promotion all over the country. Its quite a bit like that but is being put on by members of the community.

Who knows where it will take us, but feel free to reach out to myself (in the comments and on twitter), or @JennyJBennett or the mastermind behind the whole deal: @chattyadmn for more information.

Until next time…

:wq!

Thinking About Lightning Application Architecture: Part 1

One of the things that really surprised me when I started poking at custom Lightning Components/Apps was the fact that I truly had to handle navigation from scratch. As a developer, we’re taught to never re-invent the wheel and to write code that is re-usable up front. Over the next few posts, I hope to discuss what I’ve found, what I’ve tried and what I’ve ultimately come up with when facing these challenges.

When I started out, I knew that navigation was something that I didn’t want to have to solve every time I set out to write an application. I knew I’d need a core group of components and events that would work in concert with one another and I knew that I’d have to keep these components generic enough that they could be included in any apps that I wrote. What I’ve come up with thus far appears to be working and I’ve done a demo for a couple different people and haven’t had any major “gotchas” pointed out to me yet. I’m still trying to get the ear of some other folks that I know are much more into Lightning than I am just to level set my expectations and find out if I’m really off in the weeds or not.

Before I get into the nitty-gritty, the back story here is that I had a customer approach me about creating a Visualforce page for a very complex custom object in their org. They wanted to be able to use the page in both the browser and on Salesforce1. If this were a simple object, this could have been simpler I imagine, however there are numerous dependent picklists that forced the original VF page to be spread out amongst several other clicks/refreshes, etc. This was not going to translate well to Salesforce1. Also, we wanted to avoid two different processes, one for mobile and one for the web. I made a couple of suggestions and one of them was to create a custom app that could be “created” via App Builder for use with Salesforce1 and an app that could be accessed via the web interface. (I’ve since done some playing with lightning out and prefer going that route for the web interface as it feels more native to the Salesforce experience).

However, budgeting for such an endeavor was a challenge because I’d never done anything like this before. I’ve been through the trailhead modules, taken a HOT at Dreamforce and toyed with it a bit on my own but only using rather simple examples. This was a chance to step back and tackle a “real world” scenario. I dove in far enough to realize, as some of my past blog posts have eluded to, that Lightning really is a “framework” in the most true sense of the word. There are just enough features there for you to work with, but you are left to your own devices to get things off the ground. Therefore, I started an example project in a dev org and this is what I’ll be going over in the coming posts.

Since #TahoeDreamin had just ended, and Apex & The Limits had been asked to travel to #SEDreamin — conferences were stuck on my mind. So my sample application was a very basic conference management application, its not fully baked and I plan to write more, but currently its just venue’s and conferences because…well consulting and billable hours take precedence. (Hey kids gotta eat man!)

The actual components are simple enough that I could use pre-built items for display, etc but I wanted to see how the system would behave with completely custom pages/forms, etc. since my clients’ application was not going to be able to take advantage of things like default page layout, record pages and what have you.

Rather than discuss the ins and outs of creating my custom objects, I’ll just tell you what I have and leave you to your devices to create them (or something similar) if you’d like to follow along. We have a Conference object and a Venue object. Each object has its own information, like addresses, start dates, end dates etc. Again since that’s not at all what this post is about, I don’t want to spend too much time here. Let’s get to thinking about the structure of the application since we are trying to be generic anyway.

Navigation:
I wanted to be able to navigate to a list of conferences or a list of venues so I knew I would need a basic component to display these links to me. I knew I would need custom components to display these lists and informational pages. What I didn’t know was how to navigate between the two. My intuition told me that selecting a record would need to fire some sort of event that I could handle and initialize/load the appropriate component. I had been googling and not found a standard, built-in navigation method. I did find e.force:navigateToSObject and other similar events, but those only work in Salesforce1 (according to the docs) & I needed this to work in the web interface as well. On top of that, I needed a component to fire that event anyway. I knew I needed to fire a custom event and it was looking like I would also need a component to fire it.

However, that’s only part of the problem. Even if I managed to fire this custom event, how were people swapping components in and out. And then I came across this. Basically, I needed to create a container component that would house my custom components and swap them in and out of there.

The last piece I needed was some generic code to be able to handle my custom event, get the name of the component I intend to load and have my container component’s controller load that component. I created a “Base Component” that will be my container and a matching controller that will initialize my entire app (based on a parameter, but more on that later) and also handle my “navigation” by swapping out custom components as signified by my custom event. That looks something like this (I’m using the Lightning Design System to style things):

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes" controller="BaseComponentController">
	
	<aura:attribute name="appName" type="String" />
	<aura:attribute name="activeComponent" type="String" />

	<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
	<aura:handler event="c:NavigateToComponent" action="{!c.loadComponent}" />

	<div class="slds" style="padding: 10px;">
		{!v.body}
	</div>

</aura:component>

And my Base Component’s controller (NOTE: much of this code should be moved to the helper class, I just haven’t gotten there yet):

({
    doInit : function(component, event, helper) {
            //ignore this bit for now, more on that later
            var action = component.get("c.getDefaultComponent");
            action.setParams({
            	"appName" : component.get("v.appName")
            });

            action.setCallback(this, function(response) {
            	var state = response.getState();
            	if(state === "SUCCESS") {
            		//load new component
            		var defaultCmp = response.getReturnValue();
            		$A.createComponent(
            			defaultCmp,
            			{},
            			function(newComponent) {
            				component.set("v.body", newComponent);
            			}
            		)
            	}
            });

            $A.enqueueAction(action);
    },

    //this is the custom swapping of components piece...
    loadComponent : function(component, event, helper) {
        var newCmp = event.getParam("targetCmp");
        var attrs = event.getParam("attributeList");

        $A.createComponent(
			newCmp,
			attrs,
			function(newComponent) {
				component.set("v.body", newComponent);
			}
		)
    }
})

The “magic” here lies in my controller’s loadComponent function. You will notice that my Base Component is handling a custom event called “navigateToComponent” — this event gets fired by another custom component (we’ll discuss that in my follow up post), my base component will handle that event and inspect it for the name of the component I’d like to load as well as any custom attributes my new component will need, such as the ID of an object I’m trying to load into that component. This function calls $A.createComponent which loads an instance of my component, and swap it into place where my Base Component has {!v.body} specified.

So now “theoretically” (this is all just my observations, but it is indeed working) I can fire my custom event, pass in the name of any component and its attributes and load it without my Base Component having to know anything about the component it is loading nor its data. If I use this base component in all my future apps (along with my custom event), navigation is solved for me.

That’s plenty of writing for me, and reading for you at the moment I think. Hopefully next week, I’ll have the custom event queued up for you :)

:wq!

Apex Logic Through Formulas: Part 3

I’ve had to reach into my own bag of formulas to find an example for this week’s entry. Last week I was focused on our local user group so we didn’t have an entry, this week I’d like to make up for that a little bit. This week’s formula is a bit more involved but nothing crazy:

formulas3-1

Let’s walk through what the formula does:
First we have an AND function wrapped around everything. If we think of the AND function as this box that we just put things into and shake up and look inside our AND box will contain a value of either TRUE or FALSE. If everything we threw inside our AND box is TRUE, then our overall value should be TRUE. If even one item in our AND box is FALSE, the whole thing is FALSE. The trick here is that our AND box contains another BOX that can have its own items inside. This inner box is an OR box and in this case only one of the items inside needs to be TRUE in order for the whole BOX to be TRUE.

So the first item we put in our AND box is checking a Boolean field on our object. This is a checkbox that is basically telling us that this “item” is external. (Let’s ignore the business case for now as its irrelevant, just know that this is a checkbox that will either be check or unchecked. Since a Boolean value is by default either TRUE or FALSE we don’t need to do any sort of comparisons, here. However we did wrap this in a NOT function. So, if our External Item checkbox is indeed checked, (therefore TRUE) our NOT box is reading NOT(TRUE). So basically if our checkbox is checked, this would return FALSE thereby making our entire AND box false. Clear as mud? We’ll try to clear that up when we have an actual example, but for now lets check the next part.

Lines 3 through 7 contain our OR box. We are checking to see if our Opportunity is either in a stage of VALUE1, VALUE2, or VALUE3. (The stages have been changed to something generic as this is an actual formula in an org and I’m not comfortable giving away too much information). So if our Opportunity is set to any of those three stages, our entire OR box will be TRUE. Incidentally if we only had four stages, this OR box could have been removed and we simply could have said inside our AND box: “NOT(ISPICKVAL(StageName, ‘VALUE4’))” meaning if it isn’t VALUE4 than it must be one of the other values, but I digress.

Lastly we are checking a multi-picklist field (ssshhhh don’t tell @SteveMoForce) to see if one of the selected values is ‘APPLICATION’ and if so, then that part will also be TRUE.

Lets walk through an example before we get into code. Lets say we have the following Opportunity information:

External Item: unchecked
StageName: VALUE2
Item Key: SERVICE and APPLICATION are selected

This formula would return TRUE on line 2: NOT(External_Item__c) which is saying NOT(TRUE). If something isn’t TRUE than it must be FALSE. So we are checking to see if External_Item__c is FALSE (unchecked) which it is indeed unchecked, so the result of our NOT box is TRUE, since External Item is not TRUE (checked). (I know its still mud…but you’ll get it).

For our OR box, we are NOT in StageName ‘VALUE1’ but we are in StageName ‘VALUE2’ — so since one of our items in our OR box is true, the whole OR box is TRUE.

Lastly, our Item Key indeed contains the text ‘APPLICATION’ so that too is TRUE. So basically our big AND box that wraps this whole thing contains:

AND(
    TRUE,
    TRUE,
    TRUE
)

So everything in our AND box is TRUE and therefore our whole box is TRUE and this rule would fire. Its reminiscent of “solving for X” using substitution back in our Algebra days, (and you thought you’d never USE that stuff).

Now let’s code this puppy. Again let’s assume that I’ve done the proper querying of my Opportunity and included the fields that I am interested in. As we did back in lesson 1, we are going with a very verbose solution. This could be written in so many different ways and the idea here is to help you simply tie the logic together. We’ll refactor it, but lets start with using perhaps some nested IF statements (again, this code won’t compile on it own):

formulas3-2

Line 1:
This is just our function declaration. Let’s not worry too much here at the moment as the logic is within the braces.

Line2: We are checking to see if the External Item is NOT True (unchecked). This matches our formula’s use of the NOT() function. We could have also written it like this:

if(myOpportunity.External_Item__c == False) {...}

…but that wouldn’t have really matched what our formula was saying. It would have been equal, but not “equivalent” ;)

Line 3:
Using our Opportunity example from above, that would pass and we would fall into our next IF statement on line 3. Here we are checking the Opportunity’s StageName to see if its either ‘VALUE1’, ‘VALUE2’, or ‘VALUE3’. The || symbol in our if statement means “OR” and follows the same rules as the OR box in our formulas. If any of those comparisons are True, then the whole thing passes through to the next line.

Line 4:
Here we are checking to see if our multi-picklist field as APPLICATION for one of its selected values. In Apex, you can use the “contains” function to look into some text to see if a given sequence of letters or numbers are present. Also, a multi-picklist field’s selected values in apex appear as one long string with each selected item separated by a semi colon. So our example would have: ‘SERVICE;APPLICATION’ as the value for that field. PLEASE NOTE: this code as written could wind up in a bug because if for some reason your multi-picklist had both ‘APPLICATION’ and ‘APPLICATION LICENSE’ as viable selections, and someone chose the latter, our code on line 4 would still find the word “APPLICATION” in our selection even though it was part of the “APPLICATION LICENSE” selection. So there is a better way to do that by splitting up the selections into a list and looking for the value that way, but that lies beyond the scope and I don’t want to dive too deeply too soon.

Upon passing line 4 successfully, we return a value of True in our function and we are done. If at any point we don’t pass the if statements, we fall out and wind up on line 9 where we return false.

As stated there are many other ways to write this:

formulas3-3

or

formulas4-3

but my point is to get you to focus on the similarities of the logic between Apex and Formulas. We can worry about writing more concise code once you get more comfortable with the concepts. Your homework? Yes there is homework. Given the following Opportunity, will it pass our tests or fail them? If it fails, where will it fail?

External Item: checked
StageName: VALUE3
Item Key: 'SERVICE' and 'APPLICATION' are selected

or

External Item: unchecked
StageName: VALUE4
Item Key: 'SERVICE'

I hope this helps you along your path. As always, I am open to suggestion and send me those formulas!

:wq!