Solved

jquery spservices 'undefined'

Posted on 2013-01-01
6
3,381 Views
Last Modified: 2013-01-07
I created the following function and I'm trying to figure out whey the alert(boolval) returns 'undefined'.  The alert("Hello") works though.

function GetGroupOfUser(){
var curUser = $().SPServices.SPGetCurrentUser();
$().SPServices({
      operation: "GetGroupCollectionFromUser",
      userLoginName: $().SPServices.SPGetCurrentUser(),
      async: false,
      completefunc: function(xData, Status) {
        if($(xData.responseXML).find("Group[Name='ACC']").length == 1) {
alert("Hello");
		  var boolVal = "True";
alert(boolval);
        }
	else
        {
                  var boolval = "False";
alert(boolval);
        }
      }
   }); /*close().SPServices */
}	

Open in new window


Any ideas please?

Thanks!
0
Comment
Question by:Isaac
  • 3
  • 2
6 Comments
 
LVL 17

Expert Comment

by:sonawanekiran
Comment Utility
boolVal - V is capital and javascript is case sensative language..

Try like this


var boolval = "True";
alert(boolval);
0
 
LVL 5

Author Comment

by:Isaac
Comment Utility
That does not work either.

I switched it around...here's the full code that's calling the function...

<script type="text/javascript" language="javascript" src="http://moss2007vm1:35714/sdimetrics/js%20Assets/SDIfunctions.js"></script>
<script type="text/javascript" language="javascript" src="http://moss2007vm1:35714/js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" language="javascript" src="http://moss2007vm1:35714/js/jquery.SPServices-0.7.2.min.js"></script>


<script type="text/javascript" language="javascript">
$(document).ready(function() {
var valBool = GetGroupOfUser();

alert(valBool);

  $().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "SDIMetrics",
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='MAJCOM' /><FieldRef Name='ID' /></ViewFields>",
    completefunc: function (xData, Status) {
      $(xData.responseXML).SPFilterNode("z:row").each(function() {
	var liHtml = "<li>" + $(this).attr("ows_Title") + "</li>";
        $("#tasksUL").append(liHtml);
alert(liHtml);
      });

    }
  });
  
});
</script>




function GetGroupOfUser(){
var boolval = false;
var curUser = $().SPServices.SPGetCurrentUser();
$().SPServices({
      operation: "GetGroupCollectionFromUser",
      userLoginName: $().SPServices.SPGetCurrentUser(),
      async: false,
      completefunc: function(xData, Status) {
        if($(xData.responseXML).find("Group[Name='ACC']").length == 1) {
alert("Hello");
		  var boolVal = "True";
                  return boolVal;
        }

      }
   }); /*close().SPServices */
}	

Open in new window

0
 
LVL 11

Expert Comment

by:mcnute
Comment Utility
If you're trying to get the alert which is in your first scriptblock it will return undefined when the if clause is not firing.
In other words to return the boolVal you should take it out of the if clause of the GetGroupOfUser() function like so:

function GetGroupOfUser(){
var boolval = false;
var curUser = $().SPServices.SPGetCurrentUser();
$().SPServices({
      operation: "GetGroupCollectionFromUser",
      userLoginName: $().SPServices.SPGetCurrentUser(),
      async: false,
      completefunc: function(xData, Status) {
        if($(xData.responseXML).find("Group[Name='ACC']").length == 1) {
alert("Hello");
		  var boolVal = "True";

        }
                        return boolVal;
      }
   }); /*close().SPServices */

// better to put the return boolVal here, cause this returns even if ajax fails.
}

Open in new window


It is worthing to note, that if your ajax call fails then no boolVal will be returned either so you might return that value even outside your ajax call after the  /*close().SPServices */ comment to return at least false if ajax fails.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 5

Author Comment

by:Isaac
Comment Utility
I made the change but now i get "boolVal" is undefined.
0
 
LVL 11

Accepted Solution

by:
mcnute earned 500 total points
Comment Utility
Oh sorry, I've overseen, that in you ajax call you're redefining boolval. Once you defined outside, don't define it again elsewhere. Just set it to the according value in that context.

What I mean is you usually define a variable only once: var somvar = "something";
Additionally, the first variable is of type boolean by declaring it false, and the second value you declare is of type string, because you've put it within quotes. Stick to one type for that variable.

So try it like that:

function GetGroupOfUser(){
var boolVal = false;
var curUser = $().SPServices.SPGetCurrentUser();
$().SPServices({
      operation: "GetGroupCollectionFromUser",
      userLoginName: $().SPServices.SPGetCurrentUser(),
      async: false,
      completefunc: function(xData, Status) {
        if($(xData.responseXML).find("Group[Name='ACC']").length == 1) {
alert("Hello");
		  boolVal = true;

        }
                        
      }
   }); /*close().SPServices */

return boolVal;
}

Open in new window


Note: This will set boolVal to true only if the ajax completes correctly. But will return false if ajax call fails.
0
 
LVL 5

Author Comment

by:Isaac
Comment Utility
When I call the function from the code below, if the boolVal is false, I get boolVal is undefined.  Everything is ok if it's true though.

<script type="text/javascript" language="javascript">
$(document).ready(function() {
var getBoolVal= [b]GetGroupOfUser()[/b];
if(getBoolVal)
{
alert("True");
}
else
{
alert("False");
}
  $().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "SDIMetrics",
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='MAJCOM' /><FieldRef Name='ID' /></ViewFields>",
    completefunc: function (xData, Status) {
          $(xData.responseXML).SPFilterNode("z:row").each(function() {
        var liHtml = "<li>" + $(this).attr("ows_Title") + "</li>";
        $("#tasksUL").append(liHtml);
alert(liHtml);
      });

    }
  });
  
});
</script>

Open in new window

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now