Tuesday, July 2, 2019

Creating an IIS Redirect Rule from Subdomain to the Root Domain

IIS Rewrite Rules have a lot of inherent complexity.  They use regular expressions and are difficult to debug.  The server variables have little documentation provided by Microsoft.  Recently we had an ask from the business to redirect any requests for subdomains to the main domain.  While this is simple to understand it is difficult to write.

Before you create and debug a rewrite rule you need a couple things:
1.  Install IIS by going into Programs and Features then Windows Features
2.  Install the Web Platform Installer https://www.microsoft.com/web/downloads/platform.aspx  This installs modules for IIS like NuGet installs packages for .NET
3.  Use the Web Platform Installer to install the Rewrite Module

Here is the end result:



Match URL
The thing that hung me up for most of the time was the match url.  This is not the full URL as you would expect but simply the page that was requested like default.aspx.  This is Mistake #1 from Lex Li:  https://blog.lextudio.com/the-very-common-mistakes-when-using-iis-url-rewrite-module-a2ab7e4fee59

Condition Order
In order to do back references to conditions (see the {C:2} and {C:3} variables) you have to put the negate false conditions first.  If you don't the back reference does not work.

HTTP_HOST Matching
If you do not put the regular expression carat ^ for start and the dollar $ sign for end, IIS will redirect until the URL is too long and it throws an error.

Putting It All Together
https://www. - We always want to redirect to secure www
{C:2} and {C:3} - This is a back reference to the last condition, the last set of parenthesis.
{R:1} - This is the requested page from the Match URL

Debugging the Redirect Rule

1.  Here is some excellent documentation how to create a simple test file to debug your rewrite rule:

2.  I used Expresso to debug the regular expression to match the subdomain.rootdomain.  You can also use IIS by double clicking on the Re-write module for the site.    http://www.ultrapico.com/expresso.htm  

3.  What I found to be very useful was to change the redirect url to a query string to see what the variables were.  
<action type="Redirect" url="http://www.somesitethatdoesnotexist.com/?{C:2}-{R:1}" appendQueryString="false" />


Here is the reference documentation from Microsoft:
https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-20-configuration-reference





Friday, June 21, 2019

Resetting the Admin Password in Sitefinity

If you lost the admin password for a site you can reset the password for the admin user in the database.  This will set the password format to clear text with no salt for the encryption.

UPDATE sf_users
SET    passwd = 'password',
       salt = NULL,
       password_format = 0
WHERE  user_name = 'someadminuser' 

Tuesday, May 21, 2019

How to redirect from HTTP to HTTPS in a web.config in CrownPeak CMS


<rewrite>
  <rules>
  <%
  if (context.PublishingPackage.PackageName.Contains("Live")
    {
    %>
    <rule name="Redirect to https" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
    </rule>
    <%
  }
  %>
  </rules>
</rewrite>

Monday, February 11, 2019

What happened to my Salesforce System.Debug statements?


On our project we have complex report generation which creates a debug log file at the 5MB limit when the Apex Debug Log Level is set to finest.  According to Salesforce:

“Each debug log must be 5 MB or smaller. Debug logs that are larger than 5 MB are reduced in size by removing older log lines, such as log lines for earlier System.debug statements. The log lines can be removed from any location, not just the start of the debug log.“


If you just want to see the debug statements, simply log at the error level and then set everything else to none.

    System.debug(LoggingLevel.ERROR, ‘some logging message’);

Debug Levels
  • Database : None
  • Workflow : None
  • Validation : None
  • Callouts : None
  • Apex Code : Error
  • Apex Profiling : None
  • Visualforce : None
  • System:  None
  • Wave: None
  • Nba: None

If you want to debug in Visual Studio at the Apex finest level, break your code up into small testable pieces so that you can pinpoint errors and the log files can be under the 5MB limit.  The Single Responsibility should be your best friend when working in Salesforce.

Augmenting Salesforce CLI Commands for VS Code


I have been working with the Salesforce Extension Pack for VS Code and I have been mostly pleased with the capabilities.  On my current project, I find myself constantly switching between orgs.  I was tired of typing in the command to list out the current org and the command to switch the current org.  
There is a great hotkey VS Code extension called terminal-command-keys that allows you to create a hotkey that executes a terminal command.  The Salesforce CLI with hotkeys; what could be better!
Here are the steps to use
  1. Install the VS Code extension terminal-command-keys
  2. Edit your keybindings.json by pressing Ctrl-Shift-P on the PC or Command-Shift-P on the Mac.  Edit the file on the right



Add a command to list all your orgs by pressing Ctrl-Shift-l
    {
    "key" : "ctrl+shift+l",
    "command": "terminalCommandKeys.run",
    "args" :
        {
            "cmd": "sfdx force:org:list",
            "saveAllFiles": false
        }
    },

Add a command to upload the current file by pressing Ctrl-Shift-u
    {
        "key": "ctrl+shift+u",
        "command": "terminalCommandKeys.run",
        "args": {
            "cmd": "sfdx force:source:deploy --sourcepath  ${file} --loglevel fatal",
            "newTerminal": false,
            "saveAllFiles": true,
            "showTerminal": true
        }
    },

Add a command to switch to your development org.  In my case, the alias is BossSand. Replace with the alias for your org.
    {
        "key" : "ctrl+shift+d",
        "command": "terminalCommandKeys.run",
        "args" :
        {
            "cmd": "sfdx force:config:set defaultusername=BossSand",
            "saveAllFiles": false
        }
    },

Add a command to switch to your test org.  In my case the alias is BossStage. Replace with the alias for your org.
    {
        "key" : "ctrl+shift+v",
        "command": "terminalCommandKeys.run",
        "args" :
        {
            "cmd": "sfdx force:config:set defaultusername=BossStage",
            "saveAllFiles": false
        }   
    }


Friday, January 18, 2019

Debugging Salesforce Tests with VS Code

Debugging in Salesforce is relatively new and it immature compared to debugging in Visual Studio or Eclipse.  Even Microsoft Access has a better debugger than Salesforce.  Essentially logs files are created when running tests and then the log files are used to step through the code.  It is after the fact debugging, you cannot change the path that the code has gone.

If you haven't already, set up VS Code for Salesforce


  1. Click the Debug icon
  2. In the drop down list at the top under Debug, Add Configuration.  Select Apex Replay Debugger
  3. Make sure that you have pulled all your code and tests down from source control or from your org.
  4. Using Ctrl-Shift-P on Windows or Command-Shift-P on a Mac, select SFDX: Turn On Apex Debug Log for Replay Debugger
  5. Set a break point in your test
  6. Run your test by clicking Run Test above the test method name in VS Code 
  7. Using Ctrl-Shift-P on Windows or Command-Shift-P on a Mac, select SFDX: Turn Off Apex Debug Log for Replay Debugger
  8. Using Ctrl-Shift-P on Windows or Command-Shift-P on a Mac, select SFDX: Get Apex Debug Logs
  9. Click the Debug icon
  10. Click the Launch Apex Replay Debugger
  11. You can see local variables on the left and you can step into code

Setting Up VS Code with Salesforce

These are step by step instructions to get VS Code working with Salesforce.


  1. Go to your Sandbox, under Setup search for Dev Hub.  Enable Dev Hub and GA.  This will allow you to create scratch orgs.
  2. Install the Salesforce CLI which will allow you to push and pull code and run tests:   https://developer.salesforce.com/tools/sfdxcli
  3. Install VS Code which is now the standard for editing code for Salesforce:  https://code.visualstudio.com/download
  4. Install Java if you do not have it installed yet.  The Salesforce Extension Pack requires it.  https://www.java.com/en/download/
  5. Install the Salesforce Extension Pack.  In VS Code, click the extensions icon and search for Salesforce Extension Pack.
  6. Install Lightning Web Components extension in VS Code
If your Salesforce code is in your org and not in source control then you are in Classic Development and not Salesforce DX.  

Classic Development Mode (directly interact with an Org)



* After creating the project, create these folders under the default folder before doing a Retrieve Source From Org:  

  • classes
  • objects

Salesforce DX Mode (your source is in GitHub or Bitbucket)

Example of cloning a repository, creating a scratch org and pushing the code

git clone https://github.com/dreamhouseapp/dreamhouse-sfdx
cd dreamhouse-sfdx
sfdx force:config:set defaultdevhubusername=your@username.com
sfdx force:org:create -a dreamhouse -s -f config\project-scratch-def.json -d 7
sfdx force:source:push
sfdx force:user:permset:assign -n dreamhouse
sfdx force:org:open -p /lightning/page/home