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 :
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

I have the same question (0)
  • Suggested answer
    JohnAnonymous Profile Picture
    5,241 on at

    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.

  • Suggested answer
    Community Member Profile Picture
    on at

    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?

  • Community Member Profile Picture
    on at

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

  • Community Member Profile Picture
    on at

    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];

                       }

  • Suggested answer
    JohnAnonymous Profile Picture
    5,241 on at

    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.

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans