Flow Action updating fields, but not updating in Work notes

matthew_hughes
Kilo Sage

I'm creating a Flow Action that will update the fields as well as the work notes based on the selected options from a catalogue item.

I'm using the following script:


(function execute(inputs, outputs) {
    try {
        // Optional: initialise outputs to safe defaults
        outputs.process_successful = false;
        outputs.process_message = '';

        var anyUpdate = false;

        // Get business application record
        var grRecord = new GlideRecord('cmdb_ci_business_app');
        if (!grRecord.get(inputs.strAppSysID)) {
            outputs.process_successful = false;
            outputs.process_message = 'Record not found for sys_id: ' + inputs.strAppSysID;
            return;
        }

        // Only update the values that were selected
        if (inputs.blnUpdateBusinessOwner) {
            grRecord.u_business_owner = inputs.strBusinessOwner; // sys_id for reference
            anyUpdate = true;
        }
        if (inputs.blnUpdateBusinessOwnerRepresentative) {
            grRecord.u_business_owner_representative = inputs.strBusinessOwnerRepresentative; // sys_id
            anyUpdate = true;
        }
        if (inputs.blnUpdateTechnicalOwner) {
            grRecord.u_custodian = inputs.strTechnicalOwner; // sys_id
            anyUpdate = true;
        }
        if (inputs.blnUpdateTechnicalOwnerRepresentative) {
            grRecord.u_cmdb_data_steward = inputs.strTechnicalOwnerRepresentative; // sys_id
            anyUpdate = true;
        }

        if (inputs.blnUpdateBusinessOwningPlatformLab) {
            grRecord.u_business_owning_business_unit = inputs.strBusinessOwningBusinessUnitFunction; // ref/sys_id
            grRecord.u_business_owning_division = inputs.strBusinessOwningPlatform; // ref/sys_id

            if (inputs.strBusinessOwningLab) {
                grRecord.department = inputs.strBusinessOwningLab; // sys_id
            } else {
                grRecord.department = inputs.strBusinessOwningPlatform; // sys_id fallback
            }
            anyUpdate = true;
        }

        if (inputs.blnUpdateTechnicalOwningPlatformLab) {
            grRecord.u_accountable_it_division = inputs.strTechnicalOwningPlatform; // ref/sys_id
            grRecord.u_accountable_it_business_unit = inputs.strTechnicalOwningBusinessUnitFunction; // ref/sys_id

            if (inputs.strTechnicalOwningLab) {
                grRecord.u_accountable_it_organisation = inputs.strTechnicalOwningLab; // ref/sys_id
                grRecord.u_accountable_it_team = inputs.strTechnicalOwningLab; // ref/sys_id
            } else {
                grRecord.u_accountable_it_organisation = inputs.strTechnicalOwningPlatform; // ref/sys_id
                // leave team untouched if not provided
            }
            anyUpdate = true;
        }

        if (!anyUpdate) {
            outputs.process_successful = true;
            outputs.process_message = 'No Ownership fields were selected for update.';
            return;
        }

        // Update the record without running business rules/workflows
        grRecord.setWorkflow(false);
        grRecord.update();

        // Initialise the audit string BEFORE concatenation
        var strWorknoteAudit = '';

        // Business ownership section
        if (inputs.blnUpdateBusinessOwner || inputs.blnUpdateBusinessOwnerRepresentative || inputs.blnUpdateBusinessOwningPlatformLab) {
            strWorknoteAudit += 'The following Business Ownership fields were updated:\n';
        }
        if (inputs.blnUpdateBusinessOwner) {
            strWorknoteAudit += '- Business Owner: ' + grRecord.u_business_owner.getDisplayValue() + '\n';
        }
        if (inputs.blnUpdateBusinessOwnerRepresentative) {
            strWorknoteAudit += '- Business Owner Representative: ' + grRecord.u_business_owner_representative.getDisplayValue() + '\n';
        }
        if (inputs.blnUpdateBusinessOwningPlatformLab) {
            strWorknoteAudit += '- Business Owning Organisation: ' + grRecord.department.getDisplayValue() + '\n';
            strWorknoteAudit += '- Business Owning BU: ' + grRecord.u_business_owning_business_unit.getDisplayValue() + '\n';
            strWorknoteAudit += '- Business Owning Platform: ' + grRecord.u_business_owning_division.getDisplayValue() + '\n';
        }

        // Technical ownership section
        if (inputs.blnUpdateTechnicalOwner || inputs.blnUpdateTechnicalOwnerRepresentative || inputs.blnUpdateTechnicalOwningPlatformLab) {
            // Add a separating newline only if something already exists
            if (strWorknoteAudit) strWorknoteAudit += '\n';
            strWorknoteAudit += 'The following Technical Ownership fields were updated:\n';
        }
        if (inputs.blnUpdateTechnicalOwner) {
            strWorknoteAudit += '- Technical Owner: ' + grRecord.u_custodian.getDisplayValue() + '\n';
        }
        if (inputs.blnUpdateTechnicalOwnerRepresentative) {
            strWorknoteAudit += '- Technical Owner Representative: ' + grRecord.u_cmdb_data_steward.getDisplayValue() + '\n';
        }
        if (inputs.blnUpdateTechnicalOwningPlatformLab) {
            strWorknoteAudit += '- Accountable Tech Organisation: ' + grRecord.u_accountable_it_organisation.getDisplayValue() + '\n';
            strWorknoteAudit += '- Accountable Tech BU: ' + grRecord.u_accountable_it_business_unit.getDisplayValue() + '\n';
            strWorknoteAudit += '- Accountable Tech Platform: ' + grRecord.u_accountable_it_division.getDisplayValue() + '\n';
            strWorknoteAudit += '- Accountable Tech Team: ' + grRecord.u_accountable_it_team.getDisplayValue() + '\n';
        }

        // Finalise outputs
        outputs.process_successful = true;
        outputs.process_message = strWorknoteAudit || 'Updates applied, but no audit details were generated.';

    } catch (myError) {
        var msg = (myError && myError.message) ? myError.message : (myError + '');
        outputs.process_successful = false;
        outputs.process_message = 'Record update failed with error: ' + msg;
    }
})(inputs, outputs);
 
I'm calling my action via a subflow:
matthew_hughes_0-1767784070848.png

 

Both of these Actions will update some fields and work notes on a business application. However, what I've noticed is that it will update the work notes from the first Action, but not the second one.  When I check the flow context for my second Action, the results appear correctly in the process_message:

matthew_hughes_1-1767784183141.png

 

 I've defined my outputs within my script:
matthew_hughes_2-1767784219902.png

 

matthew_hughes_3-1767784249195.png

 

 

So I'm not too sure why I can't see anything in the work notes of a business application.

 
1 ACCEPTED SOLUTION

matthew_hughes
Kilo Sage

I've fixed my own issue. I forgot to add the message output to my subflow

View solution in original post

3 REPLIES 3

Singh3
Tera Expert

In the current script, work notes are not being updated on the record.

You are building the audit information in the variable strWorknoteAudit, but that value is only assigned to outputs.process_message. This means the text is returned to the Flow, not written to the cmdb_ci_business_app record.

There is no assignment to the work_notes field, for example:
grRecord.work_notes = strWorknoteAudit;
grRecord.update();

To update work notes, you need to explicitly set it on the GlideRecord before calling update().

Hope this clarifies the behavior.
If this helps, please consider marking it as Helpful or Accept as Solution so it can help others as well.

matthew_hughes
Kilo Sage

I've fixed my own issue. I forgot to add the message output to my subflow

TejasSN_LogicX
Tera Contributor

Hi @matthew_hughes  ,

 I checked your script — it looks great overall.
Just one suggestion: inside your Flow Action script, before calling grRecord.update(), append the audit string to the work_notes field like this:

 

if (strWorknoteAudit) {
grRecord.work_notes = (grRecord.work_notes || '') + '\n' + strWorknoteAudit;
}

 

 

I think grRecord.update() only updates fields and doesn’t automatically append strWorknoteAudit to the work notes.

Everything else in your script looks perfect!