Unable to set value for custom field

Divya Kandula
Kilo Guru

Hi Everyone,

 

I have a table Declaration goods Table which has two fields Commodity code and Commodity code hierarchy. Commodity code is referencing custom table which has commodity code, Goods Desc and Hierarchy Desc. 

My requirement is to print commodity code hierarchy field with Hierarchy Desc value only if Goods Desc is Other.

I have written a Before BR for this which has below code

 

    var comHierarchy = current.u_commodity_code_hierarchy;
    var gr = new GlideRecord('sn_customerservice_commodity_code'); //custom table
    gr.addEncodedQuery('sys_id=' + current.u_commodity_code);
    gr.query();
    if (gr.next()) {
        var goodsDesc = gr.u_hierarchy_description;
        comHierarchy = goodsDesc.replace(/[||]+/g, '\n');
    }
    comHierarchy = goodsDesc.replace(/[||]+/g, '\n');
    current.update();
 
This is not updating the value of Commodity code hierarchy.
Please let me know where im wrong
1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @Divya Kandula ,

I tried your problem in my PDI and it works for me, can you please use below code 

var comHierarchy = current.u_commodity_code_hierarchy;
    var gr = new GlideRecord('sn_customerservice_commodity_code'); //custom table
    gr.addEncodedQuery('sys_id=' + current.u_commodity_code);
    gr.query();
    if (gr.next()) {
        var goodsDesc = gr.u_hierarchy_description;
        comHierarchy = goodsDesc.replace(/[||]+/g, '\n');
    }
    current.setValue('u_commodity_code_hierarchy', comHierarchy);

 

Please mark my answer correct and helpful if this works for you

 

Thanks and Regards 
Sarthak

View solution in original post

4 REPLIES 4

Community Alums
Not applicable

Hi @Divya Kandula ,

I tried your problem in my PDI and it works for me, can you please use below code 

var comHierarchy = current.u_commodity_code_hierarchy;
    var gr = new GlideRecord('sn_customerservice_commodity_code'); //custom table
    gr.addEncodedQuery('sys_id=' + current.u_commodity_code);
    gr.query();
    if (gr.next()) {
        var goodsDesc = gr.u_hierarchy_description;
        comHierarchy = goodsDesc.replace(/[||]+/g, '\n');
    }
    current.setValue('u_commodity_code_hierarchy', comHierarchy);

 

Please mark my answer correct and helpful if this works for you

 

Thanks and Regards 
Sarthak

Thank you @Community Alums i dont know how i missed this line

OlaN
Giga Sage
Giga Sage

Hi,

 

A few pointers.

Avoid using current.update in a business rule, because you risk ending up with an infinite loop. (the update causes an update, which causes an update and so on).

 

In this particular scenario I think there is no need to query the other table for the value, because if you have a valid reference to the record, you can dot-walk to get the attribute directly instead.

 

Providing a simple example on how you can do below, just replace the fields to ones that suit your requirements.

	var someValue = current.u_custom_attribute_reference.u_some_field_value.toString();
	var updatedDescription = '';
	if (someValue){
		updatedDescription = someValue.replace('text_to_replace', 'replacement_text');
	}

	if (updatedDescription){
		current.setValue('some_other_field_to_update_on_current_record', updatedDescription);
	}

I tried dot walking but it dint work for some reason