The CreatorCon Call for Content is officially open! Get started here.

Script Include Help

cnharris1
Kilo Sage

Good afternoon developers,

 

I need some help with an OOB script include: TMFCommonOrderAPIUtil

The backstory is that I'm in the Order Management for Telecom, Media & Tech application. I have a requirement to automatically create a customer account from an incoming payload to ServiceNow.

OOB, ServiceNow requires that the customer account exists in the platform before it will create the order. If the customer account doesn't exist, I'm given this error message: 

cnharris1_0-1699472510012.png

 

Here's the code that I think I need to modify to create the account but so far, I'm not having any luck

mapAccount: function(orderObject) {

        var accountId = '';
        var relParty = orderObject.relatedParty;
        if (!gs.nil(relParty)) {
            for (var i = 0; i < relParty.length; i++) {
                if (relParty[i]['@referredType'] == Constants.CUSTOMER_REFFERED_TYPE) {
                    if (!gs.nil(relParty[i].id)) {
                        var accountGr = this.apiCoreUtil.getRecordBySysIdOrExternalId(relParty[i].id, sn_tmt_core.Constants.TABLE_ACCOUNTS, Constants.EXTERNAL_IDS_TO_LOOKUP);
                        if (!gs.nil(accountGr) && accountGr.next()) {
                            accountId = accountGr.getValue('sys_id');
                        }
                       
                    }
                    return accountId;
                }
            }
        }
        return accountId;
    },



Could someone help me in figuring this out?

 

Best regards,


cnharris1

1 ACCEPTED SOLUTION

harshav
Tera Guru

mapAccount: function(orderObject) {

        var accountId = '';
        var relParty = orderObject.relatedParty;
        if (!gs.nil(relParty)) {
            for (var i = 0; i < relParty.length; i++) {
                if (relParty[i]['@referredType'] == Constants.CUSTOMER_REFFERED_TYPE) {
                    if (!gs.nil(relParty[i].id)) {
                        var accountGr = this.apiCoreUtil.getRecordBySysIdOrExternalId(relParty[i].id, sn_tmt_core.Constants.TABLE_ACCOUNTSConstants.EXTERNAL_IDS_TO_LOOKUP);
 
                        if (!gs.nil(accountGr) && accountGr.next()) {
                            accountId = accountGr.getValue('sys_id');
                        }else if(!accountGr.next()){
                             var gr = new GlideRecord('your_account_table');
                             gr.initialize();
                            //set whatever fields you want to set
                             gr.name = something;
                            accountId = gr.insert(); // this will return sys_id
                         }            
                    }
                    return accountId;
                }
            }
        }
        return accountId;
    }

View solution in original post

2 REPLIES 2

harshav
Tera Guru

mapAccount: function(orderObject) {

        var accountId = '';
        var relParty = orderObject.relatedParty;
        if (!gs.nil(relParty)) {
            for (var i = 0; i < relParty.length; i++) {
                if (relParty[i]['@referredType'] == Constants.CUSTOMER_REFFERED_TYPE) {
                    if (!gs.nil(relParty[i].id)) {
                        var accountGr = this.apiCoreUtil.getRecordBySysIdOrExternalId(relParty[i].id, sn_tmt_core.Constants.TABLE_ACCOUNTSConstants.EXTERNAL_IDS_TO_LOOKUP);
 
                        if (!gs.nil(accountGr) && accountGr.next()) {
                            accountId = accountGr.getValue('sys_id');
                        }else if(!accountGr.next()){
                             var gr = new GlideRecord('your_account_table');
                             gr.initialize();
                            //set whatever fields you want to set
                             gr.name = something;
                            accountId = gr.insert(); // this will return sys_id
                         }            
                    }
                    return accountId;
                }
            }
        }
        return accountId;
    }

Thanks for responding harshav! That code helped me out!