Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Suggested answer

Link-entity XrmFetch (to hide a button from ribbon)

Posted on by 37
hello guys,
I have a button /Submit For Approval/i want to hide it based on some conditions the button is in the ribbon of an entity /expense/ then in this entity i have a subgrid for entity/expense line/, in this subgrid expense line i have a subgrid of entity /attachment/ so in this attachment i have 2 fields /FileUrl/,/Attachment/ so based on these 2 fields if they are empty i dont want the button to be shown and if they are not empty the button should be shown.
i'll send my fetch , my main problem is how to retrive the data from it to make a condition on them  
 
  var recordId = Xrm.Page.data.entity.getId().substr(1, 36);
 
    Xrm.WebApi.retrieveMultipleRecords(/mr_expenses/, `?fetchXml=<fetch>
      <entity name=/mr_expenses/>
        <filter>
          <condition attribute=/mr_expensesid/ operator=/eq/ value=/${recordId}/ />
        </filter>
        <link-entity name=/mr_expenseslines/ from=/mr_expenseid/ to=/mr_expensesid/>
          <link-entity name=/new_attachment/ from=/new_regardingattachmentid/ to=/mr_expenseslinesid/>
            <attribute name=/new_attachfile/ />
            <attribute name=/new_fileurl/ />
          </link-entity>
        </link-entity>
      </entity>
</fetch>`).then(e=>{
    console.log(e)
  • Suggested answer
    Dengliang Li Profile Picture
    Dengliang Li Microsoft Employee on at
    Link-entity XrmFetch (to hide a button from ribbon)
    Hi,
     
    You can use the following API to get the id of the subgrid record for the current form.
    GridEntity (Client API reference) in model-driven apps - Power Apps | Microsoft Learn
    gridEntity.getId (Client API reference) - Power Apps | Microsoft Learn
     

    function onload(executionContext) {
        var formContext = executionContext.getFormContext();
     
        //Get expenseline subgrid controlThe Get Control method is populated with the name of the subgrid.
        var gridContext = formContext.getControl("ActiveExpenseLines");
        if(gridContext !== null){
     
            //Delay for one second and wait for the subgrid to finish loading before reading.
            setTimeout(() => {
               
                var myRows = gridContext.getGrid().getRows();
                var myRow = myRows.get();
                //Get the ID of the first expenseline subgrid record
                var expenseLinesId = myRow[0].data.entity.getId();
                //OData Query Expressions
                //cr2c4_attachment_ExpenseLine_cr2c4_expenselinethe name of the relationship between the Expenseline entity and the Attachment entity
                var query = `?$select=cr2c4_newcolumn&$filter=cr2c4_expenselineid eq ${expenseLinesId}
                                &$expand=cr2c4_attachment_ExpenseLine_cr2c4_expenseline($select=cr2c4_filename,cr2c4_attachment,cr2c4_fileurl)`;
                Xrm.WebApi.retrieveMultipleRecords("cr2c4_expenseline", query).then(
                function success(result) {
                    for (var i = 0; i < result.entities.length; i++) {
                        console.log(result.entities[i].cr2c4_attachment_ExpenseLine_cr2c4_expenseline[0]);
                    }                    
                },
                function (error) {
                    console.log(error.message);
                }
            );
            }, 1000);
       
            }
        }
     
    Gets the attachment field and the FileUrl field in the Attachment entity. You can get these fields to build the condition.
     

     
    If my answer was helpful, please click Like, and if it solved your problem, please mark it as verified to help other community members find more.
    If you have further questions, please feel free to contact me .
     
    Best Regards,
    Dengliang Li
     

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans