Link to home
Start Free TrialLog in
Avatar of Isaac
IsaacFlag for United States of America

asked on

jquery spservices 'undefined'

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!
Avatar of Kiran Sonawane
Kiran Sonawane
Flag of India image

boolVal - V is capital and javascript is case sensative language..

Try like this


var boolval = "True";
alert(boolval);
Avatar of Isaac

ASKER

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

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.
Avatar of Isaac

ASKER

I made the change but now i get "boolVal" is undefined.
ASKER CERTIFIED SOLUTION
Avatar of mcnute
mcnute
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Isaac

ASKER

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