1

I have a custom list form that processes information. There is a people picker on the form that can contain multiple users. When the user hits save, the js code parses through each user in the people picker and extracts information from the User Information List to build an XML string.

There are multiple content types sharing the same editform.aspx, and the other content types are working correctly. However, as of right now, this is the only content type that has a people picker included.

All other tests the return true statement saves the form. Hopefully someone can point out what I am overlooking.

if (selectedContentType == "CONTENTTYPENAMEGOESHERE"){
                if ($("input[Title='XML']").val() != ""){
                    $("input[Title='XML']").val("");
                }
                var messageId = SP.UI.Notify.addNotification("Processing user information.  Please wait...", false);

                var userXML = "";
                $("[id$='_UserField_upLevelDiv']").each(function(){
                    var fieldName = $(this).parentsUntil(".ms-formbody").parent().prev().text();                            
                    fieldName = fieldName.replace(" * ","");
                    if ($(this).text().length > 1){
                        if(fieldName.indexOf("Users") >= 0){
                            var accountName = "";
                            var dispName = "";
                            var email = "";
                            var ppVal = $().SPFindPeoplePicker({ peoplePickerDisplayName: fieldName});
                            for (var i = 0; i < ppVal.dictionaryEntries.length; i++){
                                var queryStr = "<Query><Where><Eq><FieldRef Name='EMail'/><Value Type='Text'>" + ppVal.dictionaryEntries[i].Email + "</Value></Eq></Where></Query>";
                                $().SPServices({
                                    operation: "GetListItems",
                                    webURL: "/",
                                    listName: "User Information List",
                                    CAMLQuery: queryStr,
                                    completefunc: function (xData, Status) {
                                        $(xData.responseXML).SPFilterNode("z:row").each(function() {
                                            accountName = $(this).attr("ows_Name").substring(8);
                                            dispName = $(this).attr("ows_Title");
                                            email = $(this).attr("ows_EMail");
                                        });
                                        var newXML = "<User LoginName=\"DOMAIN\\\\\\\\"+accountName+"\" Email=\""+email+"\" Name=\""+dispName+"\" Notes=\"Added via workflow\"/>";
                                        userXML = userXML.concat(newXML);
                                    }
                                });
                            }
                        }
                    }
                });
                setTimeout(function(){
                    $("input[Title='XML']").val(userXML);
                    return true;
                },3000);
            }

The settimeout was added because the code would not go in sequential order and would have a null value. 3 seconds seems to be the magic spot, so I set to that. Again, the return statement in that block is not firing.

Ideas?

1

If you REALLY need the function to do all of the work before it returns then you should use async: false with the SPServices call

But you should really embrace the async nature of JavaScript

  • That, and rearranging the setTimeout call in a child block, worked. Thanks. Yes, typically I do use asynchronization calls, but since OTB workflows could not build a dynamic string in a loop, I needed this process to complete before submitting the form. Thanks again! – JuanTrev Dec 31 '13 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.