Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum
Suggested answer

Urgent help - How to stop this script from executing on already populated Form?

Posted on by 85

Hi,

I am using this JS script to populate two blank fields OnLoad of the Main form. How can I amend this so that, if these fields are already populated, don't "over-write" them?

function onFormLoad() {
var userId = Xrm.Page.context.getUserId();
var request = new XMLHttpRequest();
var newuserId = userId.replace(/{|}/g, "");
var emailAddressField = Xrm.Page.getAttribute("crc1e_csosubmitteremail");
request.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/systemusers(" + newuserId + ")?$select=internalemailaddress", true);
request.setRequestHeader("OData-MaxVersion", "4.0");
request.setRequestHeader("OData-Version", "4.0");
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader("Content-Type", "application/json; charset=utf-8");
request.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
request.onreadystatechange = function() {
if (this.readyState === 4) {
request.onreadystatechange = null;
if (this.status === 200) {
var user = JSON.parse(this.response).value;
var emailAddress = user[0].internalemailaddress;
emailAddressField.setValue(result["internalemailaddress"]);
}
else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
request.send();

var userName = Xrm.Page.context.getUserName();
Xrm.Page.getAttribute("new_csosubmittername").setValue(userName);
}

Thanks

K.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,321 Most Valuable Professional on at
    RE: Urgent help - How to stop this script from executing on already populated Form?

    Hello,

    Just add the code

    if (!!Xrm.Page.getAttribute("crc1e_csosubmitteremail").getValue()) {

    return;

    }

    At the very beginning of your onload handler.

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

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

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,552 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,552 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans