Monday, July 27, 2015

Achieve Composite Unique Key using jQuery SPServices

The below code is to achieve composite unique key for the combination of two fields.

<script language="javascript" type="text/javascript" src="../../SiteAssets/jquery-1.11.1.min.js"></script>
<script language="javascript" type="text/javascript" src="../../SiteAssets/jquery.SPServices-2013.01.js"></script>

<script type="text/javascript">
//Validate for Composite primary key (Sku Number, Serial Number)
        function ValidateData() {        
            var skuSearch = $("#iSku option:selected").val();
            var serialSearch = $("#iSerial").val();
            var revSearch = $("#iRev").val();
            var cond = false;
            $().SPServices({
                operation: "GetListItems",
                async: false,
                listName: "ListName",
                CAMLViewFields: "<ViewFields><FieldRef Name='SkuNum'/><FieldRef Name='SerialNo'/></ViewFields>",
                completefunc: function (xData, Status) {
                    $(xData.responseXML).SPFilterNode("z:row").each(function () {
                        if (skuSearch == $(this).attr("ows_SkuNum") && serialSearch == $(this).attr("ows_SerialNo")) {
                            cond = true;
                        }

                    });
                }
            });
            //If Valid
            if (cond == true) {
                //GetData(skuSearch, serialSearch);//Method to fetch the field values. Not Mentioned in blog
            }
            else {
               // ClearData(); //Method to clear the fields. Not specified in blog.
                alert("Data not found. Please try different combination.");
            }
        }
</script>


If you want to achieve composite unique key for the combination of more than two fields, the above code will help but prefer the below link to modify the CAML query accordingly.
http://aasai-sharepoint.blogspot.in/2013/02/caml-query-with-multiple-conditions.html 

No comments:

Post a Comment