Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested answer

How to handle server methods in D365

(0) ShareShare
ReportReport
Posted on by 235

Hello All,

I am using a standard class in my code but this class need System admin role to use it, if you don't have System admin the method will jump to the "catch" statement and give you the error written there . The class name "DocuTemplateRender" method renderTemplateToStream ,so my question how I can add this class and method to a security role in D365 FF&O.

Any help would be appreciated 

Thanks 

Maen.M

  • Suggested answer
    ergun sahin Profile Picture
    8,812 Moderator on at
    RE: How to handle server methods in D365

    As far as I understand you can't get into the renderTemplateToStream method in debug. I don't know why you can't get into method in debug. if you want a solution without further complicating the work;

    Then, you can move the controls inside the method to your own method and see which one it explodes.

    There are four controls. You have all the necessary objects. You can just move the controls to your own method or for testing purposes, copy the method as is and review it in debug.

    The solution part will be easier after determining which one the error is caused by.

  • Maen.Moutaz Profile Picture
    235 on at
    RE: How to handle server methods in D365

    in case (it is not authorized to read the object to be thrown into the Stream) how can we solve this?

    Also how to use "templateStream.CanWrite" and for what ?

  • ergun sahin Profile Picture
    8,812 Moderator on at
    RE: How to handle server methods in D365

    Can you try "templateStream.CanWrite" in your code

  • Suggested answer
    ergun sahin Profile Picture
    8,812 Moderator on at
    RE: How to handle server methods in D365

    It's hard to tell where it got an error without going into debug. Below, I commented on 4 points in the code where it throw errors. Of course, the method may throw errors from other methods that it calls.

    public void renderTemplateToStream(DocuTemplate template, Map filtersToApply, System.IO.Stream outputStream, object context = null)
    {
        //1.Wrong parameters specified
        //template and outputStream must be full. 
        //If you are getting this error, either the code could not find the record in DocuTemplate, 
        //or it is not authorized to read the object to be thrown into the Stream 
        //(it may not find the object too)
        //
        if (!template || !outputStream)
        {
            throw error(error::missingParameter(null));
        }
    
        if (!filtersToApply)
        {
            filtersToApply = new Map(Types::String, Types::Class);
        }
        
        //2.Function renderTemplateToStream has been incorrectly called.
        //I don't think the error is from here
        else if (filtersToApply.keyType() != Types::String || filtersToApply.valueType() != Types::Class)
        {
            throw error(error::wrongUseOfFunction(methodStr(DocuTemplateRender, renderTemplateToStream)));
        }
        
        //3.Function renderTemplateToStream has been incorrectly called.
        // CanWrite Property indicates whether the current stream supports writing.
        if (!outputStream.CanWrite)
        {
            throw error(error::wrongUseOfFunction(methodStr(DocuTemplateRender, renderTemplateToStream)));
        }
    
        var filtersByPublicName = this.translateFilterMap(filtersToApply);
    
        switch (template.TemplateType)
        {
            case OfficeAppApplicationType::Excel:
                this.renderExcelTemplate(template, filtersByPublicName, outputStream, context);
                break;
            case OfficeAppApplicationType::Word:
                this.renderWordTemplate(template, filtersByPublicName, outputStream, context);
                break;
            default:
            //4.Finally the type should be Excel or word
                var ex = new System.NotImplementedException();
                throw error(ex.Message);
        }
    }

  • Maen.Moutaz Profile Picture
    235 on at
    RE: How to handle server methods in D365

    here is the code :

    System.IO.MemoryStream templateStream = new System.IO.MemoryStream();

    DocuTemplateRender renderer = new DocuTemplateRender();

    renderer.renderTemplateToStream(template, filtersToApply,templateStream ,OfficeTrimmable::construct());

    templateStream.Seek(0, System.IO.SeekOrigin::Begin);

    when debugging after going to the highlighted line when I am a System admin the debugger comes back to this line and proceed with the execution when having Human resource manager role it didn't come back to the highlighted line and jump to the end of the code to catch and throw the error inside.

    "throw error(strFmt("Document '%1' generation failed for employee %2", employeeDocumentType.Description, HcmWorker::find(currWorkerRecId).name()));" 

  • ergun sahin Profile Picture
    8,812 Moderator on at
    RE: How to handle server methods in D365

    Since it is not called from the Class menuItem, there is no logic to make a new menuItem.

    As I have already written, I don't think the issue is about the security of the class. These classes contain many structures that will require authorization. It is difficult to say anything for sure, because you do not share the error you received, and the codes you received the error.

    If the error seems meaningless, you can check it with Debug or check why it fell into catch in the code.

  • Maen.Moutaz Profile Picture
    235 on at
    RE: How to handle server methods in D365

    Dear Osman Akgündüz and Ergün Şahin

    I have tried the service operation with no luck

    and i have tried to add the class to actionmenu item but i got error on build that this class doesn't contain "Main" method

    Also i have tried adding the DocuTemplate table to the privilege  no luck .

    Ergün  the error is from the catch in my method.

  • Suggested answer
    atul yadav Profile Picture
    310 on at
    RE: How to handle server methods in D365

    Hi Ma'en Moutaz,

    As Osman mentioned, you must create an action menu item for the class than add that menu item to the privileges

    and than add that privilege to the desired role.

    Regards,

    Atul Yadav

    dynamicscommunity101.blogspot.com

  • Suggested answer
    OsmanIstanbul Profile Picture
    2,781 on at
    RE: How to handle server methods in D365

    As Ergun mentioned you must add your class to an action menu item then add to the key on the above screen.

  • Suggested answer
    ergun sahin Profile Picture
    8,812 Moderator on at
    RE: How to handle server methods in D365

    I don't think the error was caused by the Class itself. Can you share the error?

    If I remember correctly, many of the methods of this class were using the DocuTemplate table. The error may be caused by this table.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 16th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 283,860 Test role Public

#2
Ludwig Reinhard Profile Picture

Ludwig Reinhard Microsoft Employee

#3
Martin Dráb Profile Picture

Martin Dráb 225,155 Moderator

Leaderboard

Product updates

Dynamics 365 release plans