Returning "null" as a value in a field update when

Posted by jsniemi79 on 24-Sep-2014 08:22

I have written a trigger to update a text field on my user with a pipe delimited list of values from a related object. I'd like the end result to be in the this format: "value|value|value...".  When I run the debug on my code using the println, I get exactly what I was expecting.  However, when I run the trigger on the object, it adds a value of "null" before the first pipe.

Here is the code for my trigger:

rbv_api.setFieldValue("USER", {!id}, "subordinate_sales_agents", '');

{!#LOOP_BEGIN.ORG_CHART}

var AcctID = rbv_api.selectQuery("SELECT sales_agent_id#value FROM agility_user_data WHERE agility_login = ?", 1, '{!ORG_CHART#C.loginName}');

if (AcctID.length == 1) {

var SA1 = rbv_api.getFieldValue("USER", {!id}, "subordinate_sales_agents");
var SA2;

if (SA1 != '') {
var SA2 = SA1 + '|' + AcctID[0][0]; 
} else {
var SA2 = AcctID[0][0];
}

rbv_api.setFieldValue("USER", {!id}, "subordinate_sales_agents", SA2);

}

{!#LOOP_END.ORG_CHART}

var text = rbv_api.getFieldValue("USER", {!id}, "subordinate_sales_agents");
rbv_api.println(text);

Here is the screenshot of the debug on my system:

Here is the value on my user after the trigger was executed:

All Replies

Posted by Godfrey Sorita on 24-Sep-2014 08:59

Hi Jason,

You can use log() API to trace the value of your variables when running a trigger.

The code below is a slightly modified version of your code. It will still execute the same actions but it logs the values of your variables DURING trigger run-time. 

rbv_api.setFieldValue("USER", {!id}, "subordinate_sales_agents", '');

{!#LOOP_BEGIN.ORG_CHART}
var AcctID = rbv_api.selectQuery("SELECT sales_agent_id#value FROM agility_user_data WHERE agility_login = ?", 1, '{!ORG_CHART#C.loginName}');

if (AcctID.length == 1) {
  var SA1 = rbv_api.getFieldValue("USER", {!id}, "subordinate_sales_agents");
  rbv_api.log("debug", "SA1:"+SA1);
  var SA2;

  if (SA1 != '') {
    var SA2 = SA1 + '|' + AcctID[0][0]; 
  } else {
    var SA2 = AcctID[0][0];
  }

  rbv_api.setFieldValue("USER", {!id}, "subordinate_sales_agents", SA2);
  rbv_api.log("debug", "SA2:"+SA2);
}

{!#LOOP_END.ORG_CHART}

var text = rbv_api.getFieldValue("USER", {!id}, "subordinate_sales_agents");
rbv_api.println(text);
rbv_api.log("debug", "text:"+text);

After running the trigger, kindly check the results by clicking the log button(See screenshot below).

Regards,

Godfrey

Posted by jsniemi79 on 24-Sep-2014 14:43

I ran this code and when it went through the debugger, it appears like all the variables are being assigned appropriately.

Here are the results from the log.

[2014-09-24 15:39:18,509] SA1:

[2014-09-24 15:39:18,511] SA2:RBAGWELL

[2014-09-24 15:39:18,516] SA1:RBAGWELL

[2014-09-24 15:39:18,516] SA2:RBAGWELL|DSMITH

[2014-09-24 15:39:18,517] text:RBAGWELL|DSMITH

However, if I run the code, it still appears to be adding the null value to the front of that string.

Posted by Varun Tayal on 24-Sep-2014 15:47

After you enabled logging, you verified log after debugging. But did you get the same result in the logs after running your trigger?

Posted by jsniemi79 on 24-Sep-2014 15:50

Nope, it shows the null values.

[2014-09-24 15:41:58,344] SA1:null
[2014-09-24 15:41:58,348] SA2:null|RBAGWELL
[2014-09-24 15:41:58,352] SA1:null|RBAGWELL
[2014-09-24 15:41:58,356] SA2:null|RBAGWELL|DSMITH
[2014-09-24 15:41:58,356] text:null|RBAGWELL|DSMITH

Posted by Varun Tayal on 24-Sep-2014 16:36

I tested this scenario and I can tell you this as a workaround for now-

1. Change your first line such that instead of setting empty string '' set single space ' ' to your field.

2. Change your if condition <if (SA1 != ' ')> accordingly.

Let me know if this works for you.

-Hope this helps

Posted by jsniemi79 on 25-Sep-2014 08:57

Thanks for the work around.  That definitely helped.  

Posted by Varun Tayal on 25-Sep-2014 09:08

Well that's great. Just in case you still want to use empty String, you just need to change your if condition to check for null string-

if (SA1 != '' && SA1 != 'null')

This thread is closed