One Page Project: Promocode Checker

Updated 13/09/2022

In this tutorial, we are going to create a system that automatically checks promotions against a promo code provided by the customer. If valid then we will create a request to have a voucher sent to them. This is a version of a piece of automation I used to tackle a long-time manual process.

Scenario

The scenario is you want to open a request to send vouchers of some description to a customer that orders additional services from you during a promotion. You want it to be able to filter out late submissions and only when an opportunity is closed won.

Your sales team creates the opportunity manually when they have a customer on the phone. If the customer quotes a promotion code on the phone the salesperson will fill out the custom field required

Initial Set Up

First, you need to create a custom field on the opportunity object. Create it as a text field so it can hold both letters and numbers.

Custom Metadata Type: Promo code

Next, we need to create a custom metadata set. To do this you need to go to setup then custom settings. Click new on custom settings and create the object with the name of the Promo code. Then create the following fields.

  • Start Date a date field that will be populated with the first day of the promotion
  • End Date a date field that will be populated with the last day of the promotion
  • Information is a long text field that holds the information about the promotion to be pulled on to the request
  • Amount is a number field that pulls through the value of the vouchers.

When naming each entry of the promo code you will use the name field to hold the promo code which will be done for matching purposes in the flow. So if the promotion is NEWYEAR23 then the name of the promo code custom setting would be NEWYEAR23.

Custom Object: Voucher Request

Now we are going to create an object which holds the awarded vouchers on them. We will call this object Voucher Request and have the name field set as auto number VR-{00000}. We should now create the following custom fields.

  • Date Award a date field that will be used to record when they are been awarded them.
  • Promocode a string field that will hold the promotion code
  • Information a field that will pull from the information field on the metadata object
  • Amount is a field that will hold the Amount value of the vouchers.
  • Company a lookup to the company pulled from the opportunity.
  • Contact a lookup to the contact who is the contact on the opportunity.
  • Status a picklist that will have the values of Sent, Not Sent.
  • Opportunity a lookup to the opportunity it was generated from

List View Voucher Request

Go to voucher request and create a custom view. In the filter criteria just have it display all records at not sent. This would give your team a list to work through. Click save on this and pin this view name the view Vouchers To Send.

Automation

We will be building a single record triggered flow the criteria will be when an opportunity is set to closed won, and the Promo code is not blank that way we are not running it all the time and only when a promotion is present. Go to flows in setup and click add new. Select Record Triggered Flow. This will be an after-save flow as it will be accessing one set of data and creating another.

Record Get: Promo code

We are going to get the promocode__mdt the criteria we are going to check for is name equals the opportunity promo code field. Start Date is Equal to or Greater than Today’s date, and End Date is Equal to or Less than today’s date. You can leave the rest of the element as it is.

Decison: Promocode Found?

In the decision, we are checking whether or not the promo code record was found if not then this leads to flow end. If we do find it then we want to pull the information down from it with information from the opportunity to the Record Create.

Record Get: Contact Role

Get the primary contact role related to this opportunity we need the contactid from this to assign to the voucher request. Filter by opportunity equals Record.Id and isPrimary equal true.

Record Create: Voucher Request

Now we will populate a record to create. Select Opportunity and populate the values. You need to populate the values with

  • Date Awarded = {$Flow.Date}
  • Promocode = {!Promocode.Name}
  • Information = {!Promocode.Information}
  • Status = Not Sent
  • Amount = {!Promocode.Amount}
  • Account = {!Record.AccountId}
  • Contact = {Contact_Role.ContactId}
  • Opportunity = {$Record.Id}

Now, this is done you can save this, and the flow should look like the image below.

Full graphical overview of the flow that is built in this tutorial for checking a promocode.

Testing Automation

Now all that is left to test but before we do this but just before this we need to put some test data in.

Testing Data

Promo code Data

Populate a Promo code record with the name TEST and put the end and start date to whatever your date range is for testing so the current date and maybe an additional date. Put the amount at 100 and put some text in the information field.

Opportunity Data

Fill out an opportunity don’t set it to won make sure it has a promo code filled with TEST and make sure it has a contact role assigned to it. If not add one. Once you have all this data in place.

Testing Flow

Go to debug and update the select the opportunity you want to test on. Change the stage to Won and click run. Look through the debug sidebar to check it has done everything. If you get any errors check through the errors to see where it has gone wrong.

Further Learning

Want to work on some more one-page projects check here. If you want to check out tutorials in general click here and if you want to have a read-through of the tips if you are currently going through workflow migration. Salesforce also has Salesforce Trailhead which is a fantastic way of learning.

Other Posts