Hi Everyone, I have a custom field on the Item rec...
# suitescript
v
Hi Everyone, I have a custom field on the Item record that is set to Inline HTML and is read-only (so I am not storing any value in it). I want to display a value in this field dynamically — this value will come from another record after the Item record is loaded. My question is: 👉 Is there any way to show this value in the Inline HTML field in View Mode of the Item record using scripting (User Event, Client Script, or any other approach)? I’ve tried to find a way, but so far I haven’t been able to make the field display a dynamic value when the Item record is simply opened in View Mode. Any guidance or suggestions would be really helpful.
d
Can you just set up sourcing for this field?
v
But how can I do that? The value I want to display in that field needs to be fetched from another custom record based on certain conditions — for example, if the internal ID of the item record matches the item ID column value in the custom record, then I need to retrieve the value of a specific field from that custom record and set it on the custom field of the item record. However, I’m not finding any reference to that custom record in the sourcing options.
d
Oh, yes, conditions are not supported. Then you can try UserEvent beforeLoad and set value onto form
v
Yes, I’ve tried using a User Event script, but I’m not able to see the changes to the custom field on the item record while in View mode. I have to manually edit and save the record to see the update, at least the first time.
d
Ok, I think I got it. It's a bad habit to try to update the record while in View mode. Consider a schedule or mapreduce to just update the field properly for those records where it's not yet set
v
I just want to confirm that it is not possible to set a field in NetSuite while the record is in View mode, even if the field is read-only and does not actually store any value.
d
If you need to display it temporary only when someone is looking at the form, then you can try to set it on the form (not the record itself). If you need to update it the record - then it's better to use a scheduled script
v
Copy code
/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(['N/search', 'N/log'], function(search, log) {

    function beforeLoad(context) {
        var rec = context.newRecord;

        // Current Item Internal ID
        var itemInternalId = rec.id; 
        log.debug('Current Item Internal ID', itemInternalId);

        // Search in custom record for this item
        var customRecSearch = search.create({
            type: 'customrecord_item_aa',
            filters: [
                ['custrecord_itemid', 'is', itemInternalId] // Match by internal ID
            ],
            columns: ['custrecord_item_avg_a']
        });

        var result = customRecSearch.run().getRange({ start: 0, end: 1 });
        log.debug('Search Result Length', result.length);

        if (result.length > 0) {
            var avgAging = result[0].getValue('custrecord_item_avg_a');
            log.debug('Average Aging from Custom Record', avgAging);

            // Set values in following mode : (VIEW, EDIT, CREATE)
            rec.setValue({
                fieldId: 'custitem_cal_aa',
                value: avgAging
            });
            log.debug('Value Set on Item Record Field', 'Average Aging: ' + avgAging);
        } else {
            log.debug('No Custom Record Found', 'Item Internal ID: ' + itemInternalId);
        }
    }

    return {
        beforeLoad: beforeLoad
    };
});
this is the script I am using , could you please suggest the change ?
d
rec.setValue you changing the record. Change the form instead
v
@Dmitry Masanov Thank you so much Brother. It really helps me a lot.
🙌 1