Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Writing JScript to autopopulate a field

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

We have 6 address individual address fields that need to be aggregated into a separate 'Address Block' field. The current code used to autopopulate the 'Address Block' field works on contacts but not on accounts. This is because the root name of the state field at account level is different from the root name of the state field for the contact. The current code uses the contact state field name.

Is it possible to point a Java Script at an individual entity within CRM11? Below is the JScript in question:

//--- AUTOPOPULATE ADDRESS BLOCK
function autopopulateAddressBlock() {
var address_block = "";
if (Xrm.Page.getAttribute("address1_name").getValue() != null) {
address_block = address_block + Xrm.Page.getAttribute("address1_name").getValue() + "\n";
}
if (Xrm.Page.getAttribute("address1_line1").getValue() != null) {
address_block = address_block + Xrm.Page.getAttribute("address1_line1").getValue() + "\n";
}
if (Xrm.Page.getAttribute("address1_line2").getValue() != null) {
address_block = address_block + Xrm.Page.getAttribute("address1_line2").getValue() + "\n";
}
if (Xrm.Page.getAttribute("address1_line3").getValue() != null) {
address_block = address_block + Xrm.Page.getAttribute("address1_line3").getValue() + "\n";
}
if (Xrm.Page.getAttribute("address1_city").getValue() != null) {
address_block = address_block + Xrm.Page.getAttribute("address1_city").getValue() + ", ";
}
if (Xrm.Page.getAttribute("tprnew_state").getSelectedOption().text != null) {
address_block = address_block + Xrm.Page.getAttribute("tprnew_state").getSelectedOption().text + " ";
}
if (Xrm.Page.getAttribute("address1_postalcode").getValue() != null) {
address_block = address_block + Xrm.Page.getAttribute("address1_postalcode").getValue() + "\n";
}
if (Xrm.Page.getAttribute("address1_country").getValue() != null) {
address_block = address_block + Xrm.Page.getAttribute("address1_country").getValue() + "\n";
}
Xrm.Page.getAttribute("tpr_addressblock").setValue(address_block);
}

*This post is locked for comments

  • Somesh2207 Profile Picture
    Somesh2207 1,563 on at
    RE: Writing JScript to autopopulate a field

    Great Stanton. Glad that I could help you.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Writing JScript to autopopulate a field

    That did it, many thanks to the both of you. I have learned a lot and I appreciate your time.

  • Verified answer
    Somesh2207 Profile Picture
    Somesh2207 1,563 on at
    RE: Writing JScript to autopopulate a field

    [quote user="Stanton Coman"]If you change the state field and any other address field the script runs. [/quote]

    I think this is happening because there is onchange event on all the fields except the state field on the Contact form. Somehow the developer would have missed that.

    Can you do the below steps:

    1. Open the Contact form editor, click/ select on the Field which is working (anything except state e.g. City).
    2. Click on the Change properties in the Ribbon menu. Navigate to the Events tab like below:

    Ans4.png

    You should see an "OnChange" event attached below.

    3. We need to ensure this same OnChange should be present on the state field as well.

    4. If it is not, then Copy the function from here and add to the State field, so that the onchange would look sae for both City and State field.

    For copying from the city field, double click on the event handler row (Highlighted above)

    It should look like below. Copy the Function from here, and paste on the similar function on the state field

    ]Ans4.png

    please try this and let me know if it works.

    Please mark this as answer if it solves your query/ problem.

    Thanks

    Somesh Siripuram

    Sr CRM Consultant

    GitHubLinkedIn profile | Blog

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Writing JScript to autopopulate a field

    Ok so I've found something new. Whoever put this together created two web resources, one for Contacts another for Accounts, and put the original JScript in each resource. They weren't labeled well so I couldn't find them.

    I changed the JScript back to the original code and updated the state field called for each entity.

    It runs perfectly on the Account level now however on Contacts it will not update the address block OnChange if the State field is the only field you change. If you change the state field and any other address field the script runs. This doesn't happen at the account level, the address block updates OnChange if you change the Account state field.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Writing JScript to autopopulate a field

    Sorry, I'm new to this. How do I assign the function to the OnChange event? When I try to add the web resource to the Account entity form it tells me it's an "Invalid Web Resource type".

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Writing JScript to autopopulate a field

    Yes, all three fields are populated however in the past this has not stopped the script for populating the changed information into the address block. The problem with this script began when I was forced to create a new state field at the contact level so that we could get the field mapping to work. Before that both entities used a state fields with the same tpr_state name.

  • Suggested answer
    Somesh2207 Profile Picture
    Somesh2207 1,563 on at
    RE: Writing JScript to autopopulate a field

    Hi Stanton,

    From the error description, it seems that you have not registered the Function on the on change event, as it is not able to find the function. 

    Can you please recheck if that is the case.

    Please mark this as answer if it resolves your query

    Thanks

    Somesh Siripuram

    Sr CRM Consultant

  • ashlega Profile Picture
    ashlega 34,477 on at
    RE: Writing JScript to autopopulate a field

    Hi Stanton,

     have you added the web resource to your account form(and have you published all just to be sure)?

     As for the contact, I'm wondering if all 3 fields have been populated (otherwise, that if condition at the beginning wouldn't work):

     address1_line1, address1_city, tprnew_state

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Writing JScript to autopopulate a field

    Still running into the same problem, 'Address Block' will not update on Contact and I get the message below when trying to change any of the address fields on Accounts.

    State_5F00_Error.JPG

    I checked the dependencies of the Web Resource and it shows the Contact entity but not the Account entity. However, when I look at the dependencies of Accounts the Web Resource comes up. On dependencies from the Contact side the Web Resource doesn't show up. I'm confused as two why it appears differently in all three.

    //--Autopopulate Address Block

    function autopopulateAddressBlock() {

    var address_block = "";

    var attribName="tprnew_state";

    if(Xrm.Page.data.entity.getEntityName() == "account") attribName = "tpr_state";

    if (Xrm.Page.getAttribute("address1_line1").getValue() != null && Xrm.Page.getAttribute("address1_city").getValue() != null && Xrm.Page.getAttribute(attribName).getSelectedOption().text != null)

     {

        if (Xrm.Page.getAttribute("address1_name").getValue() != null) {

            address_block = address_block + Xrm.Page.getAttribute("address1_name").getValue() + "\n";

        }

        if (Xrm.Page.getAttribute("address1_line1").getValue() != null) {

            address_block = address_block + Xrm.Page.getAttribute("address1_line1").getValue() + "\n";

        }

        if (Xrm.Page.getAttribute("address1_line2").getValue() != null) {

            address_block = address_block + Xrm.Page.getAttribute("address1_line2").getValue() + "\n";

        }

        if (Xrm.Page.getAttribute("address1_line3").getValue() != null) {

            address_block = address_block + Xrm.Page.getAttribute("address1_line3").getValue() + "\n";

        }

        if (Xrm.Page.getAttribute("address1_city").getValue() != null) {

            address_block = address_block + Xrm.Page.getAttribute("address1_city").getValue() + ", ";

        }

        if (Xrm.Page.getAttribute(attribName).getSelectedOption().text != null) {

            address_block = address_block + Xrm.Page.getAttribute(attribName).getSelectedOption().text + " ";

        }

        if (Xrm.Page.getAttribute("address1_postalcode").getValue() != null) {

            address_block = address_block + Xrm.Page.getAttribute("address1_postalcode").getValue() + "\n";

        }

        if (Xrm.Page.getAttribute("address1_country").getValue() != null) {

            address_block = address_block + Xrm.Page.getAttribute("address1_country").getValue() + "\n";

        }

        Xrm.Page.getAttribute("tpr_addressblock").setValue(address_block);

    }

    }

  • ashlega Profile Picture
    ashlega 34,477 on at
    RE: Writing JScript to autopopulate a field

    ACtually, no.. try this:

    Try this:

    function autopopulateAddressBlock() {

     var address_block = "";

     var attribName="tprnew_state";

     if(Xrm.Page.data.entity.getEntityName() == "account") attribName = "tpr_state";

     if (Xrm.Page.getAttribute("address1_line1").getValue() != null && Xrm.Page.getAttribute("address1_city").getValue() != null && Xrm.Page.getAttribute(attribName).getSelectedOption().text != null)

      {

         if (Xrm.Page.getAttribute("address1_name").getValue() != null) {

             address_block = address_block + Xrm.Page.getAttribute("address1_name").getValue() + "\n";

         }

         if (Xrm.Page.getAttribute("address1_line1").getValue() != null) {

             address_block = address_block + Xrm.Page.getAttribute("address1_line1").getValue() + "\n";

         }

         if (Xrm.Page.getAttribute("address1_line2").getValue() != null) {

             address_block = address_block + Xrm.Page.getAttribute("address1_line2").getValue() + "\n";

         }

         if (Xrm.Page.getAttribute("address1_line3").getValue() != null) {

             address_block = address_block + Xrm.Page.getAttribute("address1_line3").getValue() + "\n";

         }

         if (Xrm.Page.getAttribute("address1_city").getValue() != null) {

             address_block = address_block + Xrm.Page.getAttribute("address1_city").getValue() + ", ";

         }

         if (Xrm.Page.getAttribute(attribName).getSelectedOption().text != null) {

             address_block = address_block + Xrm.Page.getAttribute(attribName).getSelectedOption().text + " ";

         }

         if (Xrm.Page.getAttribute("address1_postalcode").getValue() != null) {

             address_block = address_block + Xrm.Page.getAttribute("address1_postalcode").getValue() + "\n";

         }

         if (Xrm.Page.getAttribute("address1_country").getValue() != null) {

             address_block = address_block + Xrm.Page.getAttribute("address1_country").getValue() + "\n";

         }

         Xrm.Page.getAttribute("tpr_addressblock").setValue(address_block);

     }

    }

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

Announcing Our 2025 Season 1 Super Users!

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

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,321 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans