Monday, May 2, 2022

Download the attachment of record on focus of a field D365 FO

class DapApplicantEventViewer

{    

[FormControlEventHandler(formControlStr(HRMApplication, AttachmentLink), FormControlEventType::GotFocus)]

    public static void AttachmentLink_OnGotFocus(FormControl sender, FormControlEventArgs e)

    {

        FormDataSource FormDatasource;

        DocuRef          docuRef;

        HRMApplication   hRMApplication;

        MenuFunction     menuFunction;

        HcmApplicant HcmApplicantLocal;

        Args             argsMenu= new Args();

        


        FormRun form = sender.formRun();

        FormDatasource  =       form.dataSource(formDataSourceStr(HRMApplication,HRMApplication)) as FormDataSource;

        hRMApplication                   = FormDatasource.cursor();


        select RecId,TableId from HcmApplicantLocal

            where HcmApplicantLocal.Recid == hRMApplication.Applicant;

        select docuref

                where docuref.RefRecId == HcmApplicantLocal.recid

            &&    docuref.RefTableId  == HcmApplicantLocal.Tableid;


        argsMenu.record(docuRef);

        argsMenu.parmEnumType(enumNum(DocuCode));

        argsMenu.parmEnum(DocuCode::Open);

        URL displayUrl = DocumentManagement::getAttachmentPublicUrl(docuref);

        //Info(strFmt("%1", displayUrl));

        menuFunction = new MenuFunction(menuItemActionStr(DocuActionOpen),MenuItemType::Action);

        menuFunction.run(argsMenu);  

    }

}