To call Apex methods, this module provides the apex, and apexCacheable functions. The Apex method to be called must be a global class, implementing the System.Callable interface.
1global with sharing class ApexTest implements System.Callable {2 global class ApexTestException extends Exception {}34 // Interface method5 public Object call(String method, Map<String, Object> params) {6 // Both method and params will be received7 if (method == 'getAccounts') {8 return getAccounts(params);9 } else {10 // Exceptions can be thrown11 throw new Exception('Invalid method!');12 }13 }1415 private static List<Account> getAccounts(Map<String, Object> params) {16 // Validate parameters17 if (params == null || String.isBlank((String) params.get('searchKey')) == null) {18 return new List<Account>();19 }2021 // Extract parameters.22 // NOTE: Javascript Dates are received as Strings, they need to be parsed 23 // with24 // Date newDate = Date.valueOf((String.valueOf(dateString)));25 String searchKey = String.valueOf(params.get('searchKey'));26 String searchKeyProcessed = '%' + String.escapeSingleQuotes(searchKey) + '%';2728 // return29 return [SELECT Id, Name FROM Account WHERE Name LIKE :searchKeyProcessed LIMIT 100];30 }31}
In your custom LWC, use the apex, and apexCacheable methods. The difference between apex, and apexCacheable is that apexCacheable uses the Lightning Web Components caching layer.
1import { api, wire, LightningElement } from 'lwc';23import {4 CGCloudNamespaceWithUnderscore,5 apexCacheable,6 apex,7 getResponse,8 refreshApex9} from 'cgcloud/tpmGenericUtils';1011export default class MyApexTestComponent extends LightningElement {1213 imperativeData = null;1415 wiredResponse = null;16 wiredData = null;1718 // Store the params for the request19 requestParams = {20 searchKey = ''21 };2223 // It's possible to use the wire adapter only with the apexCacheable method24 @wire(apexCacheable, {25 descriptor: {26 className: 'ApexTest',27 method: 'getAccounts'28 },29 params: '$requestParams'30 })31 wiredFunction(value) {32 this.wiredResponse = value; // Save wired data to be able to call33 // refreshApex later34 const { data, error } = getResponse(value); // Use 'getResponse' to extract data35 if (error) {36 console.error(error);37 }38 this.wiredData = data;39 }4041 onSearchKeyChange(event) {42 // Set the new parameters43 this.requestParams = {44 searchKey = event.detail.value45 };46 // The change to requestParams will trigger a refresh of 47 // the wire adapter data.48 }4950 onOtherEvent() {51 // You can force-refresh the wire adapter data52 // with refreshApex53 refreshApex(this.wiredResponse);54 }5556 // You can call Apex method imperatively57 imperativeApex() {58 apex({ // You can also use apexCacheable to cache the response59 descriptor: {60 className: 'ApexTest',61 method: 'getAccounts'62 },63 params: this.requestParams64 })65 .then(getResponse) // use getResponse to extract data66 .then((data) => {67 this.imperativeData = data;68 });69 }70}
Example: Load Static Resources
To get the URL of the static resource to be loaded, use the getStaticResourceURL function, and then load the static resource using the standard Lightning Web Components tools.
1import { LightningElement } from 'lwc';2import { loadScript, loadStyle } from 'lightning/platformResourceLoader';3import {4 getStaticResourceURL5} from 'cgcloud/tpmGenericUtils';67// getStaticResourceURL returns a Promise8const myZippedResourceURLPromise = getStaticResourceURL('MyZippedResource');910export default class MyStaticResourceComponent extends LightningElement {1112 connectedCallback() {13 // Wait for the Base URL promise to resolve14 myZippedResourceURLPromise15 .then((baseURL) => Promise.all([16 // load your resources17 loadScript(this, baseURL + '/MyScript.js'),18 loadStyle(this, baseURL + '/MyStyle.css')19 ]))20 .then(() => {21 // You can use your resource after loaded22 window.MyApp.run();23 });24 }25}
Example: Access the Internationalization Properties
1import {2 UserId3} from 'cgcloud/tpmGenericUtils';45console.log(UserId);67// 005TC0000002jXJYAY
Example: Get the Client Form Factor
The FORM_FACTOR attribute returns the client form factor.
1import {2 FORM_FACTOR3} from 'cgcloud/tpmGenericUtils';45console.log(FORM_FACTOR);67// Large
Example: Get the CGCloud Namespace
You can get the CGCloud namespace through the properties, CGCloudNamespace, and CGCloudNamespaceWithUnderscore.
1import {2 CGCloudNamespace,3 CGCloudNamespaceWithUnderscore4} from 'cgcloud/tpmGenericUtils';56console.log(CGCloudNamespace);78// cgcloud910console.log(CGCloudNamespaceWithUnderscore);1112// cgcloud__
Example: Use the TPMManualInput Class
The TPMManualInput class represents a manual input entry for the TPM grids. When reading manual inputs on TPM components, you receive an array of instances of the TPMManualInput class. By manipulating and setting this array again into the TPM component, you can change the loaded inputs for the TPM grids.
Methods
The TPMManualInput class supports the following methods:
getKPICode(): Returns the manual input editable KPI code.
getPeriodDateFrom(): Returns the period from date for the manual input. If the manual input is for the total period, it returns null.
getPeriodDateThru(): Returns the period through date for the manual input. If the manual input is for the total period, it returns null.
getTacticId(): Returns the tactic ID of the tactic the manual input applies to.
getProductId(): Returns the product ID of the product the manual input applies to.
getComponentId(): Returns the component ID of the BOM Component the manual input applies to.
getValue(): Returns the value of the manual input.
Constructor
The constructor for creating an instance of the TPMManualInput expects the options object as a single parameter. The options passed in this object define the characteristics of the manual input entry. Here are the possible options you can pass to the constructor:
Property
Type
Description
options.kpi
KPIDefinition
Optional. The KPI definition to create the manual input for. You can get KPI definitions by calling the getKPIs() method. To create the manual input, a KPI definition must be for an editable or an editable calculated compound main KPI. Provide either options.kpi or options.kpiCode.
options.kpiCode
String
Optional. The editable KPI code to create the manual input for. Provide either options.kpi or options.kpiCode.
options.period
PeriodDefinition
Required. The period definition to create the manual input for. You can get period definitions by calling the getPeriods() method.
options.tacticId
String
Optional. The Tactic ID the manual input applies to. Required if the KPI has the promotion tactic scope. Default value is null.
options.productId
String
Optional. The product ID the manual input applies to. It can be any level of the product hierarchy (Category, Brand, and more). If not set, the manual input references the KPI total value. Default value is null.
options.componentId
String
Optional. The product ID of the BOM Component part where the input is applied. This option is only used for KPIs whose scope include BOM Components. The default value is null.
options.value
Number
Optional. The numeric value for the KPI. The default value is null.
Example
1import { TPMManualInput } from 'cgcloud/tpmGenericUtils';23...45myMethod() {6 // Get TPM component relevant to the custom page7 const cmp = this.template.querySelector('cgcloud-...');89 // Get periods data10 const periods = await cmp.getPeriods();1112 // Create a new TPM manual input instance13 const mi = new TPMManualInput({14 kpiCode: 'ABCD',15 period: periods.find((p) => p.periodtype === 'Total'),16 tacticId: sampleTacticId,17 productId: sampleCategoryId,18 value: 1234519 });2021 // After creating the TPM Manual input you can add it to the existing22 // array and set it on the TPM component23 cmp.set...24}