DSCPUtil Utill d365fo x++

DSCPUtil Utill d365fo x++

Code:

using System.Convert;
using System.Text;

internal final class DSCPUtil
{
    

    // BP Deviation Documented
    public static LogisticsAddressCountryRegionName countryRegionName(LogisticsAddressCountryRegionId _countryRegionId)
    {
        LogisticsAddressCountryRegionName shortNameText = LogisticsAddressCountryRegionTranslation::find(_countryRegionId).ShortName;
        str dereferencedLabelId = SysLabel::labelId2String(shortNameText, currentUserLanguage());
        return dereferencedLabelId? dereferencedLabelId : shortNameText;
    }

    public static LogisticsAddressCountryRegionId findByShortName(
        LogisticsAddressCountryRegionShortName  _countryRegion,
        LanguageIdAll                           _languageId = currentUserLanguage())
    {
        LogisticsAddressCountryRegionTranslation    countryRegionTranslation;

        if (_countryRegion)
        {
            select firstonly countryRegionTranslation
                where countryRegionTranslation.ShortName == _countryRegion &&
                      countryRegionTranslation.LanguageId == _languageId;
        }
        return countryRegionTranslation.CountryRegionId;
    }

    public static LogisticsAddressCountryRegionId findByLongName(
        LogisticsAddressCountryRegionLongName   _countryRegion,
        LanguageIdAll                           _languageId = currentUserLanguage(),
        boolean                                 _forUpdate = false)
    {
        LogisticsAddressCountryRegionTranslation    countryRegionTranslation;

        if (_countryRegion)
        {
            countryRegionTranslation.selectForUpdate(_forUpdate);

            select firstonly countryRegionTranslation
                where countryRegionTranslation.LongName == _countryRegion &&
                      countryRegionTranslation.LanguageId == _languageId;
        }

        return countryRegionTranslation.CountryRegionId;
    }

    public static HcmIdentificationType findByIdentificationType(
        HcmIdentificationTypeId _identificationTypeID,
        boolean _forUpdate = false,
        ConcurrencyModel _concurrencyModel = ConcurrencyModel::Auto)
    {
        HcmIdentificationType identificationType = null;
        identificationType.selectForUpdate(_forUpdate);

        if (_forUpdate  && _concurrencyModel != ConcurrencyModel::Auto)
        {
            identificationType.concurrencyModel(_concurrencyModel);
        }

        select firstonly identificationType
            index hint IdentificationTypeIdx
            where identificationType.IdentificationTypeId == _identificationTypeID;

        return identificationType;
    }

    public static str getInfoLog()
    {
        str error;
        Exception exception;
        SysInfologEnumerator enumerator;
        SysInfologMessageStruct msgStruct;

        enumerator = SysInfologEnumerator::newData(infolog.cut());

        while (enumerator.moveNext())
        {
            msgStruct = new SysInfologMessageStruct(enumerator.currentMessage());
            exception = enumerator.currentException();
            error = strfmt("%1 %2", error, msgStruct.message());
        }

        return error;
    }

    public static LogisticsPostalAddress findByLocation(LogisticsLocationRecId _recId,  boolean _update = false )
    {
        LogisticsPostalAddress logisticsAddress;

        if (_recId)
        {
            logisticsAddress.selectForUpdate(_update);

            select firstonly logisticsAddress
                where logisticsAddress.Location == _recId;
        }

        return logisticsAddress;
    }

    public static LogisticsLocation findByLocationRecId(LogisticsLocationRecId _recId,  boolean _update = false )
    {
        LogisticsLocation logisticsLocation;

        if (_recId)
        {
            logisticsLocation.selectForUpdate(_update);

            select firstonly logisticsLocation
                where logisticsLocation.RecId == _recId;
        }

        return logisticsLocation;
    }

    public static int totalDependentsByPerson(RecId _personRecid)
    {
        HcmDependentRelationship hcmDependentRelationship;
        HcmPersonalContactRelationship hcmPersonalContactRelationship;
        DirPartyRelationship    dirPartyRelationship;

        select count(RecId) from hcmDependentRelationship
            join hcmPersonalContactRelationship
            join dirPartyRelationship
            where hcmDependentRelationship.PersonalContactRelationship == hcmPersonalContactRelationship.RecId
            && dirPartyRelationship.RecId == hcmPersonalContactRelationship.PartyRelationship
            && dirPartyRelationship.ParentParty == _personRecid;

        return hcmDependentRelationship.RecId;

    }

    public static container getAttachments(RefRecId _recId, TableId _tableId)
    {

        DocuRef   docuRef;
        container attchments;

        select firstonly1  docuRef
            where docuRef.RefRecId == _recId
            && docuRef.RefTableId == _tableId;

        if(docuRef)
            attchments = DocumentManagement::getAttachmentAsContainer(docuref);

        return attchments;

    }

    //public boolean createAttachment(TableId _tableId, RecId _recId, DocuTypeId _type, str _base64String, str _extension, str _onlyFileName)
    //{
    //    System.IO.MemoryStream     fileStream;
    //    HcmPersonRecId      author;
    //    DocuRef             docuRef;
    //    System.Exception    exception;
    //    Filename            fileNameWithExtension;
    //    System.IO.MemoryStream stream;
    //    boolean             ret;
    //    ;

    //    try
    //    {
    //        if(_base64String)
    //        {
    //            fileStream  = Binary::constructFromContainer(BinData::loadFromBase64(_base64String)).getMemoryStream() as System.IO.MemoryStream;//stream;
    //            fileStream.Seek(0, System.IO.SeekOrigin::Begin);
    //            File::SendFileToUser(fileStream,'BusinessProcess');
    //            //author      = HcmWorkerLookup::currentPerson();
    //            //fileNameWithExtension    = strFmt("%1%2", _onlyFileName, _extension);
    //            ////str uniqueFileName = storageProvider.GenerateUniqueName(_Filename);
    //            //docuRef = DocumentManagement::attachFile(_tableId, _recId, curext(), _type, fileStream, fileNameWithExtension, _extension, _onlyFileName, '');
    //            ////if(Docu::validateExtension(fileExtension))
    //            //if(docuRef)
    //            //{
    //            //    DocuRef = DocuRef::findRecId(DocuRef.RecId, true);

    //            //    ttsbegin;
    //            //    DocuRef.Author = author;
    //            //    DocuRef.update();
    //            //    ret = true;
    //            //    ttscommit;
    //            //}
    //        }
    //    }
    //    catch(exception)
    //    {
    //        throw error(DSCPUtil::getInfoLog());
    //    }

    //    return ret;
    //}
    [AifCollectionTypeAttribute("DSCPAttachmentContract", Types::Class)]
    public boolean createAttachment(TableId _tableId, RecId _recId ,DSCPAttachmentContract contract)
    {
        System.IO.Stream    fileStream;
        System.Byte[]       byteArray;
        UTF8Encoding        utf8Encoding;
        HcmPersonRecId      author;
        DocuRef             docuRef;
        System.Exception    exception;
        Filename            fileName;
        System.IO.MemoryStream stream;
        boolean             ret;
        ;

        try
        {
            if(contract.parmStringData())
            {
                utf8Encoding= new UTF8Encoding();
                byteArray   = utf8Encoding.GetBytes(contract.parmStringData());
                stream      = new System.IO.MemoryStream(byteArray);
                fileStream  = Binary::constructFromContainer(BinData::loadFromBase64(contract.parmStringData())).getMemoryStream();//stream;
                author      = HcmWorkerLookup::currentPerson();
                fileName    = strFmt("%1.%2", contract.parmName(), contract.parmExtension());

                // <Abdul Ghani 10-02-2022>
                // Start
                // This method is creating issue while selection record in FnO as record company match with current company in case of shared tables
                //docuRef = DocumentManagement::attachFile(
                //    _tableId,
                //    _recId,
                //    curext(),
                //    DocuType::typeFile(),
                //    fileStream,
                //    fileName,
                //    contract.parmExtension(),
                //    contract.parmName(),
                //    contract.parmNotes());

                Common record = this.findRecord(_tableId, _recId);
                docuRef = DocumentManagement::attachFileForRecord(record, DocuType::typeFile(), fileStream, fileName, contract.parmName(), contract.parmNotes());
                // <End>

                if(docuRef)
                {
                    DocuRef = DocuRef::findRecId(DocuRef.RecId, true);

                    ttsbegin;
                    DocuRef.Author = author;
                    DocuRef.update();
                    ret = true;
                    ttscommit;
                }
            }
        }
        catch(exception)
        {
        }

        return ret;
    }

    public Common findRecord(TableId _tableId, RecId _recId, Boolean _forUpdate = false)
    {
        Common      common;
        DictTable   dictTable;
        ;
        dictTable = new DictTable(_tableId);
        common = dictTable.makeRecord();
 
        common.selectForUpdate(_forUpdate);
 
        select common where common.RecId == _recId;
 
        return common;
    }

    public str getDocumentBase64String(DocuRef docuRef)
    {
        str base64String;
        System.IO.MemoryStream      memoryStream;
        ;

        if(docuRef)
        {
            memoryStream = new System.IO.MemoryStream();
            DocumentManagement::getAttachmentStream(docuRef).CopyTo(memoryStream);
            base64String = System.Convert::ToBase64String(memoryStream.ToArray());
        }
        
        return base64String;
    }

}

Comments

Popular posts from this blog

D365FO – AX – X++ –Refresh, Reread, Research, and ExecuteQuery

Create Inventory Journal through Code in D365FO X++

SalesLine Reservation in D365fo x++