I am trying to loop through the Address workbook a...
# suitescript
m
I am trying to loop through the Address workbook and retrieve the country value when the address has the default shipping true. alert 1 doesn't reach so the addressSubrecord bit is likely an issue, but not sure what:
Copy code
function saveRecord(context) {
    log.debug({title: "Context Save", details: context.mode});
    try {
        const currentRecord = context.currentRecord;
        const addressSublistCount = currentRecord.getLineCount({
            sublistId: 'addressbook'
        });
        alert("Number of Address Found: " + addressSublistCount);
        for (let i = 0; i < addressSublistCount; i++) {

            const defaultShipping = currentRecord.getSublistValue({
                sublistId: 'addressbook',
                fieldId: 'defaultshipping',
                line: i
            });
            alert("Default Shipping Found? " + defaultShipping);
            if (defaultShipping === true) {
                alert("Found a Default Shipping - Get Country Value " + i);
                const addressSubrecord = currentRecord.getSublistSubrecord({
                    sublistId: 'addressbook',
                    fieldId: 'addressbookaddress',
                    line: i
                });
                alert("1");
                const countryValue = addressSubrecord.getValue({
                    fieldId: 'country'
                });
                alert("2");
                dialog.alert({
                    title: 'Default Shipping Address Found',
                    message: 'Country for Default Shipping Address: ' + countryValue
                });
            }
        }
    } catch
        (e) {
        log.debug({title: e.title, details: e.message});
    }
    return true;
}
k
you might try logging/alerting the actual result from getSublistValue
it might be returning something like
"T"
instead of
true
; actually displaying the values will help you identify if that's the case, or if it's failing to return any value etc
t
@mg2017 for me a few things you might want to consider.. 1. getSublistRecord works in isDynamic false, you might have better luck with getCurrentSublistSubrecord 2. this code from what you're doing might be better off serverside in a aftersubmit UE because there are usually problems with subrecord problems before the initial record is created
Copy code
require(['N/record','N/runtime','N/search','N/format','N/currentRecord'], function(record,runtime,search,format, cr) {
  try{
    const customerId = 323481;

    var currentRecord = record.load({
        type: record.Type.CUSTOMER,
        id: customerId,
        isDynamic: true
    });

    //var currentRecord = cr.get();

        const addressSublistCount = currentRecord.getLineCount({
            sublistId: 'addressbook'
        });
        for (let i = 0; i < addressSublistCount; i++) {

            const defaultShipping = currentRecord.getSublistValue({
                sublistId: 'addressbook',
                fieldId: 'defaultshipping',
                line: i
            });
            currentRecord.selectLine({
                sublistId: "addressbook",
                line: i
            });
            if (defaultShipping === true) {
                var addressSubrecord = currentRecord.getCurrentSublistSubrecord({
                    sublistId: 'addressbook',
                    fieldId: 'addressbookaddress',
                });
                const countryValue = addressSubrecord.getValue({
                    fieldId: 'country'
                });
            }
        }

  } catch (e) {
    log.error("ERROR:", JSON.stringify({type: e.type,name: e.name,message: e.message,stack: e.stack,cause: JSON.stringify(e.cause),id: e.id}));
  }
});
you can probably run this in the debugger and it'll work
m
I still have more work left on this script. I only used a Client Script to set a field mandatory based on the country value and to alert the user.
Using a User Event script - I think we can throw an error but that would mean the user would need to recreate all the data?
t
depends on what you're trying to do i guess, anyway I wanted to just highlight that subrecord manipulation in client script on create is a combination thats likely to lead to errors
👍 1