Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Getting function undefined while calling it on on-click event in javascript

(0) ShareShare
ReportReport
Posted on by

Getting this error

Uncaught ReferenceError: changeMSsegment is not defined

if (checkedProductGroups.contains(PG_result[j].new_name)) {
addInput = $("<input type='checkbox' checked='checked' style='border:none; width:25px; align:left;' id='" + PG_result[j].new_productgroupId + "' onclick='clickProductGroup()'/>")[0];
}
else {
addInput = $("<input type='checkbox' style='border:none; width:25px; align:left;' id='" + PG_result[j].new_productgroupId + "' onclick='clickProductGroup()'/>")[0];
}

******************************************

function changeMSsegment() {
debugger;
var OnChangeCheckedMSArr = new Array();
var OnChangeCheckedPGArr = new Array();
var formType = Xrm.Page.ui.getFormType();
if (formType != 1) {
Xrm.Page.data.save();
}
// var ckbArr = $("#new_addDiv_0 input[type='checkbox']:checked");
var ckbArr = parent.document.getElementById("new_addDiv_0").querySelectorAll('input[type="checkbox"]:checked');
var ckbPGArr = parent.document.getElementById("new_addDiv_1").querySelectorAll('input[type="checkbox"]:checked');
// var ckbPGArr = $("#new_addDiv_1 input[type='checkbox']:checked");

//required field logic
if (formType == 1) {
if (ckbArr.length > 0) {
$("#new_addDiv_0").css("border", "1px #6699cc solid");
}
else {
$("#new_addDiv_0").css("border", "1px red solid");
}
if (ckbPGArr.length > 0) {
$("#new_addDiv_1").css("border", "1px #6699cc solid");
}
else {
$("#new_addDiv_1").css("border", "1px red solid");
}
}


for (var i = 0; i < ckbPGArr.length; i++) {
OnChangeCheckedPGArr.push(ckbPGArr[i].id);
}
$("#new_addDiv_1").empty();
if (ckbArr.length > 0) {
for (var i = 0; i < ckbArr.length; i++) {
var options_1 = "$select=new_name,new_productgroupId&$filter=new_MSSegment/Id eq guid'" + ckbArr[i].id + "' and statecode/Value eq 0";
var PG_result = RetrieveMyMultipleRecords("new_productgroup", options_1);
if (PG_result != null) {
for (var j = 0; j < PG_result.length; j++) {
var pOption = PG_result[j].new_name;
var addInput = "";
if (OnChangeCheckedPGArr.contains(PG_result[j].new_productgroupId)) {
addInput = $("<input type='checkbox' checked='checked' style='border:none; width:25px; align:left;' id='" + PG_result[j].new_productgroupId + "' onclick='clickProductGroup()'/>")[0];
}
else {
addInput = $("<input type='checkbox' style='border:none; width:25px; align:left;' id='" + PG_result[j].new_productgroupId + "' onclick='clickProductGroup()'/>")[0];
}
var addLabel = $("<label class='mslab' style='width:150px;'/>")[0];
addLabel.innerText = pOption;
var addBr = $("<br/>")[0]; //it's a 'br' flag
parent.document.getElementById("new_addDiv_1").appendChild(addInput);
parent.document.getElementById("new_addDiv_1").appendChild(addLabel);
parent.document.getElementById("new_addDiv_1").appendChild(addBr);
}
}
}
}
}

*This post is locked for comments

  • Suggested answer
    JohnAnonymous Profile Picture
    5,241 on at
    RE: Getting function undefined while calling it on on-click event in javascript

    And that's why these kind of customizations are unsupported. Turbo forms loads en renders the page very differently than non-turbo forms. Most likely your javascript isn't loaded at all, or is loaded in an iFrame an is 'living' in a complete different context than the elements you are trying to access.

    You'll probably need to access the parent object of the iFrame to make you're code work. Please review this post for a solution: www.magnetismsolutions.com/.../unsupported-scripts-and-global-functions-and-variables-in-2016

    Please note that these customizations will probably break again with the next update.

  • Community Member Profile Picture
    on at
    RE: Getting function undefined while calling it on on-click event in javascript

    Hi Mohd

    asterik(*) is not there in the code, I have just added to show the function seperately and I have placed wrong code before asterik(*). We are calling like this

    if (checkedMSsegments.contains(MS_Arr[i][1])) {

                           addInput = $("<input type='checkbox' checked='checked' style='border:none; width:25px; align:left;' id='" + MS_Arr[i][0] + "' onclick='changeMSsegment()'/>")[0];

                       }

                       else {

                           addInput = $("<input type='checkbox' style='border:none; width:25px; align:left;' id='" + MS_Arr[i][0] + "' onclick='changeMSsegment()'/>")[0];

                       }

  • Community Member Profile Picture
    on at
    RE: Getting function undefined while calling it on on-click event in javascript

    Hi Martin,

        $("#new_addDiv_0").append(addInput);
         $("#new_addDiv_0").append(addLabel);
         $("#new_addDiv_0").append(addBr);

    Earlier it was working, after enabling turbo forms it stopped working. Then we have changed above lines

    as below and still not working

    parent.document.getElementById("new_addDiv_1").appendChild(addInput);
    parent.document.getElementById("new_addDiv_1").appendChild(addLabel);
    parent.document.getElementById("new_addDiv_1").appendChild(addBr);

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Getting function undefined while calling it on on-click event in javascript

    I can't see call for changeMSsegment function in the code. From where are you calling it?

    Is asterik(*) line there in the code only present here?

  • Suggested answer
    JohnAnonymous Profile Picture
    5,241 on at
    RE: Getting function undefined while calling it on on-click event in javascript

    It can't seem to find the function in the scope you are working on.

    And this also seems be changing the DOM which is not supported.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

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

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 231,723 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans