Stupid question time: ``` // For each exp...
# suitescript
g
Stupid question time:
Copy code
// For each expense line, if cseg_comseg has a value, copy it to custcoll_gnt_comm_rqst
          for (var i = 0; i < context.newRecord.getLineCount({ sublistId: 'expense' }); i++) {
              var commNo = context.newRecord.getSublistText({ sublistId: 'expense', fieldId: 'cseg_comseg', line: i });
              if (commNo) {
                  context.newRecord.setSublistText({ sublistId: 'expense', fieldId: 'custcoll_gnt_comm_rqst', text: commNo, line: i });
              }
          }
Error: Invalid custcoll_gnt_comm_rqst reference key COM53. We're replacing a custom field with a custom segment, and I've got both lists. using the same values but different IDs, hence the setSublistText. The custom field is filtered, but this IS a valid value. Why is it giving me the error?
a
you said you're replacing a custom field with a custom segment... but your code is getting
cseg
text and setting
custcol
text which implies you're going the other way?
wouldn't a CSV import be preferable to a suitescripted solution here?
g
the accounting folks are switching to a new external expense system
so it's pulling from the purchase orders and writing to the vendor bills
when the integration writes to the segment on vendor bills, it needs to also update the legacy field
a
oh what middleware are you using?
g
I switched to using an ID lookup and it says the column field is null...
Esker
oh, no middleware, they're building their own integration
a
... never heard of it... anyway yeah integrations with customsegments are oftent a nightmare
g
problem is the field is filtered by a field on the custom record it's sourced from
but. the segment doesn't have that same filtering
however, even when I use an ID or value that's in the filtered list, it still throws the error
is it just not possible to setSublistValue or setSublistText on a sourced/filtered list/record column?
a
so this what I used for boomi to set custom segments created my own passthru text fields where i set the value to the cseg internal id i needed and then just this beforesubmit UE takes the values from the passthru text fields and sets it on the appropriate cseg field
Copy code
const beforeSubmit = (scriptContext) => {
            if (scriptContext.type !== 'create' && scriptContext.type !== 'edit')
                return;
            log.debug({title: 'set custom segments', details:' *** START ***'});
            const sublistObj = {
                journalentry: 'line',
                invoice: 'item',
                creditmemo: 'item',
                vendorbill: 'expense',
                vendorcredit: 'expense'
            };

            const rec = scriptContext.newRecord;
            const sublistId = sublistObj[rec.type];

            const lineCount = rec.getLineCount({ sublistId });

            for( let i = 0; i < lineCount; i++ ) {
                const opUnit = rec.getSublistValue({
                    sublistId,
                    fieldId: 'custcol_cp_opunit_passthru',
                    line: i
                });
                log.debug('opUnit',opUnit);
                const activity = rec.getSublistValue({
                    sublistId,
                    fieldId: 'custcol_cp_activity_passthru',
                    line: i
                });
                log.debug('activity',activity);
                try {
                    if (opUnit) {
                        rec.setSublistValue({
                            sublistId,
                            fieldId: 'cseg_npo_region',
                            value: opUnit,
                            line: i
                        });
                    }
                    if (activity) {
                        rec.setSublistValue({
                            sublistId,
                            fieldId: 'cseg_paactivitycode',
                            value: activity,
                            line: i
                        });
                    }


                } catch (e) {
                    log.error({
                        title: 'error',
                        details: e
                    });
                }
            }
        }

        return {beforeSubmit}
I'm not sure if your issue is related to the get/set text vs. value or the fact that csegs are actually custom records as well, and the lookups for them are a little weird... or both 😄
g
Nevermind, I had a typo in my set value lookup
a
ROFL
g
for got the [0] on search.lookupFields
👍 1
so it returned null even though there was a result
Copy code
// For each expense line, if cseg_comseg has a value, copy it to custcoll_gnt_comm_rqst
      for (var i = 0; i < context.newRecord.getLineCount({ sublistId: 'expense' }); i++) {
        var commNo = context.newRecord.getSublistValue({ sublistId: 'expense', fieldId: 'cseg_comseg', line: i });
        // Get the segment's related commitment number
        const relatedField = search.lookupFields({
          type: 'customrecord_cseg_comseg',
          id: commNo,
          columns: ['custrecord_comseg_related']
        });

        const relatedNum = relatedField.custrecord_comseg_related[0] ? relatedField.custrecord_comseg_related[0].value : null;
        log.debug('commNo', commNo);
        log.debug('relatedField', JSON.stringify(relatedField));
        log.debug('relatedNum', relatedNum);


        if (commNo) {
          context.newRecord.setSublistValue({ sublistId: 'expense', fieldId: 'custcoll_gnt_comm_rqst', value: relatedNum, line: i });
        }
      }
works now!
Sometimes all you need is to say things out loud to another programmer to get the answer lol
n
Just seen this thread and I'm surprised it wasn't: "custcoll_gnt_comm_rqst" should have been "custcol_gnt_comm_rqst" <-- single "l" in custcol 🙃