I am creating a select field (address) on a case r...
# suitescript
m
I am creating a select field (address) on a case record via a UE script and populating the values from a saved search in a Client Script > PageInit, this is working fine. When creating a new case and a company is selected, the select field is populated and a free text field is set with the default shipping address. I am having issues trying to update the free text field if the user changes the select field to another address. Code below:
message has been deleted
Copy code
function fieldChanged(context) {
            if (context.fieldId === 'company') {
                var field_changed = context.fieldId;
                var current_record = context.currentRecord;
                var company = current_record.getValue({
                    fieldId: 'company'
                });
                log.debug({title: "Field Changed...", details: context.fieldId})

                if (company) {
                    try {
                        var CUSTOMER = record.load({
                            type: record.Type.CUSTOMER,
                            isDynamic: false,
                            id: company
                        });
                        var address_list = current_record.getField({
                            fieldId: 'custpage_selectfield_dmcs'
                        });
                        log.debug("EMPTY ADDRESS LIST");
                        address_list.removeSelectOption({
                            value: null
                        });
                        var address_details = GetCustomerAddress(CUSTOMER.id, search, current_record, field_changed);
                    } catch (e) {
                        log.error("Catch Block");
                    }
                }
            }
            if (context.fieldId === 'custpage_selectfield_dmcs') {
                var current_record = context.currentRecord;
                var company = current_record.getValue({
                    fieldId: 'company'
                });
                var _field_id = context.fieldId;

                if (company) {
                    try {
                        var CUSTOMER = record.load({
                            type: record.Type.CUSTOMER,
                            isDynamic: false,
                            id: company
                        });
                        var address_details = GetCustomerAddress(CUSTOMER.id, search, current_record, _field_id);
                        log.debug({title: "End of Process", details: address_details});
                    } catch (e) {
                        log.error("Catch Block - Error");
                    }
                }
            }
        }

        function GetCustomerAddress(customer_id, search, current_record, field_changed) {
            var customer_internal_id = customer_id;
            var customer_address_search = search.load({
                id: "customsearch_customer_add_search"
            });
            log.debug({title: "Internal ID Passed to filter", details: customer_internal_id});

            customer_address_search.filters.push(search.createFilter({
                name: 'internalid',
                operator: <http://search.Operator.IS|search.Operator.IS>,
                values: customer_internal_id
            }));

            var myPagedData = customer_address_search.runPaged({
                "pageSize": 1000
            });
            var address;
            log.debug({title: "Address Count", details: myPagedData.count});
            if (myPagedData.count > 0) {
                myPagedData.pageRanges.forEach(function (pageRange) {
                    var myPage = myPagedData.fetch({index: pageRange.index});
                    myPage.data.forEach(function (result) {

                        const address_internal_id = result.getValue({
                            name: 'addressinternalid',
                            summary: 'GROUP',
                        });
                        address = result.getValue({
                            name: 'address',
                            summary: 'GROUP',
                        });
                        const default_shipping_address = result.getValue({
                            name: "isdefaultshipping",
                            summary: "GROUP",
                        })

                        if (field_changed === 'company') {
                            log.debug("Field Changed - Company");
                            if (default_shipping_address === true) {
                                log.debug("Set Default Shipping Address");
                                current_record.setValue({
                                    fieldId: "custevent_case_address",
                                    value: address
                                });
                            }
                            var address_book_field = current_record.getField({
                                fieldId: 'custpage_selectfield_dmcs'
                            });
                            log.debug("Insert Options");
                            address_book_field.insertSelectOption({
                                value: address_internal_id,
                                text: address
                            });
                        }

                        if (field_changed === 'custpage_selectfield_dmcs') {
                            log.debug("Drop Down Change - Update Address");
                            current_record.setValue({
                                fieldId: 'custevent_case_address',
                                value: address
                            })
                        }
                        log.debug({title: "Address", details: address});
                        return true
                    });
                });

            } else {
                log.debug({title: "No Address Found For this Customer", details: customer_internal_id})
            }
        }
b
too much code to start guessing things
what doesnt work / what errors do you get
m
Ah, i realised i didn't specify. When i set the company on the case record, the fieldChanged gets fired. Calls a saved search and gets addresses found for that company. The custom drop down gets populated. A free text field is also set with the default shipping address. If i change the value in the custom drop down, another fieldChanged is fired. I am wanting to update the free text field with the address value selected in the drop down...right now it updates it once (first time round), but no subsequent changes when selecting different drop down options.
b
thats not what your code does
your logic for custpage_selectfield_dmcs is to set to the last address from your search
your code has a lot of duplicated logic for both your cases
for example, it tries to load the customer record despite not using it for either case
your case for custpage_selectfield_dmcs tries to do a customer search, despite having no reason to do so