Important Update: Community URLs redirect issues are partially resolved. Learn More. .

cancel
Showing results for 
Search instead for 
Did you mean: 

Generate Field IDs in Javascript from SQL

RusFM
Contributor III

If you have access to your DB, then you can run some SQL to generate the field IDs for your custom objects.

select 
    '''' + fl.field_name + ''':' +
    '''' + cast( fd.field_id as nvarchar ) + ''','
from       tblModule           m
inner join tblLevel            l   on ( l.module_id = m.module_id )
inner join tblIVFieldDef       fd  on ( l.level_id  = fd.level_id )
inner join tblFieldTranslation fl  on ( fd.field_id = fl.field_id )
where 
    ( m.module_id = ??? ) -- get this from tblModule
order by
    fl.field_name asc

You will need to set SSMS to return results in text, and-

- wrap the output with the var declaration and final curly brace

- remove the final comma of the output (after the last field id)

var fields = {
    'Application':'20575',
    'Application Acronym':'20623',
    'Application Name':'20577'
};

Paste this in your custom object and you can lookup the field IDs like-

var id = fields['Application'] // returns '20575'

Hope it helps someone.

Rus

1 REPLY 1

RusFM
Contributor III

To expand on this for multiple environments, note the use of location.host.indexOf() to determine what environment you're in.  You'll need to tweak that logic for your domain names.  Then run the SQL on each environment to get the field IDs to paste in.  Don't forget to remove the last comma in the generated JSON or it will case an error.  If performance is a concern the field map could be stored in sessionStorage.

Rus

 

 

function getFieldId( fieldAlias ) {

    // returns the field id for a given field alias
    // fieldAlias must match, but is case insensitive
    // run a specific sql query for each environment, paste in the generated json here, remove the trailing comma
    
    var host = 'prod';
    // modify this for your domain/server names
    if( location.host.indexOf( 'staging' ) > 1 ) { host = 'staging'; }

    var fieldAliasLower = fieldAlias.toLowerCase();

    console.log( 'host = ' + host + ', fieldAliasLower = ' + fieldAliasLower );
    
    var fieldMap = 
        '{ "prod": { ' +
                '"field1": "100", ' +
                '"field2": "101", ' +
                '"field3": "102", ' +
                '"field4": "103", ' +
                '"field5": "104" ' +
            '}, ' +
            '"staging": { ' +
                '"field1": "112", ' +
                '"field2": "124", ' +
                '"field3": "125", ' +
                '"field4": "134", ' +
                '"field5": "137" ' +
            '} ' +
        '}';
        
    var map = JSON.parse( fieldMap );
    var fieldId = map[host][fieldAliasLower]

    console.log( 'fieldId = ' + fieldId );

    return fieldId;

}

 

.And here's the SQL-

[Edit]  You'll need to find your module ID in tblModule and replace the 1234 below. 

 

-- generate partial json that can be included in the getFieldId() function
select 
    '''"' + lower( fd.Alias ) + '": "' + cast( fd.field_id as nvarchar ) + '", '' +'
from       tblModule     m
inner join tblLevel      l  on ( l.module_id = m.module_id )
inner join tblIVFieldDef fd on ( l.level_id  = fd.level_id )
where 
    ( m.module_id = 1234 )
order by
    fd.Alias