Monday, 20 December 2021

LCS API to start and stop customer managed environments D365 F&O

Microsoft has released the LCS API to start and stop customer managed environments. Using these APIs will ensure the LCS environment status is synced with the actual environment.

Now you can start and stop environments through Microsoft Dynamics Lifecycle Services (LCS) via the LCS Environment API.

  • Only Customer-managed environments are supported. Self-service environments do not have the same concept of stop and start and are not supported by this API. Microsoft-managed environments are not supported.
  • These APIs will trigger/invoke the operation. A successful response only indicates that the trigger was successful.
  • For stop, non-success will be returned if the environment is already undergoing another operation or if the environment is already stopped.
  • For start, non-success will be returned if the environment is already undergoing another operation but will return success if the environment is already started.


Other useful LCS API's

Sunday, 10 October 2021

How to get last exception error message using x++ code

private InfologText getInfoLogMessage(int _infologPosition, Exception _logLevel, boolean _resetInfoLog)  

{

       #define.NewLine('\n')

    SysInfologEnumerator        enumerator = SysInfologEnumerator::newData(infolog.copy(max(_infologPosition,1), Global::infologLine()));

       SysInfologMessageStruct      msgStruct;

       Exception                           exception;

       InfologText                 infotext;

    while(enumerator.moveNext())

    {

           msgStruct = SysInfologMessageStruct::construct(enumerator.currentMessage());

           exception = enumerator.currentException();

 

           if(_logLevel == Exception::Info || _logLevel == exception

        || (_logLevel == Exception::Warning && exception == Exception::Error))

           {

                  if(infotext)

                  {

                         infotext += #NewLine;

                  }

               

                  infotext += strFmt('%1', msgStruct.message());

           }

    }

    if(_resetInfoLog)

    {

           infolog.clear(_infologPosition);

    }

}

Tuesday, 20 April 2021

How to use Excel Office Scripts in Power Automate

Office Scripts

Office Scripts in Excel is a new feature in Office365 that allows you to Automate your day-to-day tasks using power automate





  • You can record your Excel actions with the Action Recorder, which creates a script. 
  • You can also create and edit scripts with the Code Editor. Your scripts can then be shared across your organization so your coworkers can also automate their workflows.
  • TypeScript is a superset of JavaScript.
  • Every script runs the code in a single main method.
Connecting Office Scripts to Power Automate



Connectors are the bridges between Power Automate and applications. The Excel Online (Business) connector gives your flows access to Excel workbooks. The "Run script" action lets you call any Office Script accessible through the selected workbook. You can also give your scripts input parameters so data can be provided by the flow, or have your script return information for later steps in the flow.

The Excel Online (Business) connector has been a valuable part of the Power Automate ecosystem for some time now. It has a wide variety of applications

Prerequisites
In order to run Office Scripts via Power Automate, the following must be true:
  • Your organization must have enabled Office Scripts via the switch in the Office Admin Portal
  • Your organization must have enabled Power Automate and the Excel Online (Business) connector
Create an Office Script
  1. Go to the Automate tab and select All Scripts.
  2. Select New Script.
  3. Create script
  4. Rename the script to . Press the script name to change it.
  5. Save the script by pressing Save Script.
Example


Create an automated workflow with Power Automate

I have created a flow, to create separate files based on the excel header column.


Calling the created office script in power automate.

Please find below some handy scripts.

Format a Table

Below script used to format a table with excel data

function main(workbook: ExcelScript.Workbook, sheetName: string

  tableRowRange: workbook.getWorksheet(sheetName)   

  workbook.getWorksheet(sheetName).addTable(workbook.getWorksheet(sheetName).getRange('A1').getSurroundingRegion(), true);  

  workbook.setIsDirty(true

}


Get Column Names

Below script used to get excel header column details

function main(workbook: ExcelScript.Workbook, rangeAddress: string): string {

  let sh = workbook.getFirstWorksheet();

  let rg = sh.getRange(rangeAddress)

  return JSON.stringify(rg.getValues());

Format a table without a header columns

function main(workbook: ExcelScript.Workbook, sheetName: string, ) 

  tableRowRange: workbook.getWorksheet(sheetName)  

 workbook.getWorksheet(sheetName).addTable(workbook.getWorksheet(sheetName).getRange('A1:AY70'), false); 

// 'A1:AY70' refers to columnrow

  workbook.setIsDirty(true) 

}

Delete empty row 

function main(workbook: ExcelScript.Workbook) {
  let selectedSheet = workbook.getActiveWorksheet();
  let table = selectedSheet.getTables()[0];
  let tableDataRange = table.getRangeBetweenHeaderAndTotal();
  let dataValues = tableDataRange.getValues();

  let removed = 0;
  // Important that you go from bottom to top to account for removing correct rows. 
  for (let i = dataValues.length-1; i >=0 ; i--) {
    let [event, date] = dataValues[i];
    if (event === '' || date === '') {
      // tableDataRange.getRow(i).getFormat().getFill().setColor('Yellow');      
      table.deleteRowsAt(i);
      removed++;
    }
  }
  console.log(`Removed ${removed} rows.`)
}