General Helpers
Hsl.extend
Hsl.extend(target: any, ...args: any[]): T;
Extends the first parameter with the non-undefined values from each subsequent parameter. This works very similar to Object.assign but skips properties whose value is undefined.
Hsl.extend({ x: 5, y: "abc" }, { x: 7, z: 1 }) // { x: 7, y: "abc", z: 1 }
Hsl.extend({ x: 5 }, { x: 4}, { x: 3 }) // { x: 3 }
Hsl.extend({ x: 5 }, { x: 4}, { x: undefined }) // { x: 4 } the value from the last object isn't
// used because it is undefined
var settings = { width: 55, height: 100 };
Hsl.extend(settings, { width: 60 })
settings.width // 60, the first parameter is mutated
Hsl.type
Returns an object's type. This method can be useful when writing dynamic code to interact with multiple field types. It also handles cross-frame objects correctly.
Hsl.type(obj: any): string;
// Examples
Hsl.type({}); // "object"
Hsl.type([]); // "array"
Hsl.type(2); // "number"
Hsl.type("abc"); // "string"
Hsl.type(new Date()); // "date"
Hsl.type(/abc/); // "regexp"
Hsl.type(null); // "null"
Hsl.type(undefined); // "undefined"
Hsl.type(function () {}); // "function"
Hsl.type(new Error()); // "error"
// Custom scripts are loaded in a separate iframe from the actual CRM Form. As the result, instanceof Date will not work. Hsl.attribute('datefield').getValue() instanceof Date; // false // Instead, use Hsl.type, which does work across frames. Hsl.type(Hsl.attribute('datefield').getValue()) === "date" // true
Hsl.String.format
Utility functions related to strings. Either Hsl.String or Hsl.Str can be used to access this namespace.
A function that allows a format to be specified with placeholders that will be replaced by any additional arguments specified. The first argument must always be a format string that may contain placeholders in the form of "{n}" where n is a zero-based index for the placeholders. The remaining arguments represent the values that will be substituted for the placeholders in the string, i.e. the first argument after the format string will replace any placeholder of "{0}" and the second argument after the format string will replace any placeholder of "{1}". Any placeholder in the string that does not have a corresponding value specified as an argument will be replaced with an empty string.
Hsl.String.format(formatString: string, ...placeHolderValues: string[]): string
// Results in "The account Contoso has 3 active opportunities"
Hsl.String.format("The account {0} has {1} active opportunities.", "Contoso", "3");
Hsl.xmlEncode
Encodes text to be placed within an XML document.
Hsl.xmlEncode(text: string): string;
var lastname = "O'Neil"; // Last name contains characters that need to be encoded to put in the fetch xml.
var filterXml = "<filter>" +
"<condition attribute='lastname' operator='eq' value=' " + Hsl.xmlEncode(lastname) + "' />" +
"</filter>";
openEntityForm
Opens the form for the specified record.
Hsl.openEntityForm(name: string, id?: string, parameters?: Xrm.Utility.FormOpenParameters, openInNewWindow?: boolean): void;
Hsl.openEntityForm(name: string, id?: string, parameters?: Xrm.Utility.FormOpenParameters, opts?: OpenEntityFormOptions): void;
- name: string – Logical name of the entity to open the form for.
- id?: string - Id of the record to open the form for. If not specified, a new record form will be opened.
- parameters?: FormOpenParameters – String key/value pairs of additional parameters to pass.
- formid - the id of the form to open
- navbar – "on" to display the navbar, "off" to not display the navbar, "entity" to only show the options for related entities.
- cmdbar – "true" to display the command bar, "false" to not display the command bar.
- Additional parameters matching the names of attributes on the form to set the value of for a new entity form or custom form parameters. See https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/developer/set-field-values-using-parameters-passed-form for details.
- The fourth parameter is an option object with the following properties:
- openInNewWindow: boolean – whether to open in a new window
Hsl.WebResource.createUrl
Returns the URL for a web resource given the name of the web resource. Additionally, CRM context parameters and/or additional custom parameters may be added to the URL. If additional parameters are added they should be specified as a query string without the leading "&" or "?", e.g. "param1=1¶m2=something".
It is encouraged to use this method since it puts a web resource caching token in the URL. Without it, CRM will not cache web resource pages. If the browser supports document.currentScript, that is used to generate the URL instead of the caching token.
Hsl.WebResource.createUrl(wrName: string, dataParameter?: string | Dictionary<string>): string;
Hsl.Guid
equals
Determines whether two guids are equal. This is the recommended approach rather than check for string equality since the guids coming back from CRM's APIs have different formats. This method also treats null, undefined, and empty string as being equal.
Hsl.Guid.equals(a: string, b: string): boolean;
Hsl.Guid.equals("87AD3517-1E4F-4C85-85C9-39B9DF734B51", "{87ad3517-1e4f-4c85-85c9-39b9df734b51}") // true
Hsl.Guid.equals("", null) // true
Hsl.Guid.equals(null, undefined) // true
Hsl.Guid.equals("87AD3517-1E4F-4C85-85C9-39B9DF734B51", "97AD3517-1E4F-4C85-85C9-39B9DF734B51") // false
clean
Converts the guid into lowercase, with dashes, without curly bracks. If passed null
or undefined
this method will return null.
Hsl.Guid.clean(guid: string | null): string | null
Hsl.Guid.clean("{87AD3517-1E4F-4C85-85C9-39B9DF734B51}")// returns '87ad3517-1e4f-4c85-85c9-39b9df734b51'
normalize [Deprecated]
Converts the guid into a standard format – upper case, with dashes, without curly brackets. If passed an empty string, null, or undefined, this method returns null.
Hsl.Guid.normalize(guid: string): string;
getNew
Generates a new, random guid as a string. Formatted in the same format Hsl.Guid.clean would give.
Hsl.Guid.getNew(): string;