Sharepoint 2013 - JSOM - Not Saving record when People object added

Hello,

We are running SharePoint 2013, on Premise. I am using JSOM on an .aspx page in SharePoint to create a custom form for a list (as I have to combine two lists for this). When I comment out the People assignment, the solution goes through with an error (SCRIPT5007: Unable to get property 'apply' of undefined or null reference), but it saves the list item. With the People item, it fails without an error message and does not save the item.

This is using JavaScript (JSOM) for the manipulation of the values. As you can see, I am not using the People selector for this form, but just a hidden field that I am storing a value. I have access to the full SP.User object. Based on the reference material below, what I am doing should work.

I have tried the following:
user.get_title (User Full Name)
user.get_id (user ID in SharePoint
user.get_loginName (AD user identifier)

for the value to be sent. It is currently set to the SP.User object, and that is not working either.

Help
Any help on solving the following two problems would be appreciated:
SCRIPT5007: Unable to get property 'apply' of undefined or null reference
Person Set for adding to SharePoint list saving

Error Information/location
// MicrosoftAjax.js
Function.__typeName="Function";Function.__class=true;Function.createCallback=function(b,a){return function(){var e=arguments.length;if(e>0){var d=[];for(var c=0;c<e;c++)d[c]=arguments[c];d[e]=a;return b.apply(this,d)}return b.call(this,a)}};Function.createDelegate=function(a,b){return function(){return b.apply(a,arguments)}};Function.emptyFunction=Function.emptyMethod=function(){};Function.validateParameters=function(c,b,a){return 

Open in new window


HTML (of just the single Person Element)
        <div class="row bhiOutlinedRow">
            <div class="col-xs-12 col-sm-4 bhiCAFLabel"><span class="bhiFormLabel"><span class="ms-formvalidation"> *</span> Requestor:</span></div>
            <div class="col-xs-12 col-sm-8">
                <div id="RequestorName"></div>
                <input type="hidden" id="ff3{$Pos}" name="Requestor" data-type="Person" />
            </div>
        </div>

Open in new window


JavaScript:

            function addCAFForm() {
                siteURL = 'https://intracleandev.bissell.com/legal';
                var clientContext = new SP.ClientContext(siteURL);
                var oList = clientContext.get_web().get_lists().getByTitle('CAF - Full Form');
                var itemCreateInfo = new SP.ListItemCreationInformation();

                this.oListItem = oList.addItem(itemCreateInfo);
                getCurrentUser();

                $("#bhiCAFForm :input").each(function (index, element) {
                    // Let's make sure we are not capturing any buttons here
                    if ($(this).attr('type') != 'button') {
                        switch ($(this).attr("data-type")) {
                            case 'text':
                                oListItem.set_item($(this).attr('name'), $(this).val());
                                break;
                            case 'number':
                                if (!isNaN(parseInt($(this).val()))) {
                                    oListItem.set_item($(this).attr('name'), parseInt($(this).val()));
                                }
                                break;
                            case 'money':
                                if (!isNaN(parseFloat($(this).val()))) {
                                    oListItem.set_item($(this).attr('name'), parseFloat($(this).val()));
                                }
                                break;
                            case 'choice':
                                if ($(this).attr('type') == 'radio') {
                                    oListItem.set_item($(this).attr('name'), $('input[name=' + $(this).attr('name') + ']:checked').val());
                                }
                                else if ($(this).attr('type') == 'checkbox') {
                                    var checkboxes = $('input[name=' + $(this).attr('name') + ']:checked');
                                    var result = new Array();

                                    for (var i = 0; i < checkboxes.length; i++) {
                                        result[i] = $(checkboxes[i]).val();
                                    }
                                    oListItem.set_item($(this).attr('name'), result);
                                }
                                else {
                                    oListItem.set_item($(this).attr('name'), $(this).val());
                                }
                                break;
                            case 'Person':
                            	//var user = SP.FieldUserValue.fromUser(curUser.get_loginName());
                            	var user = SP.FieldUserValue.fromUser("homecare\\hertenrw");
                            	oListItem.set_item('RequestorId', user);
                                console.log($(this).attr('name') + ", " + $(this).val());
                                //                                oListItem.set_item($(this).attr('name'), $(this).val());
                                console.log(curUser.get_loginName());
                                console.log(user);
                                //oListItem.set_item($(this).attr('name'), curUser.get_loginName());
                         //       oListItem.set_item($(this).attr('name'),  SP.FieldUserValue.fromUser(curUser.get_loginName()));
                                break;
                            case 'date':
                                if (($(this).val() != null) && ($(this).val() != "") && (isValidDate($(this).val()))) {
                                    oListItem.set_item($(this).attr('name'), $(this).val());
                                }
                                break;
                            default:
                                oListItem.set_item($(this).attr('name'), $(this).val());
                                break;
                        }
                    }
                });         // end of $(":input").each(function(){
			

                oListItem.update();
                clientContext.load(oListItem);
                clientContext.executeQueryAsync(
                        Function.createDelegate(this, this.addListSuccess),
                        Function.createDelegate(this, this.addListFailure)
                    );
            }


            // The following SharePoint management code is from:
            // http://www.plusconsulting.com/blog/2013/05/crud-on-list-items-using-rest-services-jquery/



            function addListSuccess() {
                console.log("Success");
            }       // end of function addListSuccess(data) {

            function addListFailure() {
                console.log("Failure");
            }

            function getCurrentUser() {
                this.clientContext = new SP.ClientContext.get_current();
                this.oWeb = clientContext.get_web();
                currentUser = this.oWeb.get_currentUser();
                this.clientContext.load(currentUser);
                clientContext.executeQueryAsync(
                    function () {
                        $("#RequestorName").html(currentUser.get_title());
                        $("input[name=Requestor]").val(currentUser.get_loginName());
                        curUser = currentUser;
                    },
                    function () { }
                );
            }       // end of function getCurrentUser() {

Open in new window


Specific section of JavaScript with the issue:
                            case 'Person':
                            	//var user = SP.FieldUserValue.fromUser(curUser.get_loginName());
                            	var user = SP.FieldUserValue.fromUser("homecare\\hertenrw");
                            	oListItem.set_item('RequestorId', user);
                                console.log($(this).attr('name') + ", " + $(this).val());
                                //                                oListItem.set_item($(this).attr('name'), $(this).val());
                                console.log(curUser.get_loginName());
                                console.log(user);
                                //oListItem.set_item($(this).attr('name'), curUser.get_loginName());
                         //       oListItem.set_item($(this).attr('name'),  SP.FieldUserValue.fromUser(curUser.get_loginName()));
                                break;

Open in new window


XML of the SharePoint List Object
      <m:properties>
        <d:FileSystemObjectType m:type="Edm.Int32">0</d:FileSystemObjectType>
        <d:Id m:type="Edm.Int32">38</d:Id>
        <d:ContentTypeId>0x0100540A2BBE36BDFB419BA3433C172D2ED1</d:ContentTypeId>
        <d:Title m:null="true" />
        <d:CAF_x0020_Number m:type="Edm.Double">0</d:CAF_x0020_Number>
        <d:RequestorId m:null="true" />
        <d:Is_x0020_this_x0020_CAF_x0020_a_>DRAFT</d:Is_x0020_this_x0020_CAF_x0020_a_>
        <d:Choose_x0020_one m:null="true" />
        <d:Type_x0020_of_x0020_Agreement m:null="true" />
        <d:Choose_x0020_One_x003a_>New Contract</d:Choose_x0020_One_x003a_>

Open in new window


Highlighted field
        <d:RequestorId m:null="true" />

Open in new window


Reference of code:
http://pointofint.blogspot.com/2014/03/how-to-set-any-spfield-value-with-jsom.html
LVL 2
bissellGRAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bissellGRAuthor Commented:
The following is the solution Microsoft and I worked out.

Issue with the Apply
The solution was the the delegate was not getting called properly. by removing the this.functionName and making it just functionName resolved that issue.

Issue with the Requestor ID
Even though the XML stated that the Requester field name was supposed to be RequestorID, by going into the field itself, it states in the URL that the field name is Requestor. This resolved the second issue.

Failed Save
The failed save was happening because it could not find RequestorID in the field list. By changing it over to Requestor, this resolved the issue. Since the inception of this code, I have added more fields that needed to be blocked out from getting added into the SP Object. These allow for the the object to still save.

JavaScript function for parsing all of the values
                        /**
                         * addCAFForm
                         **/
                        function addCAFForm() {
                            siteURL = '/legal';
                            var clientContext = new SP.ClientContext(siteURL);
                            var oList = clientContext.get_web().get_lists().getByTitle('CAF - Full Form');
                            var itemCreateInfo = new SP.ListItemCreationInformation();
                            isFormAdded = 1;

                            this.oListItem = oList.addItem(itemCreateInfo);
                            getCurrentUser();

                            $("#bhiCAFForm :input").each(function (index, element) {
                                // Let's make sure we are not capturing any buttons here
                                if ($(this).attr('type') != 'button') {
                                    if ($(this).attr('name') == 'Law_x002f_Jurisdiction_x0020_Des') {
                                        var lawSelection = $('input:radio[name=Law_x002f_Jurisdiction_x0020_Des]:checked').val();
                                        if ((lawSelection == null) || (lawSelection == 'Other')) {
                                            lawSelection = $('input:text[name=Law_x002f_Jurisdiction_x0020_Des]').val();
                                        }
                                        oListItem.set_item($(this).attr('name'), lawSelection);
                                    }
                                    else {
                                        switch ($(this).attr("data-type")) {
                                            case 'choice':
                                                if ($(this).attr('name') == 'Law_x002f_Jurisdiction_x0020_Des') {
                                                    var lawSelection = $('input:radio[name=Law_x002f_Jurisdiction_x0020_Des]:checked').val();
                                                    if (lawSelection == null) {

                                                        lawSelection = $('#ff79{$Pos}_Other_Value').val();
                                                    }
                                                    oListItem.set_item($(this).attr('name'), lawSelection);
                                                }
                                                if ($(this).attr('type') == 'radio') {
                                                    oListItem.set_item($(this).attr('name'), $('input[name=' + $(this).attr('name') + ']:checked').val());
                                                }
                                                else if ($(this).attr('type') == 'checkbox') {
                                                    var checkboxes = $('input[name=' + $(this).attr('name') + ']:checked');
                                                    var result = new Array();

                                                    for (var i = 0; i < checkboxes.length; i++) {
                                                        result[i] = $(checkboxes[i]).val();
                                                    }
                                                    oListItem.set_item($(this).attr('name'), result);
                                                }
                                                else {
                                                    oListItem.set_item($(this).attr('name'), $(this).val());
                                                }
                                                break;
                                            case 'text':
                                                if ($(this).attr('name') == 'Law_x002f_Jurisdiction_x0020_Des') {
                                                    var lawSelection = $('input:radio[name=Law_x002f_Jurisdiction_x0020_Des]:checked').val();
                                                    if (lawSelection == null) {
                                                        lawSelection = $('#ff79{$Pos}_Other_Value').val();
                                                    }
                                                    oListItem.set_item($(this).attr('name'), lawSelection);
                                                }
                                                else {
                                                    oListItem.set_item($(this).attr('name'), $(this).val());
                                                }
                                                break;
                                            case 'number':
                                                if (!isNaN(parseInt($(this).val()))) {
                                                    oListItem.set_item($(this).attr('name'), parseInt($(this).val()));
                                                }
                                                break;
                                            case 'money':
                                                if (!isNaN(parseFloat($(this).val()))) {
                                                    oListItem.set_item($(this).attr('name'), parseFloat($(this).val()));
                                                }
                                                break;
                                            case 'Person':
                                                var user = new SP.FieldUserValue();
                                                user = _spPageContextInfo.userId; // get current user ID
                                                oListItem.set_item('Requestor', user);
                                                break;
                                            case 'date':
                                                if (($(this).val() != null) && ($(this).val() != "") && (isValidDate($(this).val()))) {
                                                    oListItem.set_item($(this).attr('name'), $(this).val());
                                                }
                                                break;
                                            case 'file':
                                                break;
                                            case 'hidden':
                                                break;
                                            default:
                                                oListItem.set_item($(this).attr('name'), $(this).val());
                                                break;
                                        }       // end of switch
                                    }           // end of else for if Law Jurisdiction
                                }
                            });         // end of $(":input").each(function(){

                            oListItem.update();
                            clientContext.executeQueryAsync(
                                    Function.createDelegate(this, addListSuccess),
                                    Function.createDelegate(this, addListFailure)
                                );
                        }           // end of function addCAFForm() {

Open in new window


JavaScript functions that are called as delegates
                        function addListSuccess() {
                            console.log("Success");
                        }       // end of function addListSuccess() {

                        function addListFailure() {
                            console.log("Failure");
                        }           // end of function addListFailure()

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SharePoint

From novice to tech pro — start learning today.