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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Javascript to get parentid on customeraddress

(0) ShareShare
ReportReport
Posted on by 35

A client has a requirement to make the address form read only if the address is for an account, but editable for contacts. The parentid field is not available to add to the form and hide. I could query existing records to get the value, but this doesn't help with new address records. Is there any supported way to get the parentid field in the onload event? I've tried looking at formContext.data.attributes, and looped through all controls on the form to see what's available with no luck.

I have the same question (0)
  • cloflyMao Profile Picture
    25,210 on at

    Hi Ben,

    What is "the parentid field"?

    If you want to retrieve fields data of parent record(lookup as per my understanding), you could run Xrm.WebAPI request at OnLoad event of the form to query the parent record.

    var parentEntity = formContext.getAttribute('parent_fieldid').getValue()[0]; 
    
    var parentEntityId = parentEntity.id.replace(/\{|\}/gi, "").toLowerCase();
    
    var parentEntityName = parentEntity.entityType;
    
    var filter = "";
    
    Xrm.WebApi.retrieveRecord(parentEntityName, parentEntityId, filter).then(
        function success(result) {
            console.log(result);
        },
        function (error) {
            console.log(error.message);
        }
    );

    If "the parentid field" is a field of current entity and is not able to be added to form, you could also run Xrm.WebAPI request to get its value directly.

    var id = formContext.data.entity.getId().replace(/\{|\}/gi, "").toLowerCase();
    
    var etn = formContext.data.entity.getEntityName();
    
    Xrm.WebApi.retrieveRecord(etn, id).then(
        function success(result) {
            console.log(result);
        },
        function (error) {
            console.log(error.message);
        }
    );

  • Ben Cook Profile Picture
    35 on at

    ParentId on customeraddress is the same as CustomerId in other places in CRM. It can be either an account or contact. It can't be added to the window so formContext.getAttribute doesn't work. I can query an existing customeraddress to get the value, but that won't work for new records.

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Check the following article - butenko.pro/.../

    I'm not sure if you use Quick Create forms for new Address Records but it could help to get the originating record.

  • Ben Cook Profile Picture
    35 on at

    Xrm.Utility.getPageContext().input.data contains the parent information for new address records. I have to query to get the parent information for existing addresses.

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    You will have to combine those 2 different approaches to make it work I'm afraid.

  • Suggested answer
    Kjeld Poulsen Profile Picture
    180 on at

    This small typescript snipper pretty much shows what you need to do. You need different approach for Create/QuickCreate and other form types:

    pastedimage1633409332319v1.png

    The code is getting the parent from the utility.getpagecontext on create, but on other formtypes it will fetch the customer address, and after that, the account from the parentid on the customer address.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 61

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 52 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans