Skip to main content

Notifications

Announcements

No record found.

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.

  • Suggested answer
    Kjeld Poulsen Profile Picture
    Kjeld Poulsen 180 on at
    RE: Javascript to get parentid on customeraddress

    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.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Javascript to get parentid on customeraddress

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

  • Ben Cook Profile Picture
    Ben Cook 35 on at
    RE: Javascript to get parentid on customeraddress

    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
    a33ik 84,325 Most Valuable Professional on at
    RE: Javascript to get parentid on customeraddress

    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
    Ben Cook 35 on at
    RE: Javascript to get parentid on customeraddress

    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.

  • cloflyMao Profile Picture
    cloflyMao 25,202 on at
    RE: Javascript to get parentid on customeraddress

    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);
        }
    );

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,468 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans