CSM Script Include

tomaslindev
Mega Guru

Hello everyone,

I've been asked to modify the field used to filter the country of operation for maintenance contracts.
The logic for assigning maintenance contracts is found in this script include (ATCAutoCreateWorkOrder).
This is the code:

 

getOperationCountry: function(contract) {
         var grContract = new GlideRecord("ast_contract");
         if (grContract.get(contract)) {
            var country = grContract.u_administrative_data.u_pais_operacion;

           var query = "cmn_location_source=excel^cmn_location_type=country^u_iso_code=" + country + "^ORcountry=" + country;

           var grCountry = new GlideRecord("cmn_location");
            grCountry.addEncodedQuery(query);
            grCountry.query();
            if (grCountry.next())
                return grCountry.getUniqueValue();
            else
                return null;
        }

         return null;
     },

 

The country code of the contract account (Country field) must be obtained, which is populated from the Salesforce Operation Country field. If this field is empty, we would obtain the Operation Country from the administrative data, as is currently the case.

My code implementing these changes is as follows:

 

getOperationCountry: function(contract) {
    var grContract = new GlideRecord("ast_contract");

    if (grContract.get(contract)) {

        var country = null;

        
        var account = grContract.account;
        if (account) {
            var grAccount = new GlideRecord("customer_account");
            if (grAccount.get(account)) {
                var countryFromAccount = grAccount.country;
                if (!gs.nil(countryFromAccount)) {
                    country = countryFromAccount;
                }
            }
        }

        
        if (!country && grContract.u_administrative_data) {
            var adminData = grContract.u_administrative_data.getRefRecord(); 
            if (adminData && !gs.nil(adminData.u_pais_operacion)) {
                country = adminData.u_pais_operacion;
            }
        }

        
        if (country) {
            var query = "cmn_location_source=excel^cmn_location_type=country^u_iso_code=" + country + "^ORcountry=" + country;

            var grLocation = new GlideRecord("cmn_location");
            grLocation.addEncodedQuery(query);
            grLocation.query();
            if (grLocation.next()) {
                return grLocation.getUniqueValue(); 
            }
        }

        return null;
    }

    return null;
},

 

Does anyone see any errors?

Thank you very much and best regards.

 

4 REPLIES 4

Roshnee Dash
Tera Guru

Hi @tomaslindev 
You can add gs.info where ever possible and check. SO that you can get to know from where its breaking.

Your feedback makes the community stronger! If you found this helpful, marking it as the correct answer helps others.
Stay awesome,
Roshnee Dash

Mark Manders
Mega Patron

Are you requesting a code review or is something not working as expected. If it's the latter, it would be good to include the actual outcome.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Sorry for the confusion, I'm requesting a code review.

Ankur Bawiskar
Tera Patron
Tera Patron

@tomaslindev 

try this changes

1) use addQuery() and addOrCondition() instead of encoded query with ^OR for clarity and correctness.

2) use gs.nil() check

getOperationCountry: function(contractSysId) {
    var country = null;
    
    var grContract = new GlideRecord("ast_contract");
    if (!grContract.get(contractSysId)) {
        gs.warn('Contract not found: ' + contractSysId);
        return null;
    }

    // 1. Try to get country from contract account
    var accountSysId = grContract.account;
    if (accountSysId) {
        var grAccount = new GlideRecord("customer_account");
        if (grAccount.get(accountSysId)) {
            var countryFromAccount = grAccount.country;
            if (!gs.nil(countryFromAccount)) {
                country = countryFromAccount;
            }
        }
    }

    // 2. If not found, get from administrative data
    if (!country && grContract.u_administrative_data) {
        var adminData = grContract.u_administrative_data.getRefRecord();
        if (adminData && !gs.nil(adminData.u_pais_operacion)) {
            country = adminData.u_pais_operacion;
        }
    }

    if (!country) {
        gs.warn('No country found for contract: ' + contractSysId);
        return null;
    }

    // 3. Query cmn_location with proper OR condition
    var grLocation = new GlideRecord('cmn_location');
    grLocation.addQuery('cmn_location_source', 'excel');
    grLocation.addQuery('cmn_location_type', 'country');
    var qc = grLocation.addQuery('u_iso_code', country);
    qc.addOrCondition('country', country);
    grLocation.query();

    if (grLocation.next()) {
        return grLocation.getUniqueValue();
    }

    gs.warn('No matching location found for country: ' + country);
    return null;
}

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