In AL VS Code Extensions V2, if you try to create an extension and the ID or Name are already used, you get this error:
Because we donβt have the extension id in the name of the file (I assume you don’t have it either), you can use the following Powershell script to check what files are using the same name for example (change according to your need):
$Folder = 'your folder\with extensions' $SearchString = 'pageextension50545' #your search string $File = (Get-ChildItem $Folder -Filter '*.al' | Where-Object {Select-String $SearchString $_}).Name $File
This code will search for the string in the first line of each file and show you what files contain the same Id/name:
! Ok, this was the easy part. Now I want to assign a different Id (the next free Id) for my current file, in order to fix the error. But which is the next ID ? In good old C/SIDE it was so easy to find out things like this…
In order to find out the next free Id in AL, I need to look in each file and compare all the Ids to see which one is the greatest. The problem is that by now I have 200-300 files, so it will probably take around one week to do this manually…
Not when we have Powershell π
First let’s see the situation from my example (you can then adapt the script for your own situation):
–> I have 1 folder with 2 sub folders with AL Page Extension files:
* Files in folder 1 are named like this:
* Files in folder 2 are named like this:
* The first line of each file contains information about the page extension like this:
pageextension 50333 pageextension50333 extends “Bank Account Card”
So, let’s look automatically in each file at the first line, create an array with the IDs, Object Name that it extends and other useful information about the files:
Function Get-ALExtensionFilesInfo{ [CmdletBinding()] param( [Parameter(Mandatory=$true)][array]$ALExtensionFiles, [switch]$GetIDFromExtensionName, [switch]$GetObjectNameFromFileName ) If($GetIDFromExtensionName) { #let's complicate things for practice with regex and extract the ID from the extension name instead of simply take the Id [regex]$Regex = '[a-zA-Z]+(?<num>\d+)' } else { [regex]$Regex = '(?<num>\b\d+)' } $ExtensionNumbers = @() foreach($File in $ALExtensionFiles) { $ExtHash = @{} $stringfirstline = (Select-String -InputObject $file -Pattern $RegEx | select-object -First 1).Line $m = $Regex.Matches($stringfirstline) $number = ($m[0].Groups["num"].Value) * 1 $ExtHash.Add("FileName",$File.FullName) If(!$GetObjectNameFromFileName) { #match the object name from the first line (after word 'extends'): $ExtHash.Add("ObjectName",(([regex]::Match($stringfirstline,'(?<=extends ).*').Value) -replace '"','').TrimEnd()) } else { #OR match the object name from file name: ex Name of file is 'PEX50343 - Test.al' or 'PEX - Test.al'; Object Name is 'Test.al' $ExtHash.Add("ObjectName",[regex]::Match($File.Name,'(?<=PEX(?:\d+)? - ).*').Value) } $ExtHash.Add("ExtensionNumber",$number) $ExtHash.Add("ExtensionType",[regex]::Match($stringfirstline,'^\b\w+')) $PsObject = New-Object PSObject -property $ExtHash $ExtensionNumbers += $PsObject } return $ExtensionNumbers }
Let’s see how to use this function:
$Folder = 'C:\ALExtensionIdsSample\Folder 1' $Folder2 = 'C:\ALExtensionIdsSample\Folder 2' $Files = Get-ChildItem $Folder -Filter '*.al' $Files += Get-ChildItem $Folder2 -Filter '*.al' #get the info from extension files $ExtensionInfo = Get-ALExtensionFilesInfo -ALExtensionFiles $Files $ExtensionInfo
–> And now we have an array with custom objects that we can use:
For example, to get the last used extension ID for page extensions:
#get the maximum used id for page extensions $MaxMinNo = $ExtensionInfo | Where-Object{$_.ExtensionType -like 'pageextension'} | Measure-Object -Property ExtensionNumber -Maximum -Minimum $ExtensionInfo | Where-Object {($_.ExtensionNumber -eq $MaxMinNo.Maximum) -and ($_.ExtensionType -like 'pageextension')}
Or I know I have a page Extension for page “Tranfer Order” and I want to retrieve the Id:
$ExtensionInfo | Where-Object{($_.ObjectName -like 'Transfer Order') -and ($_.ExtensionType -like 'pageextension')}
–> Or the opposite example when I have the Extension Id and I need to find out the Name of the Object that it extends:
$ExtensionInfo | Where-Object{($_.ExtensionNumber -eq 50292) -and ($_.ExtensionType -like 'pageextension')} | Select-Object -Property ObjectName
For this script I used some simple powershell commands and a little bit of Regex. This is not a final product, but more a script made quickly when I was in need π
What do you say, would it be helpful a Powerhell module with functions for this kind of stuff ? Or maybe we should just wait for Microsoft to provide a better way of dealing with Extension Ids (or maybe they plan to not use Ids in the future) ?
You can find these scripts also on my GitHub:
https://github.com/andreilungu/Utilities/tree/master/Powershell
Filename: 001_GetALExtensionIDandObjectName.ps1
*Later Edit:
In the meantime I created a small Powershell module to help you get information about AL Extension Files. You can read more in this post:
https://andreilungu.com/nav-al-extension-files-info/
It’s now been five years since your post, so I guess it’s fair to say MS won’t do away with these IDs. I wonder what was the rationale to introduce them in the first place. Anyway, thanks for your script.