Create system property for mapping and use it for dynamic value

Priyanka Chaud1
Tera Contributor

I want to use a mapping kind of thing 

and store this in system property and use it  in my script include to make tablename,parentfield and column name dynamic how can i achieve this @suvro 

 

1 ACCEPTED SOLUTION

suvro
Mega Sage
Mega Sage

Just create a property with below values

{ "task" :{"refField": "parent", "qField" : "number" }, "incident" :{"refField": "parent_incident", "qField" : "number" }, "sys_user" :{"refField": "manager", "qField" : "name" }, "problem" :{"refField": "duplicate_of", "qField" : "number" }}

 

call that property

var tableName = gs.getProperty('property name'); //another property which stores tablename

var propV = gs.getProperty('property_name');

var propObj = JSON.parse(propV);

Now you can use it : 

propObj[tableName ].refField and propObj[tableName ].qField

View solution in original post

15 REPLIES 15

Yes Slight modification

initialize: function() {
      this.tableName = gs.getProperty('x_cyso_org_hierarc.dropdown_value'); 
       this.propV = gs.getProperty('x_cyso_org_hierarc.parentref _fields');
       this.propObj = JSON.parse(this.propV);
    },

@suvro please check the script once is it correct everywhere because answer is returning null

var TreeUtil = Class.create();
 TreeUtil.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
//   getProVa: function() {
//         var val = gs.getProperty('x_cyso_org_hierarc.dropdown_value');
    
//         return val;
//     },
initialize: function() {
      this.tableName = gs.getProperty('x_cyso_org_hierarc.dropdown_value'); 
       this.propV = gs.getProperty('x_cyso_org_hierarc.parentref _fields');
       this.propObj = JSON.parse(this.propV);
    },
    createAppropriateQuery: function(query){
        try{
            
            var arr=['=','SAMEAS','!=','ISEMPTY','ISNOTEMPTY','ANYTHING','NSAMEAS'];
            var propArr=query.split('^');
            if(propArr.length>1){
                for(var k=0;k<propArr.length;k++){
                    for (var j=0;j<arr.length;j++){
                        if (propArr[k].indexOf(arr[j])>0){
                            var a=propArr[k].split(arr[j]);
                            a=a[0].replace('OR','');
                            if(!this.fieldExist(a)){
                                query=query.replace(propArr[k]+'^OR','');
                                query=query.replace(propArr[k]+'^','');
                            }
                            break;
                        }
                    }
                }
            }
            else{
                for (var f=0;f<arr.length;f++){
                    if (propArr[0].indexOf(arr[f])>0){
                        propArr=query.split(arr[f]);    
                        if(!this.fieldExist(propArr[0]))
                            query="";
                    }
                }
            }
            return query;
        }catch(e){
            gs.error('Error in script include TreeUtil in createAppropriateQuery'+e);
        }
    },
fieldExist:function(column) {
        try{
//            var tableName = gs.getProperty('x_cyso_org_hierarc.dropdown_value'); 
          // var propV = gs.getProperty('x_cyso_org_hierarc.parentref _fields');
            //var propObj = JSON.parse(propV);
            if(column=="123TEXTQUERY321"){
                return true;
            }
            var gr= new GlideRecord('sys_dictionary');
        gr.addEncodedQuery('name='+this.tableName+"^element="+column);
            gr.query();
            return gr.hasNext();
        }catch(e){
            gs.error('Error in script include TreeUtil in fieldExist'+e);
        }
    },
    /*All users tree*/
    getAllUserListView:function(){
        try{
//         var tableName = gs.getProperty('x_cyso_org_hierarc.dropdown_value'); 
//            var propV = gs.getProperty('x_cyso_org_hierarc.parentref _fields');
//             var propObj = JSON.parse(propV);

            var query= this.createAppropriateQuery(this.getParameter('sysparm_tree_query'));
            var nextCount=this.getParameter('sysparm_next_count');
            var prevCount=this.getParameter('sysparm_tree_prev_count');
            var tree=this.CreateTree(query,nextCount,prevCount);
            return tree;
        }catch(e){
            gs.error('Error in script include TreeUtil in getAllUserListView'+e);
        }
    },
    /*Add Children Node*/
    addNodeChild: function()
    {
        try{
//          var tableName = gs.getProperty('x_cyso_org_hierarc.dropdown_value'); 
//            var propV = gs.getProperty('x_cyso_org_hierarc.parentref _fields');
//             var propObj = JSON.parse(propV);
            var managerID=this.getParameter('sysparm_parent_node');
            var query=gs.getProperty('x_cyso_org_hierarc.filter_condition')+'^'+this.propObj [this.tableName].refField+'='+this.getParameter('sysparm_parent_node');
            query=this.createAppropriateQuery(query);
            var tree=this.CreateTree(query,'addchilde',managerID);
            return tree;
        }catch(e){
            gs.error('Error in script include TreeUtil in addNodeChild'+e);
        }
    },
    getUserCount:function(){
        try{
            
//              var tableName = gs.getProperty('x_cyso_org_hierarc.dropdown_value'); 
//            var propV = gs.getProperty('x_cyso_org_hierarc.parentref _fields');
//             var propObj = JSON.parse(propV);
            var UserCount = new GlideRecordSecure(gs.getProperty('x_cyso_org_hierarc.dropdown_value'));
            var query=this.createAppropriateQuery(this.getParameter('sysparm_tree_query'));
            UserCount.addEncodedQuery(query);
            UserCount.query();
            return UserCount.getRowCount();
        }catch(e){
            gs.error('Error in script include TreeUtil in getUserCount'+e);
        }
    },
    getUsersRowCount:function(UserCount){

        return UserCount.getRowCount();
    },

   CreateTree:function(userQuery,addchildeFlag,previouscountorManagerID){
        try{
//         var tableName = gs.getProperty('x_cyso_org_hierarc.dropdown_value'); 
//            var propV = gs.getProperty('x_cyso_org_hierarc.parentref _fields');
//             var propObj = JSON.parse(propV);
            var newJson = [];
            var displayPhoto=gs.getProperty('x_cyso_org_hierarc.display_photo');
            var actualchildrenCount=0;
            var selfUser;
            var addConstraintChildren;
            var selectedUser = new GlideRecordSecure(gs.getProperty('x_cyso_org_hierarc.dropdown_value'));
            selectedUser.orderBy();
            selectedUser.addEncodedQuery(userQuery);

            if(addchildeFlag !='addchilde'){
                selectedUser.chooseWindow(previouscountorManagerID,addchildeFlag);
            }
            selectedUser.query();
            var selectedUserCount=this.getUsersRowCount(selectedUser);
            
            while(selectedUser.next()) {
                var newprop = {};
                var gendr;
                var Querystr=gs.getProperty('x_cyso_org_hierarc.filter_condition')+'^'+this.propObj [this.tableName].refField+'='+selectedUser.sys_id;
                actualchildrenCount=addchildeFlag=='addchilde' ? (actualchildrenCount+1): actualchildrenCount;
addConstraintChildren= addchildeFlag=='addchilde' ? selectedUser.getValue(this.propObj [this.tableName].refField): addConstraintChildren;
                Querystr= this.createAppropriateQuery(Querystr);
                /*Get children count*/
                var childrenUser = new GlideRecordSecure(gs.getProperty('x_cyso_org_hierarc.dropdown_value'));
                childrenUser.addEncodedQuery(Querystr);
                childrenUser.query();
                var count=this.getUsersRowCount(childrenUser);

                /*set profile image a per gender */
                gendr=selectedUser.getValue('gender');

                newprop.icon='';
                switch (gendr) {
                    case 'Female':
                        newprop.icon= 'Person_female.png';
                        break;
                    case 'Male':
                        newprop.icon= 'Person_male.png';
                        break;
                    default:
                        newprop.icon= 'Not_specified.png';
                }     

                /*Check if manager is same as user id */
                if(selectedUser.getUniqueValue() == selectedUser.getValue(this.propObj [this.tableName ].refField)){
                    count=count-1;
                    selfUser=true;
                }

                /*Set User Text*/
                var Userprop=gs.getProperty('x_cyso_org_hierarc.user_tree_configuration');
                var usertext =count> 0 ? selectedUser.getDisplayValue(this.propObj[this.tableName].qField)+' <span class="badge badge-light" style="background-color: darkcyan;">'+count+'</span>': selectedUser.getDisplayValue(this.propObj[this.tableName].qField);

                if(Userprop){
                    var userPropArr=Userprop.split(',');
                    for(var prop = 0; prop < userPropArr.length; prop++){
                        if(selectedUser.getDisplayValue(userPropArr[prop])){
                            usertext =usertext + '<span style="color: blue;font-weight: bold;"><span> | </span></span>' + selectedUser.getDisplayValue(userPropArr[prop]) ;
                        }    
                    }
                }
                newprop.id = selectedUser.getValue('sys_id');
                newprop.parent=addchildeFlag=='addchilde'?selectedUser.getValue(this.propObj[this.tableName].refField):'#';
                newprop.text = usertext;
                if(!((selectedUser.getUniqueValue() == selectedUser.getValue(this.propObj[this.tableName].refField)) && addchildeFlag=='addchilde')){
                    newJson.push(newprop);
                }
            }
        
            
            if(addchildeFlag =='addchilde' && (actualchildrenCount < selectedUserCount)){
                    var securtyConstraintUser={};   
                    securtyConstraintUser.id = previouscountorManagerID+'1';
securtyConstraintUser.parent=previouscountorManagerID;
                    
                    securtyConstraintUser.icon='Security_Constraint_small.png';
                    securtyConstraintUser.text = '<span style="color: grey">'+gs.getMessage('Hidden users due to security constraint')+':'+(selectedUserCount-actualchildrenCount)+'</span>';
                    newJson.push(securtyConstraintUser);
            }
            
            return JSON.stringify(newJson);
        }catch(e){
            gs.error('Error in script include TreeUtil in CreateTree'+e);
        }

    },

    type: 'TreeUtil'
});

 

I have made few corrections. But there are few functions where query is being sent so not sure what those queries contains for example 

this.getParameter('sysparm_parent_node');
this.getParameter('sysparm_tree_query')

CreateTree:function(userQuery,addchildeFlag,previouscountorManagerID){

There were few more so not able to understand whether this has correct values.

I would suggest you to test in pieces. Testing the whole thing together will be trouble.

Write a small piece of code test that first. Then write the next part

Corrected code below

var TreeUtil = Class.create();
 TreeUtil.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

initialize: function() {
      this.tableName = gs.getProperty('x_cyso_org_hierarc.dropdown_value'); 
       this.propV = gs.getProperty('x_cyso_org_hierarc.parentref _fields');
       this.propObj = JSON.parse(this.propV);
    },
    createAppropriateQuery: function(query){
        try{
            
            var arr=['=','SAMEAS','!=','ISEMPTY','ISNOTEMPTY','ANYTHING','NSAMEAS'];
            var propArr=query.split('^');
            if(propArr.length>1){
                for(var k=0;k<propArr.length;k++){
                    for (var j=0;j<arr.length;j++){
                        if (propArr[k].indexOf(arr[j])>0){
                            var a=propArr[k].split(arr[j]);
                            a=a[0].replace('OR','');
                            if(!this.fieldExist(a)){
                                query=query.replace(propArr[k]+'^OR','');
                                query=query.replace(propArr[k]+'^','');
                            }
                            break;
                        }
                    }
                }
            }
            else{
                for (var f=0;f<arr.length;f++){
                    if (propArr[0].indexOf(arr[f])>0){
                        propArr=query.split(arr[f]);    
                        if(!this.fieldExist(propArr[0]))
                            query="";
                    }
                }
            }
            return query;
        }catch(e){
            gs.error('Error in script include TreeUtil in createAppropriateQuery'+e);
        }
    },
fieldExist:function(column) {
        try{
//            var tableName = gs.getProperty('x_cyso_org_hierarc.dropdown_value'); 
            if(column=="123TEXTQUERY321"){
                return true;
            }
            var gr= new GlideRecord('sys_dictionary');
        gr.addEncodedQuery('name='+this.tableName+"^element="+column);
            gr.query();
            return gr.hasNext();
        }catch(e){
            gs.error('Error in script include TreeUtil in fieldExist'+e);
        }
    },
    /*All users tree*/
    getAllUserListView:function(){
        try{
//         var tableName = gs.getProperty('x_cyso_org_hierarc.dropdown_value'); 
//            var propV = gs.getProperty('x_cyso_org_hierarc.parentref _fields');
//             var propObj = JSON.parse(propV);

            var query= this.createAppropriateQuery(this.getParameter('sysparm_tree_query'));
            var nextCount=this.getParameter('sysparm_next_count');
            var prevCount=this.getParameter('sysparm_tree_prev_count');
            var tree=this.CreateTree(query,nextCount,prevCount);
            return tree;
        }catch(e){
            gs.error('Error in script include TreeUtil in getAllUserListView'+e);
        }
    },
    /*Add Children Node*/
    addNodeChild: function()
    {
        try{
//          var tableName = gs.getProperty('x_cyso_org_hierarc.dropdown_value'); 
//            var propV = gs.getProperty('x_cyso_org_hierarc.parentref _fields');
//             var propObj = JSON.parse(propV);
            var managerID=this.getParameter('sysparm_parent_node');
            var query=gs.getProperty('x_cyso_org_hierarc.filter_condition')+'^'+this.propObj[this.tableName].refField+'='+this.getParameter('sysparm_parent_node');
            query=this.createAppropriateQuery(query);
            var tree=this.CreateTree(query,'addchilde',managerID);
            return tree;
        }catch(e){
            gs.error('Error in script include TreeUtil in addNodeChild'+e);
        }
    },
    getUserCount:function(){
        try{
            
//              var tableName = gs.getProperty('x_cyso_org_hierarc.dropdown_value'); 
//            var propV = gs.getProperty('x_cyso_org_hierarc.parentref _fields');
//             var propObj = JSON.parse(propV);
            var UserCount = new GlideRecordSecure(gs.getProperty('x_cyso_org_hierarc.dropdown_value'));
            var query=this.createAppropriateQuery(this.getParameter('sysparm_tree_query'));
            UserCount.addEncodedQuery(query);
            UserCount.query();
            return UserCount.getRowCount();
        }catch(e){
            gs.error('Error in script include TreeUtil in getUserCount'+e);
        }
    },
    getUsersRowCount:function(UserCount){

        return UserCount.getRowCount();
    },

   CreateTree:function(userQuery,addchildeFlag,previouscountorManagerID){
        try{
//         var tableName = gs.getProperty('x_cyso_org_hierarc.dropdown_value'); 
//            var propV = gs.getProperty('x_cyso_org_hierarc.parentref _fields');
//             var propObj = JSON.parse(propV);
            var newJson = [];
            var displayPhoto=gs.getProperty('x_cyso_org_hierarc.display_photo');
            var actualchildrenCount=0;
            var selfUser;
            var addConstraintChildren;
            var selectedUser = new GlideRecordSecure(gs.getProperty('x_cyso_org_hierarc.dropdown_value'));
            selectedUser.orderBy();
            selectedUser.addEncodedQuery(userQuery);

            if(addchildeFlag !='addchilde'){
                selectedUser.chooseWindow(previouscountorManagerID,addchildeFlag);
            }
            selectedUser.query();
            var selectedUserCount=this.getUsersRowCount(selectedUser);
            
            while(selectedUser.next()) {
                var newprop = {};
                var gendr;
                var Querystr=gs.getProperty('x_cyso_org_hierarc.filter_condition')+'^'+this.propObj[this.tableName].refField+'='+selectedUser.sys_id;
                actualchildrenCount=addchildeFlag=='addchilde' ? (actualchildrenCount+1): actualchildrenCount;
addConstraintChildren= addchildeFlag=='addchilde' ? selectedUser.getValue(this.propObj[this.tableName].refField): addConstraintChildren;
                Querystr= this.createAppropriateQuery(Querystr);
                /*Get children count*/
                var childrenUser = new GlideRecordSecure(gs.getProperty('x_cyso_org_hierarc.dropdown_value'));
                childrenUser.addEncodedQuery(Querystr);
                childrenUser.query();
                var count=this.getUsersRowCount(childrenUser);

                /*set profile image a per gender */
                gendr=selectedUser.getValue('gender');

                newprop.icon='';
                switch (gendr) {
                    case 'Female':
                        newprop.icon= 'Person_female.png';
                        break;
                    case 'Male':
                        newprop.icon= 'Person_male.png';
                        break;
                    default:
                        newprop.icon= 'Not_specified.png';
                }     

                /*Check if manager is same as user id */
                if(selectedUser.getUniqueValue() == selectedUser.getValue(this.propObj[this.tableName ].refField)){
                    count=count-1;
                    selfUser=true;
                }

                /*Set User Text*/
                var Userprop=gs.getProperty('x_cyso_org_hierarc.user_tree_configuration');
                var usertext =count> 0 ? selectedUser.getDisplayValue(this.propObj[this.tableName].qField)+' <span class="badge badge-light" style="background-color: darkcyan;">'+count+'</span>': selectedUser.getDisplayValue(this.propObj[this.tableName].qField);

                if(Userprop){
                    var userPropArr=Userprop.split(',');
                    for(var prop = 0; prop < userPropArr.length; prop++){
                        if(selectedUser.getDisplayValue(userPropArr[prop])){
                            usertext =usertext + '<span style="color: blue;font-weight: bold;"><span> | </span></span>' + selectedUser.getDisplayValue(userPropArr[prop]) ;
                        }    
                    }
                }
                newprop.id = selectedUser.getValue('sys_id');
                newprop.parent=addchildeFlag=='addchilde'?selectedUser.getValue(this.propObj[this.tableName].refField):'#';
                newprop.text = usertext;
                if(!((selectedUser.getUniqueValue() == selectedUser.getValue(this.propObj[this.tableName].refField)) && addchildeFlag=='addchilde')){
                    newJson.push(newprop);
                }
            }
        
            
            if(addchildeFlag =='addchilde' && (actualchildrenCount < selectedUserCount)){
                    var securtyConstraintUser={};   
                    securtyConstraintUser.id = previouscountorManagerID+'1';
securtyConstraintUser.parent=previouscountorManagerID;
                    
                    securtyConstraintUser.icon='Security_Constraint_small.png';
                    securtyConstraintUser.text = '<span style="color: grey">'+gs.getMessage('Hidden users due to security constraint')+':'+(selectedUserCount-actualchildrenCount)+'</span>';
                    newJson.push(securtyConstraintUser);
            }
            
            return JSON.stringify(newJson);
        }catch(e){
            gs.error('Error in script include TreeUtil in CreateTree'+e);
        }

    },

    type: 'TreeUtil'
});

@suvro  if we don't use 2 properties and we are having tablename in 2nd property also in that object how can we fetch that tablename as we are fetching reffield and qfield?so we will use only one property than what will be the change in syntax of these propObj[tableName ].refField and propObj[tableName ].qField and also for tablename what we would use?

Also when i am using in every function those variables than data is coming so its working i guess defining in every function so now i had requirement to take tablename also from the same property not the 1st one so how can we do that

 

If we include TableName in the same property. How to make it dynamic ??

To make it dynamic, we need to make sure we get the tableName as input from somewhere right?

Secondly I would suggest to break this code in smaller parts and test it out each method separately