Return value from GetListItems

May 26, 2013 at 10:56 PM
Hi. I've been looking at this for days now and just can not get it to work. I'm trying to return value "email" outside the function call to a variable called "subEmail". Code is below. Any ideas? The alert does not work.
    var subEmail = getSubmitterEmail($userName);
    alert(subEmail);

    function getSubmitterEmail(userName) {
        
        var _query = "<Query><Where><Eq><FieldRef Name='firstLastName' /><Value Type='Text'>" + userName + "</Value></Eq></Where></Query>";

        $().SPServices({
            operation: 'GetListItems',
            async: false,
            webUrl: '/',
            listName: '{732DBD98-D0CF-4F7D-8281-4F9D419138D2}',
            CAMLViewFields: "<ViewFields><FieldRef Name='Email' /></ViewFields>",
            CAMLQuery: _query,
            completefunc: 
                function (xData, Status) {        
                    $(xData.responseXML).SPFilterNode("z:row").each(function() { 
                        var email = ($(this).attr("ows_Email"));
                        var isMatch = email.substr(0, 7) == "mailto:";
                        
                        if (isMatch = "true") {
                            email = email.substring(7); }
                    });             
                } //close completefunc
        }); //close SPServices  
        
        return email;
    };
});
Coordinator
May 27, 2013 at 12:52 AM
You're defining the email variable inside the completefunc function, so it's scoped to that function. When you return it, it's out of scope, so you're getting undefined.

Define the email variable at the top of your getSubmitterEmail function instead, and all will be well.

M.
May 27, 2013 at 2:33 AM
Silly me! that worked. Thanks Marc!