g_form.nameMap is not working in catalog client script

Naina3
Kilo Contributor

I am getting issue while loading a service stated as "There is a JavaScript error in your browser console". When I checked console window it is showing issue:" Error while running Client Script "UI handler": TypeError: nameMap is undefined". Below is the catalog client script. Can anyone help me with this?

function onLoad() {
    
    var nameMap = g_form.nameMap;
    
    var adjustableLables = {
        leasehold_improvements_impact: "font-size:.65em",
        vendor_software_website: "font-size:.85em",
        vendor_software_website_impact: "color:#FFF",
        internal_gen_software_site: "font-size:.70em",
        internal_gen_software_site_impact: "color:#FFF",
        internal_gen_software_website_cip_impact: "color:#FFF",
        internal_gen_software_website_cip: "font-size:.65em",
        vendor_party_software_website_cip: "font-size:.75em",
        vendor_party_software_website_cip_impact: "color:#FFF",
        computer_equipment_cip: "font-size:.95em",
        fixture_fittings_cip: "font-size:.95em",
        leasehold_improvements_cip: "font-size:.85em",
        plant_equipment_cip: "font-size:.95em",
        show_equipment_cip: "font-size:.95em",
        computer_equipment_cip_impact: "color:#FFF",
        fixture_fittings_cip_impact: "color:#FFF",
        leasehold_improvements_cip_impact: "color:#FFF",
        plant_equipment_cip_impact: "color:#FFF",
        show_equipment_cip_impact: "color:#FFF",
        computer_equipment_impact: "color:#FFF",
        fixture_fittings_impact: "color:#FFF",
        plant_equipment_impact: "color:#FFF",
        show_equipment_impact: "color:#FFF"
    };
    
    var skipDisabling = ["leasehold_improvements_cip_impact","leasehold_improvements_impact"];
    top.abc_siblings = [];
    top.calculations = {
        
        abc_divideBy : function(divisor, dividend){
            
            if( divisor == 0 || dividend == 0){
                return 0;
            }
            
            var calc = dividend / divisor;
            
            return calc;
        },
        
        up_total : function( fieldName ){
            var total = 0;

            for( var label in adjustableLables){
                
                var indx=  label.indexOf('_impact');
                
                if( skipDisabling.indexOf( label ) == -1 && indx != -1 ){
                    var hasFinance = fieldName.indexOf('_financial');
                    var fieldFor = label;
                    
                    
                    if(hasFinance == -1){
                        fieldFor = label.substring(0, indx);
                    }
                    
                    var value = g_form.getValue( fieldFor );
                    
                    if( value && value != '' ){
                            total += (value -0);
                    }
                    
                }
            }
            
            if( total > 0){
                g_form.setValue(fieldName, total );
            }            
        },
        
        show_equipment_impact : function(val){
            var calc = window.calculations["abc_divideBy"];
            return calc(5, val);
        },
        
        plant_equipment_impact : function(val){
            var calc = window.calculations["abc_divideBy"];
            return calc(5, val);
        },
        
        fixture_fittings_impact : function(val){
            var calc = window.calculations["abc_divideBy"];
            return calc(7, val);
        },
        
        computer_equipment_impact : function(val){
            var calc = window.calculations["abc_divideBy"];
            return calc(3, val);
            
        },
        
        show_equipment_cip_impact : function(val){
            var calc = window.calculations["abc_divideBy"];
            return calc(2, val);
        },
        
        plant_equipment_cip_impact : function(val){
            var calc = window.calculations["abc_divideBy"];
            return calc(5,val);
        },
        
        fixture_fittings_cip_impact : function(val){
            var calc = window.calculations["abc_divideBy"];
            return calc(5,val);
        },
        
        computer_equipment_cip_impact : function(val){
            var calc = window.calculations["abc_divideBy"];
            return calc(3,val);
        },
        
        vendor_party_software_website_cip_impact : function(val){
            var calc = window.calculations["abc_divideBy"];
            return calc(3, val);
        },
        
        internal_gen_software_site_impact : function(val){
            var calc = window.calculations["abc_divideBy"];
            return calc(3, val);
        },
        
        internal_gen_software_website_cip_impact : function(val){
            var calc = window.calculations["abc_divideBy"];
            return calc(3, val);
        },
        
        vendor_software_website_impact : function(val){
            var calc = window.calculations["abc_divideBy"];
            return calc(3, val);
        }
    };
    
    function adjustLabelStyle( forId, style ){
        var contLabel =  document.getElementById( 'label_'+ forId );
        
        if( contLabel ){
            contLabel.style = style;
        }
    }
    
    function executeCalculation( element ){
        var abc = element.getAttribute("abc");
        var worker = window.calculations[ abc ];

        if( worker ){
            var childValue= worker(element.value);
            g_form.setValue(abc, childValue );
            
            window.calculations["up_total"]('total_amount_requested');
            window.calculations["up_total"]('total_amount_financial');
        }
    }
    
    function executeAmountCalculation( element ){
    
        var internalTotal = 0;
        var externalTotal = 0;
        
        for( var x=0; x < window.abc_siblings.length; x++){
            var nm = window.abc_siblings[x];
            
            var el = g_form.getValue( nm );
            
            if( el && el != '' ){
                
                if( nm.indexOf('external_') != -1 ){
                    externalTotal += (el - 0);
                } else {
                    internalTotal += (el - 0);
                }
            }
        }
        
        if( internalTotal != 0){
            g_form.setValue('total_internal_spend', internalTotal );
        }
        
        if( externalTotal != 0){
            g_form.setValue('total_external_spend', externalTotal);
        }
    }
    
    //adjust width of all impact amounts
    for( var x=0;x<nameMap.length;x++ ){
        var nm = nameMap[x].prettyName;
        var rm;
        var fld;
        
        //with each _impact field
        if( nm.indexOf('_impact') != -1 ){
            rm = nameMap[x].realName;
            //sn grab it with friendlyName
            fld = g_form.getControl( nm );
            
            //adjust its width
            if( fld ){
                fld.style="max-width:5em";
                
            } //end if (fld)
            
        }
        
        if( adjustableLables[nm] ){
            rm = nameMap[x].realName;
            adjustLabelStyle( rm, adjustableLables[nm] );
            
        }
        
        //disable calculated fields
        var indx = nm.indexOf('_impact');
        if( adjustableLables[nm] && indx != -1 && skipDisabling.indexOf( nm ) == -1 ){
            g_form.setDisabled(nm, true);
            
            var relatedTo = nm.substring( 0, indx );
            
            var control= g_form.getControl(relatedTo);
            
            if( control ){
                control.setAttribute("abc", nm);
                
                //add event handler to impact fields
                $(control.id).observe( 'blur', function(event){
                        var nm = this.value;
                        var pn = nm.replace(/[^0-9\.]+/g,"");
                    
                        this.value = pn;
                    
                        executeCalculation(this);
                });
            }
        }
        
        //adjust amount fields
        var indx = nm.indexOf('_spend_amount_');
                
        if( indx != -1 ){
            window.abc_siblings.push(nm);
            var amControl = g_form.getControl(nm);
            
            if( amControl ){

                //add event handler to impact fields
                $(amControl.id).observe( 'blur', function(event){
                    var val = this.value;
                    var nVal = val.replace(/[^0-9\.]+/g,"");
                    this.value = nVal;
                    
                    executeAmountCalculation(this);
                });                
            }
            
        }
    }
}

 

Thanks in advance!

1 ACCEPTED SOLUTION

Chandra Prakash
Tera Expert

Hi Naina, 

Try 

  var nameMap = g_form.getValue('nameMap');

 

Mark Correct it it helps,

Chandra Prakash

View solution in original post

2 REPLIES 2

Chandra Prakash
Tera Expert

Hi Naina, 

Try 

  var nameMap = g_form.getValue('nameMap');

 

Mark Correct it it helps,

Chandra Prakash

poyntzj
Kilo Sage

Just checked in a London PDI and it works - on the Catalog Item it is good and it is on the RITM too (not sure when that came in as a lot of my code for RITM variable work relies on a variable map build via a display BR)

If so, go into the item and into the developer tools and g_form.nameMap is an option and should be OK and return you the mapping of prettyname, realname and label

g_form.getValue(nameMap) will only work if there is a variable called nameMap

Only thing I can think of is that you may need to add a small delay routine to ensure the document is complete before running - I tend to do this with routines that may lock / unlock variables on the RITM