Tuesday, March 20, 2018

X++ code to run the report through code and send mail with attachments in D365


SRSPrintDestinationSettings     printerSettings;
Array                           arrayFiles;
System.Byte[]                   reportBytes = new System.Byte[0]();
SRSProxy                        srsProxy;
SRSReportRunService             srsReportRunService = new SrsReportRunService();
Microsoft.Dynamics.AX.Framework.Reporting.Shared.ReportingService.ParameterValue[]  parameterValueArray;
Map                             reportParametersMap;
SRSReportExecutionInfo          executionInfo = new SRSReportExecutionInfo();


filename = strfmt('%1%2',SSSSSS,'.pdf');
System.IO.Stream workbookStream = new System.IO.MemoryStream();
SysMailerSMTP           mailer = new SysMailerSMTP();
SysMailerMessageBuilder builder = new SysMailerMessageBuilder();
SysEmailParameters      parameters = SysEmailParameters::find();
SrsReportRunController  ssrsController = new SrsReportRunController();
XXXXXXXContract    Contract = new XXXXXXXContract();// define contract class

ssrsController.parmReportName(ssrsReportStr(ReportName, Design));
ssrsController.parmShowDialog(false);
Contract.parmRecordId(YYYY.RecId);//Record recid
ssrsController.parmReportContract().parmRdpContract(Contract);
printerSettings = ssrsController.parmReportContract().parmPrintSettings();
printerSettings.printMediumType(SRSPrintMediumType::File);
printerSettings.fileFormat(SRSReportFileFormat::PDF);
printerSettings.overwriteFile(true);
printerSettings.fileName(filename);
                 
ssrsController.parmReportContract().parmReportServerConfig(SRSConfiguration::getDefaultServerConfiguration());
ssrsController.parmReportContract().parmReportExecutionInfo(executionInfo);
srsReportRunService.getReportDataContract(ssrsController.parmreportcontract().parmReportName());
srsReportRunService.preRunReport(ssrsController.parmreportcontract());
reportParametersMap = srsReportRunService.createParamMapFromContract(ssrsController.parmReportContract());
parameterValueArray = SrsReportRunUtil::getParameterValueArray(reportParametersMap);

srsProxy = SRSProxy::constructWithConfiguration(ssrsController.parmReportContract().parmReportServerConfig());
// Actual rendering to byte array
reportBytes = srsproxy.renderReportToByteArray(ssrsController.parmreportcontract().parmreportpath(),
                                                                      parameterValueArray,
                                                                      printerSettings.fileFormat(),
                                                                      printerSettings.deviceinfo());

builder.setFrom(SysEmailParameters::find().SMTPUserName);
builder.addTo(AAAAAA);
builder.setSubject(strfmt(bbbbbbbb));
builder.setBody(content);
if (reportBytes)
{
     System.IO.Stream            stream         = new System.IO.MemoryStream(reportBytes);
     builder.addAttachment(stream,filename);
}
SysMailerFactory::getNonInteractiveMailer().sendNonInteractive(builder.getMessage());

X++ code to get the individual dimension values from default dimension in D365


public static DimensionDefault  getDefaultDimBranchValues(DimensionValue "",DimensionValue "",DimensionValue "",DimensionValue    "")
{
        DimensionDefault            defaultDimension,result;
        container                   conAttr,conValue;
        str                         dimValue;
        int             counttrans,j;
        DimensionAttribute          dimensionAttribute;
        DimensionAttributeValue     dimensionAttributeValue;
        DimensionAttributeValueSetStorage valueSetStorage        = new DimensionAttributeValueSetStorage();
        conAttr  = ["DimensionName","DimensionName","DimensionName","DimensionName"];
        conValue = [DimensionValue,DimensionValue,DimensionValue,_reDimensionValueorting,DimensionValue];

        for (j = 1; j <= conLen(conAttr); j++)
        {
            dimensionAttribute = dimensionAttribute::findByName(conPeek(conAttr,j));
      
            if (dimensionAttribute.RecId == 0)
            {
                continue;
            }
      
            dimValue = conPeek(conValue,j);
      
            if (dimValue != "")
            {
                dimensionAttributeValue =dimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,dimValue,false,true);
                valueSetStorage.addItem(dimensionAttributeValue);
            }
        }
        result = valueSetStorage.save();
        return result;
}

X++ code to upload/Save a file in Azure location in D365


using Microsoft.Dynamics.ApplicationPlatform.Services.Instrumentation;
using Microsoft.DynamicsOnline.Infrastructure.Components.SharedServiceUnitStorage;
using Microsoft.Dynamics.AX.Framework.FileManagement;
public class AzureStorage
{
str docfiletype;
    Microsoft.Dynamics.AX.Framework.FileManagement.IDocumentStorageProvider storageProvider; 
    public void uploadfile(Filename _Filename,Filename _filePath)
    {
        guid fileGuid = newGuid();
        str fileId;
        str downloadUrl;
        System.IO.Stream    _stream;
        str fileNameAZ = strFmt('%1/%2', fileId, _fileName);//filename = Filename.Xlsx
        fileId = guid2str(fileGuid);
        _stream = File::UseFileFromURL(_filePath);//filepath = C://Temp//Filename.Xlsx
        var blobInfo = new SharedServiceUnitStorageData();
        blobInfo.Id = fileId;
        blobInfo.Category = "StorageFolder";//Folder name
        blobInfo.Name = fileNameAZ;
        blobInfo.Accessibility = Accessibility::Private;
        blobInfo.Retention = Retention::Permanent;       
        if (_stream.CanSeek)
        {
            _stream.Seek(0, System.IO.SeekOrigin::Begin);
        }
        var blobStorageService = new SharedServiceUnitStorage(SharedServiceUnitStorage::GetDefaultStorageContext());
        blobStorageService.UploadData(blobInfo, _stream);
        var uploadedBlobInfo = blobStorageService.GetData(fileId, "StorageFolder", BlobUrlPermission::Read, System.TimeSpan::FromDays(30));
//Time span for keeping the file at azure location.
        downloadUrl =uploadedBlobInfo.BlobLink;
    }
}

X++ code to create attachment of a file in D365


using Microsoft.Dynamics.ApplicationPlatform.Services.Instrumentation;
using Microsoft.DynamicsOnline.Infrastructure.Components.SharedServiceUnitStorage;
using Microsoft.Dynamics.AX.Framework.FileManagement;
public class AttachFile
{
str docfiletype;
Microsoft.Dynamics.AX.Framework.FileManagement.IDocumentStorageProvider storageProvider;
    public  boolean createAttachment(TableId tableId, RefRecId _refFieldId,Filename _Filename,Filename _filePath)
    {
        boolean                     ret = false;
        DocuRef     docuref;
        str downloadUrl;
        System.IO.Stream    _stream;
        _stream = File::UseFileFromURL(_filePath);
        str _contentType = System.Web.MimeMapping::GetMimeMapping(_filePath);
        DocuType fileType = DocuType::find(DocuType::typeFile());
        storageProvider = Docu::GetStorageProvider(fileType, true, curUserId());

        if(storageProvider)
        {
            str uniqueFileName = storageProvider.GenerateUniqueName(_Filename);
            str fileNameWithoutExtension = System.IO.Path::GetFileNameWithoutExtension(_filePath);
            str fileExtension = Docu::GetFileExtension(uniqueFileName);
  
            if(Docu::validateExtension(fileExtension))
            {
                guid FileId = newGuid();
                DocuValue docValue;
                docValue.Name = fileNameWithoutExtension;
                docValue.FileId = FileId;
                docValue.FileName = uniqueFileName;
                docValue.FileType = fileExtension;
                docValue.OriginalFileName = _Filename;
                docValue.Type = DocuValueType::Others;
                docValue.StorageProviderId = storageProvider.ProviderId;
                DocumentLocation location = storageProvider.SaveFile(docValue.FileId, uniqueFileName, _contentType, _stream);
                
                if (location != null)
                {
                    if(location.NavigationUri)
                    {
                        docValue.Path = location.get_NavigationUri().ToString();
                    }

                    if(location.AccessUri)
                    {
                        docValue.AccessInformation = location.get_AccessUri().ToString();
                        //info(docValue.AccessInformation);
                    }

                    if (docValue.validateWrite())
                    {
                       
                        docValue.insert();
                        DocuUploadResult DocuUploadResult =  new DocuUploadResult(_fileName, _contentType, false, "", newGuid());
                        DocuUploadResult.fileId(FileId);
                        docuref = DocuUploadResult.createDocuRef(tableId,_refFieldId,DocuType::typeFile());
                        if(docuref)
                        {
                            ret =  true;
                        }
                        else
                        {
                            ret =  false;
                        }


                    }
                }
            }

        }

X++ code to read the attached file in D365


using Microsoft.Dynamics.ApplicationPlatform.Services.Instrumentation;
using Microsoft.DynamicsOnline.Infrastructure.Components.SharedServiceUnitStorage;
using Microsoft.Dynamics.AX.Framework.FileManagement;
public class ReadAzureBlobStorage
{
    str docfiletype;
    Microsoft.Dynamics.AX.Framework.FileManagement.IDocumentStorageProvider storageProvider;
    Public void readfromAzureBlob(DocuRef _docuRef)
    {
        AsciiStreamIo file;
        container record;
        str downloadUrl;
        if (_docuRef.isValueAttached())
        {
            var docuValueloc = _docuRef.docuValue();
            downloadUrl = docuValueloc.Path;

            if (!downloadUrl || docuValueloc.Type == DocuValueType::Others)
            {
                str accessToken = DocumentManagement::createAccessToken(_docuRef);
                downloadUrl = Microsoft.Dynamics.AX.Framework.FileManagement.URLBuilderUtilities::GetDownloadUrl(docuValueloc.FileId, accessToken);
            }
            var docContents = storageProvider.GetFile(docuValueloc.createLocation());
            file = AsciiStreamIo::constructForRead(docContents.Content);//File::UseFileFromURL(downloadUrl));
         
        }
       
        if (file)
        {
            if (file.status())
            {
                throw error("@SYS52680");
            }
            file.inFieldDelimiter(',');
            file.inRecordDelimiter('\r\n');
           
        }
      
        while (!file.status())
        {
            record = file.read();
          
            if (conLen(record))
            {
                info(strFmt("%1 - %2",conPeek(record,1),conPeek(record,2)));
            }
        }

    }
}

Monday, March 19, 2018

X++ code to send mail code in D365

System.IO.Stream workbookStream = new System.IO.MemoryStream(); 
SysMailerSMTP   mailer = new SysMailerSMTP();
SysMailerMessageBuilder builder = new SysMailerMessageBuilder();
SysEmailParameters parameters = SysEmailParameters::find();
FileIOPermission fileIOPermission;
InteropPermission interopPerm;
Notes content;          


Filepath = c:/temp/"filename"
System.IO.Stream filestream = File::UseFileFromURL(Filepath );
   
builder.setFrom(SysEmailParameters::find().SMTPUserName);
builder.addTo("Test@test.com");
builder.addAttachment(filestream,"Bookings.csv");
builder.setSubject("Ax_Daily Shipment report");
builder.setbody(content );
content ="";
content = content + strfmt("\n<p dir=ltr align=left>Hello, </p> ");
content = content + strfmt("\n<p dir=ltr align=left>XXXXXXX : %1 </p> ","");
content = content + strfmt("\n<p dir=ltr align=left>YYYYYY: %1 </p> ", "");
content =  content + strfmt('\n<p dir=ltr align=left></p>');
content =  content + strfmt('\n<p dir=ltr align=left></p>');
// builder.setBody("Please take a look at the attachment having shipping date changed");
SysMailerFactory::getNonInteractiveMailer().sendNonInteractive(builder.getMessage());

Export to csv file in D365

fileUploadTemporaryStorageResult result;
AsciiStreamIo filecreate;
System.IO.StreamWriter StreamWriter;
Filename    localfilename , pathname;
System.IO.Stream stream;
TransDate           fromDate;
TransDate           toDate;
FilePath        pathfile;
FilenameSave path;
CommaStreamIo io;
Container Con;
FileIOPermission fileIOPermission;
InteropPermission interopPerm;
str bookingdetails;
;
super();
interopPerm = new InteropPermission(InteropKind::ClrInterop);
interopPerm.assert();
path = "Folder path" +"xxxx.csv";
fileIOPermission = new FileIOPermission(path, "r");
fileIOPermission.assert();
filecreate = AsciiStreamIo::constructForWrite();
con = conins(con,1,"SO Date");
con = conins(con,2,"Order Type");
con = conins(con,3,"Order Status");
con = conins(con,4,"SO Number");
con = conins(con,5,"Bill To");
bookingdetails = con2Str(con,",");
filecreate.write(bookingdetails);
filecreate.Dispose();
CodeAccessPermission::revertAssert();
this.Sendmail();
System.IO.File::Delete(path);

Export to Excel in D365

#AviFiles
SysOperationProgress progress1 = new SysOperationProgress();
int             row=1;
int             startrow;
int             col;
COM                 Crange;
COM                 CBorders;
COM                 CBorder;
;
DocuFileSaveResult saveResult = DocuFileSave::promptForSaveLocation("@ApplicationPlatform:OfficeDefaultWorkbookFileName", "xlsx", null, this.caption());
System.IO.Stream workbookStream = new System.IO.MemoryStream();  
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
saveResult.parmOpenParameters('web=1');
saveResult.parmOpenInNewWindow(false);
using (var package = new OfficeOpenXml.ExcelPackage(memoryStream))
{
     var sheet = package.get_Workbook().get_Worksheets();
     var worksheet = sheet.Add("First sheet");
     var cells = worksheet.get_Cells();
     OfficeOpenXml.Style.ExcelStyle style = cells.get_Item(row,1).get_style();
     OfficeOpenXml.Style.ExcelFont font = style.Font;
     OfficeOpenXml.Style.ExcelColor color = font.Color;
 cell = Cells.get_item(row,2);cell.set_Value("Entry Date");
            cellStyle = Cells.get_item(row, 2).get_style();
            style = cells.style;font  = style.font;color = font.color;
            color.SetColor(System.Drawing.Color::FromArgb(190,0,0));
     font.Size = 20;
     row++;
     Col = 1;
     cell = Cells.get_item(row,col);cell.set_Value(date2str(salesline.dlvdate,213,2,4,2,4,2));//j:10 line 1
     col++;
     package.save();
}
memoryStream.Seek(0, System.IO.SeekOrigin::Begin);
DocuFileSave::processSaveResult(memoryStream, saveResult);

Friday, March 16, 2018

Delete a model in Dynamics 365



1.    Model belongs to its own package 
(For example: An extension package with no other models in the package):
a.      Stop the following services: The AOS web service and the Batch Management Service
b.     Delete the package folder C:\AOSService\PackagesLocalDirectory\<your model>
c.      Restart the services from step 1
d.     If Visual Studio is running, refresh your models (Visual Studio > Dynamics 365 > Model management > Refresh models)
e.      In Visual Studio, perform a full database synchronization (Visual Studio > Dynamics 365 > Synchronize database...)

2.    Model belongs to a package with multiple models 
(For example, <your model> overlays Application Suite):
a.      Stop the following services: The AOS web service and the Batch Management Service
b.     Delete the model folder C:\AOSService\PackagesLocalDirectory<PackageName>\<your model> (In this example PackageName=ApplicationSuite)
c.      Restart the services from step 1
d.     In Visual Studio, refresh your models (Visual Studio > Dynamics 365 > Model management > Refresh models)
e.      In Visual Studio, build the package that the deleted models belonged to (Visual Studio > Dynamics 365 > Build models...)
f.       In Visual Studio, perform a full database synchronization (Visual Studio > Dynamics 365 > Synchronize database...)