Update child record field into the parent record field

The field in the parent record needs to be updated when the value in the child record is updated everytime.

First created a search to get the latest record. Here we are considering the latest record only when applying. The script is applied to the child record. When every time the value is updated in the child record, It gets populated into the parent record.

Here In example a status value is updating.


define(['N/search','N/record'],
    /**
     * @param{search} search
     * @param{record} record
     */
    (search,record) => {
        /**
         * Defines the search for latest allocation child record status value
         */
        const checkLatestAllocationStatus = (assetAllocationRecordId) => {
            try{
                var customrecord_asset_allocationSearchObj = search.create({
                    type: "customrecord_asset_allocation",
                    filters:
                        [
                        ],
                    columns:
                        [
                            search.createColumn({name: "internalid", label: "Internal ID"}),
                            search.createColumn({
                                name: "created",
                                sort: search.Sort.DESC,
                                label: "Date Created"
                            })
                        ]
                });
                var internalId;
                customrecord_asset_allocationSearchObj.run().each(function(result){
                    internalId = result.getValue({name: "internalid", label: "Internal ID"});
                });
                return internalId;
            }catch{
                log.debug("Error@latestAllocationStatus search result")
            }
        }
        const afterSubmit = (scriptContext) => {
            try{
                if(scriptContext.type === scriptContext.UserEventType.CREATE || scriptContext.UserEventType.EDIT || scriptContext.UserEventType.COPY) {
                    var assetAllocationRecord = scriptContext.newRecord;
                    var status = assetAllocationRecord.getValue({
                        fieldId: 'custrecord_aa_status'
                    })
                    var assetParentID = assetAllocationRecord.getValue({
                        fieldId: 'custrecord_fam_asset'
                    })
                    var checkLatestRec = checkLatestAllocationStatus(assetAllocationRecord.id);
                    if (status) {
                        record.submitFields({
                            type: "customrecord_ncfar_asset",
                            id: assetParentID,
                            values: {
                                'custrecord_fam_aa_status': status
                            },
                        });
                    }
                }
            }catch{
                log.debug("Error@afterSubmit")
            }
        }
        return { afterSubmit}

    });

Leave a comment

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