You are viewing limited content. For full access, please sign in.

Question

Question

Obtaining previous version of value

asked on August 22, 2016 Show version history

Using Workflow, I want to update a field with the previous value of the field.  Based on searching of answers.laserfiche.com I found the only way to do this was though scripting but I was not able to find any example scripts.  The code I came up with is below...

Is this the correct way to get the version just prior to the latest version or is there a better way?   (NOTE:  I had to skip two because the latest field change has already been logged as a new version, so the previous value is actually in the version 2 back).

 

DocumentInfo doc = this.BoundEntryInfo as DocumentInfo;
FieldValueCollection currentValues = doc.GetFieldValues();
var vh = doc.GetVersionHistory().OrderByDescending(v => v.Version).Skip(2).FirstOrDefault();
var fieldValue = vh.GetFieldValue(PAYABLE_STATUS_FIELDNAME);
this.SetToken("OldStatusValue",fieldValue);

 

0 0

Replies

replied on August 23, 2016

The code looks good, might want to add some error handling to deal with deleted versions and the case where there isn't an older version:

foreach (DocumentVersion ver in theDoc.GetVersionHistory().OrderByDescending(v => v.Version).Skip(2))
{
    if (ver.IsExpunged)
        continue; // deleted version, skip

    var fieldValues = ver.GetFieldValues();
    object fieldValue;
    if (fieldValues.TryGetValue(PAYABLE_STATUS_FIELDNAME, out fieldValue) && fieldValue != null)
        this.SetToken("OldStatusValue", fieldValue);

    break;
}

Skipping two versions seems like it could cause problems later, another way would be to compare the timestamps on each version to find the previous one.

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.