Update Parent record according to child status

Jun 11, 2013 at 12:11 PM
Edited Jun 11, 2013 at 12:12 PM
______Hi guys, I have a complex situation that I was wondering if you guys have an idea on how to do:

I have 2 lists, the parent list (CRDB) and child list(ReleaseDB). The parent list can have one or many child. There is a option field on Child called ReleaseStatus which can be set by user manually as “In-Progress”, “On-Hold”, “Ready to Release”. Also I have option field on Parent list called CRStatus which can be also set by user manually as “In-Progress”, “On-Hold”, “Ready to Release”.

Now So let says parent A have 3 child record (X,Y and Z) and in that suppose user is updating all 3 child ReleaseStatus as “Ready to Release” then update parent CRStatus as “Ready to Release” else do nothing..if say any ReleaseStatus is not Ready to Release then do nothing._

Also i m new to SP service. i have tried simple button in display form which wil check all child status and update parent status but i needed this code to be run automically during edit of child records.

Hopefully this explaination of my problem is clear enough. Please let me know if you need any clarification, all help are appreciated, thanks!

Regards,
Deepak
Jun 11, 2013 at 12:41 PM
You want to include a function called "PreSaveAction()" on the EditForm.aspx. That function name is recognized by SharePoint as one that will run after the "OK" button is clicked but before the form data is saved. In the function, after you run your list of instructions, you have to return true at the end to indicate to SharePoint that it's OK to proceed with the form submit/save action. For instance....
function PreSaveAction() {
    try {
        //Do some things here. If everything works, return true
        return true;    //Continues the form submit action
    }
    catch(err) {
        alert(err.message);
        return false;   //Cancels the form submit action
    }
}
Jun 12, 2013 at 4:49 AM
Tnks for the response. I have already written function of PreSaveAction which wil validate few fields according to condition.
Now issue is i m unable to check for status of all child records and if it is ready to release then update Parent record as Ready to Release.. Can u pls send me code for the same.

Thanks and Regards,
Deepak
Coordinator
Jun 12, 2013 at 5:18 AM
Deepak:

It's impossible to "send me code for the same" because your particular needs are unique. You're going to have to write it yourself.

M.
Jun 12, 2013 at 5:50 AM
Thks for the reply. yes i can write or change the code myself. can you guilde me how to start or have pseudo code or can you send me few code which is similar to my requirement. I have tried with button of Parent child which will check all related child record and if yes then update parent status.. i wanted to automatically check during any child record save. check all the releated child and if yes then update parent status.
Coordinator
Jun 12, 2013 at 9:13 PM
Take a look through the examples here in the discussions. There's lots of stuff that shows GetListItems in action.

M.
Jun 14, 2013 at 7:34 AM
Thks for the info..
I tried with few of sample and googling for SPServices..
I was able to succussly change the status from child list by adding content editor web part and in that had button and after click it calls the functions. Manually after clicking on button the code works fine.
here is the code...
<script src="/sites/QualityDev/Style%20Library/js/jquery.min.js" type="text/javascript"></script><script src="/sites/QualityDev/Style%20Library/js/jquery.SPServices-0.5.8.js" type="text/javascript"></script><script type="text/javascript">

// Global variale defined for checking of update to do or not.
var statuscheck = "True" ;
var recfound = "False" ;

function UpdateparentRec()
{
// Get the Query String values and split them out into the vals array
var vals = new Object();
var qs = location.search.substring(1, location.search.length);
var args = qs.split("&");
for (var i=0; i < args.length; i++) {
    var nameVal = args[i].split("=");
    var temp = unescape(nameVal[1]).split('+');
    nameVal[1] = temp.join(' ');
    vals[nameVal[0]] = nameVal[1];
}
var childID = vals["ID"];
alert("Child id found is " + childID);

var issueIDRec = [];

$().SPServices({        

    operation: "GetListItems",
    listName: "Time",
    CAMLViewFields: "<ViewFields><FieldRef Name='IssueID' /><FieldRef Name='ID' /></ViewFields>",
    CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID'/><Value Type='Integer'>" + childID + "</Value></Eq></Where></Query>",
    completefunc: function (xData, Status) 
    {               

        $(xData.responseXML).find("[nodeName=z:row]").each(function() 
        {   
            var issueID = $(this).attr("ows_IssueID");
            //alert(issueID) ;

            issueIDRec = issueID.split(";");                
            alert("in UpdateparentRec Function of SPService IssueID is " + issueIDRec[0]);

        });

        //ALERT("CALLING FINDISSUEID") ;
        FindIssueID(issueIDRec[0]);
    }

}); 
}

function FindIssueID(childidRec){
if (!childidRec) {
    alert("FindIssueID(ERROR): No childidRec defined as input param!");
}

$().SPServices({    

    operation: "GetListItems",
    listName: "Time",
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='IssueID' /><FieldRef Name='ID' /><FieldRef Name='ReleaseStatus' /></ViewFields>",
    CAMLQuery: "<Query><Where><Eq><FieldRef Name='IssueID'/><Value Type='Integer'>" + childidRec + "</Value></Eq></Where></Query>",
    completefunc: function (xData, Status) {                

        $(xData.responseXML).find("[nodeName=z:row]").each(function() 
        {
            recfound = "True" ;

            var title = $(this).attr("ows_Title");                
            alert(title);

            var releasestatus = $(this).attr("ows_ReleaseStatus");
            alert(releasestatus) ;

            if(releasestatus != "Ready to Release")
            {
                statuscheck = "False" ;
            }

         });

        // check if all ready to release and alteast one record found..
        //alert("Release status after loop is " +statuscheck) ;
        //alert("alteast one record found status is " +recfound) ;          
        //alert("in FindIssueID Function IssueID found for parent update is " + childidRec);

        if (statuscheck == "True" && recfound == "True")
        {
              UpdateStatus(childidRec);
        }

    }       
});    
}

function UpdateStatus(itemId){
if (!itemId) {
    alert("UpdateStatus(ERROR): No itemId defined as input param!");
}

$().SPServices({
    operation: "UpdateListItems",
    listName: "Issue",
    ID: itemId,   
    valuepairs: [["CRStatus", "Released"]],    
    completefunc: function (xData, Status){

        alert("Update Done");

    }
});
}</script><input onclick="javascript:UpdateparentRec();" type="button" value="Update parent record!"/>


Now i wanted to have this code updated in save of child list. But some how the code is now working correctly. It doesnot give any error but the flow of logic is irregular.
here is code during edit of child list,. in this too added content editor web part and code below...

<script language="javascript" src="/sites/QualityDev/Style%20Library/js/jquery.min.js" type="text/javascript"></script><script language="javascript" src="/sites/QualityDev/Style%20Library/js/jquery.SPServices-0.5.8.js" type="text/javascript"></script><script language="javascript" type="text/javascript">

// Global variale defined for checking of update to do or not.
var statuscheck = "True" ;
var recfound = "False" ;
var issueIDRec = [];

function PreSaveItem()
{

try{
// do something here...if everthing works fine, return true.     
alert("calling Presaveitem Function" ) ;    
// Get the Query String values and split them out into the vals array
var vals = new Object();
var qs = location.search.substring(1, location.search.length);
var args = qs.split("&");
for (var i=0; i < args.length; i++) {
    var nameVal = args[i].split("=");
    var temp = unescape(nameVal[1]).split('+');
    nameVal[1] = temp.join(' ');
    vals[nameVal[0]] = nameVal[1];
}
var childID = vals["ID"];
alert("Child id found is " + childID);  

$().SPServices({        

    operation: "GetListItems",
    listName: "Time",
    CAMLViewFields: "<ViewFields><FieldRef Name='IssueID' /><FieldRef Name='ID' /></ViewFields>",
    CAMLQuery: "<Query><Where><Eq><FieldRef Name='ID'/><Value Type='Integer'>" + childID + "</Value></Eq></Where></Query>",
    completefunc: function (xData, Status) 
    {               

        $(xData.responseXML).find("[nodeName=z:row]").each(function() 
        {   
            var issueID = $(this).attr("ows_IssueID");
            //alert(issueID) ;

            issueIDRec = issueID.split(";");                
            alert("in UpdateparentRec Function of SPService IssueID is " + issueIDRec[0]);

        });

        //ALERT("CALLING FINDISSUEID") ;
        //FindIssueID(issueIDRec[0]);   

    }
}) ;

$().SPServices({    

operation: "GetListItems",
listName: "Time",
CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='IssueID' /><FieldRef Name='ID' /><FieldRef Name='ReleaseStatus' /></ViewFields>",
CAMLQuery: "<Query><Where><Eq><FieldRef Name='IssueID'/><Value Type='Integer'>" + issueIDRec[0] + "</Value></Eq></Where></Query>",
completefunc: function (xData, Status) {                

    $(xData.responseXML).find("[nodeName=z:row]").each(function() 
    {
        recfound = "True" ;

        var title = $(this).attr("ows_Title");                
        alert(title);

        var releasestatus = $(this).attr("ows_ReleaseStatus");
        alert(releasestatus) ;

        if(releasestatus != "Ready to Release")
        {
            statuscheck = "False" ;
        }

    });

        // check if all ready to release and alteast one record found..
        //alert("Release status after loop is " +statuscheck) ;
        //alert("alteast one record found status is " +recfound) ;          
        //alert("in FindIssueID Function IssueID found for parent update is " + issueIDRec[0]);

        if (statuscheck == "True" && recfound == "True")
        {
              UpdateStatus(issueIDRec[0]);
        }

    }       
}); 

return true ;      // continues the form submit button.
}

catch(err){
 alert(err.message) ;
 return false ;   // cancel the form submit button.
}
}

function UpdateStatus(itemId){
if (!itemId) {
    alert("UpdateStatus(ERROR): No itemId defined as input param!");
}

$().SPServices({
    operation: "UpdateListItems",
    listName: "Issue",
    ID: itemId,   
    valuepairs: [["CRStatus", "Released"]],    
    completefunc: function (xData, Status){

        alert("Update Done");

    }
});
}</script>

Can someone pls guilde me which steps i have missed..

Regards,
Deepak