hljs.configure({cssSelector: "code"}); hljs.highlightAll();

Tuesday, November 28, 2023

Upload image file in form grid using X++ in D365FO

  In this blog we are going to Learn how to add a simple button for users to upload signature images to specific records in a grid.

Form design:





Design details:

Data model changes:

1. Add container field with EDT as BitMap in table.

User interface changes:

Form design:

1. Added my table into form data source.

2. We cant directly add table container field into grid, so add new field in grid then map data source and field.


3.Added two Action menu items on action pane as "Change and  Remove".(for the best practice written code in classes, not in clicked methods, it reduces the client server side load.)

4. change button is for user to select image from local disc and Remove button is to remove the image file from record.

Class Architecture:

1. Created two classes for upload and remove.

Upload image: 

internal final class uploadImage
{
    public void uploadImageFile(formRun _fromRun)
    {
       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();
       }
       if(imageFilePathName)
       {
	      formDataSource  signaturetable_dss =  _fromRun().dataSource(1) as FormDataSource;
	      Signaturetable     signaturetableLoc = signaturetable_dss.cursor() as Signaturetable;  
	      ttsbegin;
	      signaturetableLoc.selectForUpdate(true);
	      signaturetableLoc.Image =ImageReference::GetPackedBinaryData(imageFilePathName);
	      signaturetableLoc.update();
	      ttscommit;
       }
    }

    public static void main(Args _args)
    {
       FromRun formRun = _args.formRun();
       uploadImage object = new uploadImage();
       object.uploadImageFile(formRun);
    }
}

Remove image:

internal final class removeImage
{
    public static void main(Args _args)
    {
	FormRun formRun = _args.formrun();
	formDataSource  signaturetable_dss =  formRun().dataSource(1) as FormDataSource;
	Signaturetable     signaturetableLoc = signaturetable_dss.cursor() as Signaturetable;

	ttsbgein;
	signaturetableLoc.selectForUpdate(true);
	signaturetableLoc.Image = conNull();
	signaturetableLoc.update();
	ttscommit;
    }
}
Thanks for reading!!

No comments:

Post a Comment