Parent Location field should show only Parent Locations

Gagandeep
Tera Contributor

I have a requirement that the incident record producer should have a Parent location and sub location field. The Parent Location field should only show up options which are parent to other locations. I have a script include as below:

 

Name GetParentLocation

API Name :global.GetParentLocation

Application: Global

Client Callable: True

Accessible from : This Application Scope only

 

var GetParentLocation = Class.create();
GetParentLocation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
locationName:function()
    {
     var grLoc = new GlideRecord("cmn_location");
        //grLoc.get("dbf3b4790a0a0a6501a7673fb1b28f7f");
        // grLoc.get("8a3e85f037d0200044e0bfc8bcbe5d0d");
        var result = getChildrenReferenceIds("cmn_location", "parent", grLoc.getUniqueValue());
        gs.info("result = " + result.length + " -- " + result);

        function getChildrenReferenceIds(tableName, parentFieldName, baseRecordId) {
            var grRec = new GlideRecord(tableName);
            if (!grRec.get(baseRecordId)) {
                return false;
            }
            var allChildIds = [];
            getChildIds(baseRecordId);
            return allChildIds;

            function getChildIds(parentRecordId) {
                var childIds = [];
                var grChild = new GlideRecord(tableName);
                grChild.addQuery(parentFieldName, parentRecordId);
                grChild.query();
                gs.info(grChild.getEncodedQuery() + " -- " + grChild.getRowCount());
                while (grChild.next()) {
                    childIds.push(grChild.getUniqueValue());
                    allChildIds.push(grChild.getUniqueValue());
                }
                if (childIds.length == 0) {
                    return false;
                } else {
                    for (var idx = 0; idx < childIds.length; idx++) {
                        var theseChildren = getChildIds(childIds[idx]);
                        if (!theseChildren) {
                            continue;
                        }
                    }
                    gs.info(childIds);
                    return 'sys_idIN' +childIds;
                }
            }
        }

    },
    type: 'GetParentLocation'
});

 

I am calling this script include in my reference qualifier as below:

javascript: "sys_idIN" + new GetParentLocation().locationName(current.variables.location);

 

This results in empty data in Parent Location field.

Please guide what am I doing wrong?

 

Example of the requirement is:

Lets say location name is Vancouver and it is parent to ABC office which in turn is parent to XYZ, FGH and PQR office. I should only be seeing Vancover and ABC in my Parent Location field as they are parent to other locations

2 REPLIES 2

Gagandeep
Tera Contributor

@asifnoor 

Hi,

Few points.

1. When calling another fucntion within SI, brest way to call is
this.getChildrenReferenceIds

and define these outside your fucntion.

2. what is grLoc.getUniqueValue()? you did not query grLoc anywhere at the top of this.

3. If you are returning  return 'sys_idIN' +childIds; like this, then again in ref qualifier, you dont need "sys_idIN" 

4. Is this exactly the filed name

   grChild.addQuery(parentFieldName, parentRecordId);

5. Add gs.info in yourSI to debug and check what values are you getting at every function output and debug.

Hope this helps.