web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Set value from parent ...
Power Apps
Unanswered

Set value from parent - Model Driven Apps dialog

(0) ShareShare
ReportReport
Posted on by 432 Moderator

Hi all,

 

I'm sure this is possible so sorry if it's dull! I want to be able to set a value in a dialog as the parent record. 

nickellis74_0-1713269658938.png

So in that screenshot, I plan to add a lookup column to the 'Ticket' entity which looks up 'QTA Responses', and I would like it to set automatically when the dialog opens. I would also like to populate the Company column with the corresponding value on the parent.

I assume this is javascript of some sort. A couple of considerations - we also use the same form in the main app, so it would be great for it to know that it should only do it when opened in dialog; secondly we will likely want the same dialog from other tables, and they will listed in other columns, so a "if QTA Response then, If Account then, If Recommendation then..." will be needed eventually.

Any and all help very gratefully received as always.

Nick

Categories:
  • QuentinDURIEZ Profile Picture
    577 Moderator on at

    Hi,

    I ran into the same problem and my solution was using cookies to save the id I want to use, since there is no method from the doc to do it.

     

    First, save the GUID in a cookie, can be done in the OnLoad event of the form or the OnChange of the client field.

    //Example of a set cookie method that I use
    function _setCookie(name, value){
     const expires = new Date();
     expires.setTime(expires.getTime() + 1 * 24 * 60 * 60 * 1000);
     document.cookie = `${name}=${value}; expires=${expires.toUTCString()}; path=/`;
    }


    Then, retrieve this cookie on the OnLoad event of the modal form

     

    //Example of a get cookie that I use
    function _getCookie(name) {
     const cookieString = document.cookie;
     const cookies = cookieString.split("; ");
     for (const cookie of cookies) {
     const [cookieName, cookieValue] = cookie.split("=");
     if (cookieName === name) {
     return decodeURIComponent(cookieValue);
     }
     }
     return null; // Cookie not found
    }

     

    With this solution, you can configure some rules with the cookie name to populate or not the client field

     

    I don't know if this solution is fully supported, but it works for my case

    Regards,

  • nickellis74 Profile Picture
    432 Moderator on at

    Thank you for this. I like it in principle but I confess I've not used cookies before. What will it return? And I'm assuming I can't quite just copy and paste what you've put? I need to set the name and value somehow?

    I really appreciate the reply, and I can definitely see how this works, just weak on the detail of cookies!

     

    Nick

  • a33ik Profile Picture
    3,306 Most Valuable Professional on at

    Hello,

    Can you please provide a code that you use to open that Dialog? I believe I saw a button in the command bar of the parent form.

    I believe you use Xrm.Navigation.navigateTo there. Please check and article that describes it - https://learn.microsoft.com/en-us/power-apps/developer/model-driven-apps/clientapi/reference/xrm-navigation/navigateto#entity-record

    Doublecheck createFromEntity parameter - populating it will establish the link momentarily and you'll have no need to use cookies or anything else.

  • QuentinDURIEZ Profile Picture
    577 Moderator on at

    Hi @nickellis74,

    A cookie is simply a name - value couple so you define what your cookie is ! 

    In my case, I needed to create a contact from a opportunity using the account, so I just store the account id since I can use the web api to get the other data needed

    So my first step is storing the id of the account :

     

    //Opportunity - OnChange account & OnLoad Form
    function onChangeAccount(executionContext){
     const formContext = executionContext.getFormContext();
     const compte = formContext.getAttribute('parentaccountid').getValue();
     if(compte === null) return;
     //Store the account id in a cookie called "_account"
     _setCookie('_account', compte[0].id );
    }

     

     Then, in my contact script I retrieve the cookie value to populate the parentcustomerid field (and other fields)

     

     

    //Contact - OnLoad Form
    async function OnLoadContact(executionContext){
     const formContext = executionContext.getFormContext();
     //Only in create mode ! 
     if(formContext.ui.getFormType() !== 1) return;
     //Retrieve the cookie value using its name
     const account = _getCookie('_account');
     if(account === null) return;
     let {name, man_comptesource} = await Xrm.WebApi.retrieveRecord('account', account, '?$select=name,man_comptesource');
     formContext.getAttribute('parentcustomerid').setValue([{id : account.replace(/[{}]/g, ''), name : name, entityType : 'account'}]);
     formContext.getAttribute('man_contactsource').setValue(man_comptesource);
     formContext.data.entity.addOnSave(() => _deleteCookie('_account'));
    
    }

     

     

    Bonus, a method to delete a cookie  :

     

    function _deleteCookie(name) {
     const expires = new Date();
     expires.setTime(expires.getTime() - 1); // Set expiration to a past date
     document.cookie = `${name}=; expires=${expires.toUTCString()}; path=/`;
    }

     

     

    Regards,

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 409 Super User 2026 Season 2

#2
Mohsin Ali Profile Picture

Mohsin Ali 328

#3
WarrenBelz Profile Picture

WarrenBelz 252 Most Valuable Professional

Last 30 days Overall leaderboard