Before insert BR

Ayushi Saxena
Tera Contributor
Hi All,
 

Can anyone help me to figure out there is before insert BR to update the shipping instruction field value append with the purchase reason field value.

 

There are 3 different types values in purchase reason field for one of the case it's working fine for 1-2 case it's not working.

 
 
(function executeRule(current, previous /*null when async*/ ) {
    function toTitleCase(str) {
        return str
            .toLowerCase()
            .replace(/[^a-zA-Z0-9 ]/g, '') // Remove special characters but keep spaces
            .replace(/\b\w/g, function(match) {
                return match.toUpperCase(); // Capitalize first letter of each word
            });
    }
    var titleCaseReason = toTitleCase(current.purchase_reason.toString());
    //var originalShippingInstruction = '';
    var originalShippingInstruction = current.u_shipping_instructions.toString();
    //current.u_shipping_instructions = ' ';
    current.u_shipping_instructions = titleCaseReason + ': ' + originalShippingInstruction;
})(current, previous);
4 REPLIES 4

Anil Nemmikanti
Giga Guru

Hi @Ayushi Saxena ,

Please rephrase your problem statement. It is a bit confusing. When exactly it is working and when it is not working? With clear information it would be easier to provide the solution.

@Anil Nemmikanti,

Can you please go through again. I have updated 

Ankur Bawiskar
Tera Patron
Tera Patron

@Ayushi Saxena 

try this and see if purchase reason is null

Is purchase_reason a choice value?

if yes and you want label then use this

current.purchase_reason.getDisplayValue()

(function executeRule(current, previous /*null when async*/) {
    function toTitleCase(str) {
        return str
            .toLowerCase()
            .replace(/[^a-zA-Z0-9 ]/g, '') // Remove special characters but keep spaces
            .replace(/\b\w/g, function(match) {
                return match.toUpperCase(); // Capitalize first letter of each word
            });
    }
    var purchaseReason = current.purchase_reason ? current.purchase_reason.toString() : '';
    var titleCaseReason = toTitleCase(purchaseReason);
    var originalShippingInstruction = current.u_shipping_instructions ? current.u_shipping_instructions.toString() : '';

    // Only update if purchase reason is present
    if (titleCaseReason) {
        current.u_shipping_instructions = titleCaseReason + ': ' + originalShippingInstruction;
    }
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ayushi Saxena 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader