Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

jquery spservices 'undefined'

Posted on 2013-01-01
6
Medium Priority
?
4,020 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
How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

In case you ever have to remove a faulty web part from a page , add the following to the end of the page url ?contents=1
When using a search centre, I'm going to show you how to configure Sharepoint's search to only return results from the current site collection. Very useful when using Office 365 with multiple site collections.
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…

715 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