Sunday, June 16, 2024

way to remove special characters from a string using X++

static void RemoveAllSpecialChararcters(Args _args)
{
	str sometext = "ABC#DE%F_$#G@1&23";

	str x = System.Text.RegularExpressions.Regex::Replace(sometext, @"[#,_,$,%,@,&]”, """);
	info(x);
}
static void Dev_ReplaceTxt(Args _args)
{
	TextBuffer buffer = new TextBuffer();
	Str message;
	;

	message = " hi hello's how r u's ";
	message += " How r u doing's wujer's * ? ' what ur mot's anbej's";

	buffer.setText(message);
	buffer.replace("[*?']","\\'"); // replace special character with escape sequence
	info(buffer.getText());

}

strRem('String','*');

If you want to delete the special characters from string, you can use "strAlpha" function. This copies only the alphanumeric characters from a string.

Ex : info(strFmt("%1", strAlpha("?a*b!!!!cD123.")));

results in "abcD123".

Friday, April 26, 2024

create DMF import data project dynamically and map using XML

internal final class DMFImport
{
    /// <summary>
    /// Class entry point. The system will call this method when a designated menu 
    /// is selected or when execution starts and this class is set as the startup class.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {
        str fileExtWithoutDot;
        str contentType;
        str fileId;
        guid fileGuid = newGuid();
        DMFDefinitionGroup  definitionGroup;
        DMFDefinitionGroupEntity    definitionGroupEntityTable;
        
        //Creates DMF project
        str _defintionGroupName = "Test8";
        ttsbegin;
        definitionGroup.initValue();
        definitionGroup.DefinitionGroupName = _defintionGroupName;
        definitionGroup.OperationType = DMFOperationType::Import;
        definitionGroup.insert();
        ttscommit;
        
        fileExtWithoutDot = DMFDataSource::getFormatFileExtension('XML-Element');
        contentType     = strFmt('application/%1', fileExtWithoutDot);

        DMFEntity DMFEntity;// = DMFEntity::find("Customer groups");
        select firstonly dmfEntity
            order by dmfEntity.EntityName asc
                where dmfEntity.targetEntity == "CUSTCUSTOMERGROUPENTITY";;

       // str _entityName = "CUSTCUSTOMERGROUPENTITY";
        str _entityName = DMFEntity.EntityName;
        
        str _fileName = "Test8";

        fileId = guid2str(fileGuid);
        str _fileBase64 = "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48RG9jdW1lbnQ+PENVU1RDVVNUT01FUkdST1VQRU5USVRZPjxDTEVBUklOR1BFUklPRFBBWU1FTlRURVJNTkFNRT48L0NMRUFSSU5HUEVSSU9EUEFZTUVOVFRFUk1OQU1FPjxDVVNUT01FUkFDQ09VTlROVU1CRVJTRVFVRU5DRT48L0NVU1RPTUVSQUNDT1VOVE5VTUJFUlNFUVVFTkNFPjxDVVNUT01FUkdST1VQSUQ+RXhEZWJCUnVwdDwvQ1VTVE9NRVJHUk9VUElEPjxERUZBVUxURElNRU5TSU9ORElTUExBWVZBTFVFPjwvREVGQVVMVERJTUVOU0lPTkRJU1BMQVlWQUxVRT48REVTQ1JJUFRJT04+QWNjb3VudHMgcmVjZWl2YWJsZSBleHRlcm5hbCAtIGJhZCBkZWJ0PC9ERVNDUklQVElPTj48SVNTQUxFU1RBWElOQ0xVREVESU5QUklDRT5ObzwvSVNTQUxFU1RBWElOQ0xVREVESU5QUklDRT48UEFZTUVOVFRFUk1JRD48L1BBWU1FTlRURVJNSUQ+PFRBWEdST1VQSUQ+PC9UQVhHUk9VUElEPjxXUklURU9GRlJFQVNPTj48L1dSSVRFT0ZGUkVBU09OPjwvQ1VTVENVU1RPTUVSR1JPVVBFTlRJVFk+PC9Eb2N1bWVudD4=";
       
        //Creates a file from Base64
        System.Byte[] reportBytes = System.Convert::FromBase64String(_fileBase64);
        System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(reportBytes);

        FileUploadTemporaryStorageStrategy fileUploadStrategy = new FileUploadTemporaryStorageStrategy();
        FileUploadTemporaryStorageResult fileUploadResult = fileUploadStrategy.uploadFile(memoryStream, _fileName,contentType);

        //Adds the entity and file to the DMF Project
        ttsbegin;
        definitionGroupEntityTable.initValue();
        definitionGroupEntityTable.DefinitionGroup = _defintionGroupName;
        definitionGroupEntityTable.Entity = _entityName;
        definitionGroupEntityTable.Source = 'XML-Element';
        definitionGroupEntityTable.SampleFilePath = fileUploadResult.getFileId();
        definitionGroupEntityTable.EntityXMLName = DMFEntity.TargetEntity;

        definitionGroupEntityTable.RunInsertLogic = NoYes::Yes;
        definitionGroupEntityTable.RunValidateWrite = NoYes::Yes;
        DIS_DMFImport::GenerateAndValidateMapping(definitionGroupEntityTable);
        definitionGroupEntityTable.insert();
        ttscommit;

        DMFExecutionId  executionId = DMFUtil::setupNewExecution(_defintionGroupName);
        DMFDefinitionGroupExecution execution = DMFDefinitionGroupExecution::find(_defintionGroupName, definitionGroupEntityTable.Entity,executionId, true);

        //execution.DataProjectId = journalId;
        execution.FilePath = fileId;
        execution.update();

        //DMFQuickImportExport::doPGImport(_defintionGroupName, executionId);
    }

    /// <summary>
    /// Generates mapping for a definition group entity.
    /// </summary>
    /// <param name = "definitionGroupEntity">The definition group entity to generate mapping for.</param>
    public static void GenerateAndValidateMapping(DMFDefinitionGroupEntity definitionGroupEntity)
    {
        // Generate the mapping
        DMFXmlGeneration::generateMappingV2(definitionGroupEntity, '', true, true, false);

        // Validate generated mapping
        DMFQuickImportExport::validateXML(definitionGroupEntity.Source,
                definitionGroupEntity.SampleFilePath,
                definitionGroupEntity.Entity,
                definitionGroupEntity);
    }

}

Table browser URL in D365FO

Critical Thinking icon icon by Icons8