Update Parent record according to child field Status

Jun 27, 2013 at 8:48 AM
HI.
I m new babiee to SP Service. But have worked hard to get the start up.

I have 2 lists, the parent list (Issue) and child list(Time). The parent list can have one or many child. There is a field on Child called ReleaseStatus which can be set by user manually as “In-Progress”, “On-Hold”, “Ready to Release”.
Also I have 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._

If tried with adding code in content editor of list and on clicking of button it checks for each status and update correctly... But now i wanted to have this code in PreSaveAction on EditForm.aspx but the code now runs in differnt flow.. can any body pls help me.

Here is the code:-

<SNIP>

<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>

</SNIP>

can anybody pls guilde me for running this code corerctly in presave function.

Thanks and Regards,
Deepak
Coordinator
Jun 27, 2013 at 4:36 PM
Deepak:

You're showing a lot of code and it's not clear where you're having an issue.

I'd suggest that you upgrade from v0.5.8 to the latest 2013.01 as it is a lot more efficient and I've squashed many bugs since v0.5.8.

M.
Jun 28, 2013 at 9:33 AM
HI,, Can you please provide me an link to download teh latest 2013.01 file.

Regards,
Deepak
Jun 28, 2013 at 10:15 AM
Hi Deepak,

You can get the download file for 2013.01 from here: https://spservices.codeplex.com/releases/view/92552.
Jun 28, 2013 at 10:22 AM
Hi.. I m able to download and run the code and it run successfully... Thks for the response.

Also I wanted to check this fields values after save button.. currently i m checking during PreSaveItem function.. Is there any function in which i wanted to check after save event say call function.. tryied with PostSaveItem but the code is not running..

Regards,
Deepak
Coordinator
Jun 28, 2013 at 4:10 PM
There's no PostSaveItem function. After the save, the user is redirected to the Source query string location or the default view of the list.

M.
Jul 2, 2013 at 9:06 AM
Thks for the Reply.
My code is working fine.. but only issue is the during presaveItem function the status which was ealier saved in list is populating the value. i wanted to check for current value which is saving in list.

suppose foe example the CRStatus = "In-Progress" and user edits and inputs new value of CRStatus = "Released" then in Save Function it should give me value as "Released" but somehow it is giving value as "In-Progress"..

Any helps pls..

Regards,
Deepak
Coordinator
Jul 2, 2013 at 11:31 AM
Deepak:

I'm really not sure what you are trying to do. If the user changes a value on the form and hits the Ok button, those changes should be saved. It sounds like you've inserted some code into the process somewhere which is breaking things.

M.
Jul 3, 2013 at 6:22 AM
Let me explain once more time.. I have requirement of during save if user edit and inputs values of one of field called CRSTATUS="Released" and if all the releated CRSTATUS field is also "Released" then update the parent record as "Released".. So i have written code for the same in PreSaveItem function.

in my code it is giving CRSTATUS = "In-Progress" during PreSaveFunction. I Wanted to check this status after the save function. in PreSaveItem it should give me value as currentvalue which will save not the previous value.

Let me know if i m able to explain or needed more explaination.

Regards,
Deepak
Coordinator
Jul 3, 2013 at 11:19 AM
Edited Jul 9, 2013 at 3:14 AM
Whatever you're doing to look at the value of CRSTATUS in the PreSaveAction must not be what you actually want. Can you show your code?

M.
Jul 3, 2013 at 1:33 PM
Edited Jul 3, 2013 at 1:34 PM
Yes you are correct. i wanted the value of CRSTATUS which is going to be saved and not previous values. here is my partial code..


<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-2013.01.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{
 alert("calling Presaveitem Function" ) ;   

 issueIDRec[0] = 21;
$().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..
        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.
}

}</script>
Jul 3, 2013 at 6:23 PM
I think you need to get the value of the CR-Status from the screen:
 var value = $("[title='CR-Status'] option:selected").text();
Jul 5, 2013 at 6:38 AM
Thanks.. that solved my issue in development environment for sample list testing..