Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

jquery spservices 'undefined'

Posted on 2013-01-01
6
Medium Priority
?
4,147 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
ID: 38735644
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
ID: 38735652
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
ID: 38736120
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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 5

Author Comment

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

Accepted Solution

by:
mcnute earned 2000 total points
ID: 38736203
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
ID: 38736226
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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
Suggested Courses

971 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