Modify the line level values of a WBS record

The Work Breakdown Structure (WBS) record stores cost and revenue data for each line in an array of objects format when the timeline type is set to “Monthly.”

Example of the serializedamounts field:

serializedamounts:"[{"key":307,"lineKey":5,"type":"Cost","eac":null,"date":"2024-10-01"},{"key":308,"lineKey":5,"type":"Revenue","eac":null,"date":"2024-10-01"},{"key":309,"lineKey":5,"type":"Cost","eac":null,"date":"2024-11-01"},{"key":310,"lineKey":5,"type":"Revenue","eac":null,"date":"2024-11-01"},{"key":311,"lineKey":5,"type":"Cost","eac":null,"date":"2024-12-01"},{"key":312,"lineKey":5,"type":"Revenue","eac":null,"date":"2024-12-01"}]"

To modify a specific Cost or Revenue value in the WBS record, we must:

  1. Retrieve the existing values for the serializedamounts field.
  2. Update the relevant entries for the required months with the new data.

Steps to Update Cost/Revenue Fields

  • Load or Create the WBS Record

First, load the WBS record associated with the project. If the record doesn’t exist, create it.

  • Retrieve the Existing serializedamounts Field Value

Use the following code to fetch the current value of the serializedamounts field for a specific line:

let existingLineData = wbsRecord.getSublistValue({
                        sublistId: "lines",
                        fieldId: "serializedamounts",
                        line: lineIndex
                    });
  • Parse the Field Value

Convert the serialized JSON string into a JavaScript array of objects for easier manipulation:

let serializedAmounts = JSON.parse(existingLineData);
  • Format the Target Date

Ensure the date is in the format YYYY-MM-01 (the first day of the required month). Example:

let date = year.trim() + "-" + month + '-01';

  • Iterate Through the Array and Update Values

Find the objects with the matching date and type (Cost or Revenue), then update the eac value with the new data:

serializedAmounts.forEach(obj => {
                            if (obj.date === date && obj.type === "Cost") {
                                //setting the cost value for the particular date field
                                obj.eac = parseFloat(cost);
                            }
                            if (obj.date === date && obj.type === "Revenue") {
                                //setting the revenue value for the particular date field
                                obj.eac = parseFloat(revenue);
                            }
                        });
  • Update the Modified Array Back to the Record

Serialize the updated array back into a JSON string and set it as the new value for the field:

wbsRecord.setSublistValue({
                        sublistId: "lines",
                        fieldId: "serializedamounts",
                        line: lineIndex,
                        value: JSON.stringify(serializedAmounts)
                    });
  • Save the WBS Record

Commit the changes by saving the record:

wbsRecord.save();

Leave a comment

Your email address will not be published. Required fields are marked *