Friday, October 1, 2021

upload Image File to D365FO using X++

 I got a requirement like I have to add signature image to company and I have to populate in my custom report.

Steps:

1.Added "container field" in table.

2.Added New fast tab, buttons and Image control in form as shown below.



3. Written the code in below class. By Event handler methods I written the code.

    public class OMLegalEntityEventHandler { // Change button clicked method { FormDataSource companyInfo_ds = sender.formRun().dataSource(1) as FormDataSource; CompanyInfo companyInfo = companyInfo_ds.cursor() as CompanyInfo; str imageFilePathName; ; imageFilePathName = OMLegalEntityEventHandler::uploadImageFile(); if (imageFilePathName) { ttsbegin; companyInfo.selectForUpdate(true); companyInfo.CompanySignature = ImageReference::GetPackedBinaryData(imageFilePathName); companyInfo.update(); ttscommit; } companyInfo_ds.research(true); } //Remove button clicked method { FormDataSource companyInfo_ds = sender.formRun().dataSource(1) as FormDataSource; CompanyInfo companyInfo = companyInfo_ds.cursor() as CompanyInfo; ; ttsbegin; companyInfo.selectForUpdate(true); companyInfo.CompanySignature = conNull(); companyInfo.update(); ttscommit; companyInfo_ds.research(true); } // Data source activate method { CompanyInfo companyInfo = sender.cursor() as CompanyInfo; FormWindowControl logoImage = sender.formRun().design().controlName(formControlStr(OMLegalEntity, DashboardSignatureImage)) as FormWindowControl; Image _image; ; if (companyInfo.CompanySignature) { _image = new Image(); _image.setData(companyInfo.CompanySignature); logoImage.image(_image); } else { logoImage.image(null); } } // File upload from this method public static str uploadImageFile() { FormRun visualForm; FileUpload fileUploadControl; str imageFilePathName; ; visualForm = classFactory::formRunClassOnClient(new Args(formstr(SysGetFileFromUser))); visualForm.init(); visualForm.design().caption("@ApplicationPlatform:GetFileImageCaption"); fileUploadControl = visualForm.design().controlName('FileUpload1'); visualForm.run(); visualForm.wait(); FileUploadTemporaryStorageResult fileUploadResult = fileUploadControl.getFileUploadResult(); if (fileUploadResult != null && fileUploadResult.getUploadStatus()) { imageFilePathName = fileUploadResult.getDownloadUrl(); } return imageFilePathName; } }

Keep daxing!!

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Error : Object reference not set to an instance of an object.
    @ line FileUploadTemporaryStorageResult fileUploadResult = fileUploadControl.getFileUploadResult();

    Upon checking, fileUploadControl is getting null value. Please suggest. Thank you

    ReplyDelete
    Replies
    1. @ line fileUploadControl = visualForm.design().controlName('FileUpload1'); have you change 'FileUpload1' to any other name ?
      If yes, please use 'FileUpload1'only because this field is in standard form.

      Delete