Solved

jquery spservices 'undefined'

Posted on 2013-01-01
6
3,778 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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)
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…

738 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