Error = JavaException: java.lang.ClassCastException: com.glide.script.fencing.ScopedGlideRecord cannot be cast to com.glide.script.fencing.ScopedGlideElement

Shubham15
Kilo Contributor

While using Script Step in flow designer I am getting the above mentioned error. I am calling a table using GlideRecord and I am inserting record in the table.

Code :-

var countKey = key.length;
var countValue = value.length;
var release = inputs.release;

try{
for(var i =0; i< countKey; i++) {

var gr = new GlideRecord('rm_story');
gr.query();

gr.short_description = key[i];
gr.description = value[i+1];
var sys = gr.insert(); // Here I am able to insert the values in this table ( Story Table ).
//I am successfully getting the sys id.
 var m2m = new GlideRecord('rm_m2m_release_task');  // I am not able to insert records in this table and getting the error
 m2m.query();
 m2m.task = sys;
 m2m.product_release = release;
 m2m.insert();
}
}

catch(ex) {
gs.error("Error = "+ex);
}

 

Can someone help me with it ?

3 REPLIES 3

Chandu Telu
Tera Guru
Tera Guru

Hi,

Check the below link it may be related scope issue.

https://community.servicenow.com/community?id=community_question&sys_id=f6344b29dbd8dbc01dcaf3231f961938

 

Thanks
Chandu Telu
Please Mark ✅ Correct/helpful, if applicable,

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Shubham,

Any reason for doing a .query() instead of .initialize()?

try commenting out "m2m.product_release = release;" to see if it'll work. If it does, check the type of field "release" in inputs and also type of "product_release". 

var countKey = key.length;
var countValue = value.length;
var release = inputs.release;

try {
    for (var i = 0; i < countKey; i++) {

        var gr = new GlideRecord('rm_story');
        gr.initialize();

        gr.short_description = key[i];
        gr.description = value[i + 1];
        var sys = gr.insert(); // Here I am able to insert the values in this table ( Story Table ).
        //I am successfully getting the sys id.
        var m2m = new GlideRecord('rm_m2m_release_task'); // I am not able to insert records in this table and getting the error
        m2m.initialize();
        m2m.task = sys;
        m2m.product_release = release;
        m2m.insert();
    }
} catch (ex) {
    gs.error("Error = " + ex);
}

pveg
Tera Contributor

Hi!

 

I know this answer comes really late, but I faced the same issue today and discovered what causes this message to pop out.

 

This happens when we perform a GlideRecord and try to set a field value to a whole object. Most likely your variable sys refers to an object.

 

Even though you're getting the sys id, it tries to insert the whole referenced object into the task field. To solve it, I recommend trying to access the sys_id field and also parse it into a string, for example: sys.sys_id.toString(); 

 

That way you'll be sure you're inserting just the sys_id into the task field and there'll be no ClassCastException ðŸ˜Š.

 

Hope it helps you or another person that comes here searching for the solution to this problem.