cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

"cannot convert null to an object" in a triggerscript

JensN.
13-Aquamarine

"cannot convert null to an object" in a triggerscript

Hi @all,

i'm now outing me as a non-programmer

We have following problem: in an item were three datefields. The first field should be filled by the user, and the other two fields should be computed by a rule. Because filling a datefield by a computation it is not editable anymore we wanted to use a trigger to do this.

This works fine unless the user doesnt fill the first datefield. Then we get a javascript error:

"Internal Error: undefined: Cannot convert null to an object."

Here comes the code from the triggerscript:

try

{

var issue = bsf.lookupBean("imIssueDeltaBean");

var params = bsf.lookupBean("parametersBean");

var fieldDueDate = params.getParameter("Startfeld");

var CompValue = params.getParameter("Anzahl Tage");

var targetField = params.getParameter("Zielfeld");

}

function main()

{

var StartDate = issue.getNewFieldValue(fieldDueDate);

var StartDate = new Date().setTime(StartDate.getTime()+(CompValue*24*60*60*1000));

issue.setFieldValue(targetField, StartDate); // set field to due date

}

}

The red line is the one which causes the error.

The Trigger-Config has three fields:

  • "Startfeld" (the first datefield),
  • "Zielfeld" (the target datefield)
  • "Anzahl Tage", which holds the Number of days to compute with.

So how we get rid of this Javascript-Error? First we tried to catch an empty field by the trigger-rule with looking for a "Startfeld" != -unspecified-

but this doesnt work. So we tried to get this in the script by an if-statement in the main-function like:

if (fieldDueDate != null)

but this has no effect, this if-Statement is always false.

Other if-Statements were

if (!fieldDueDate)

or

if (!isNaN(fieldDueDate))

are always true, so we never get into the function. Can someone help me, please ? Thanks in advance...

kind regards, Jens


7 REPLIES 7

Hi Jens,

In situations which usually cause this error, it is because of a blank parameter. When the trigger attempts to pull a value from the parameters defined (in this case 'Startfield'), it finds nothing there. Is the trigger properly configured with all necessary parameters?

JensN.
13-Aquamarine
(To:JoeBartlett)

Hi,

i think it is properly configured. Its a pre-trigger and here is the rule:

and

|- Type = <Itemtype>

|- Startfeld <> Startfeld [new Value]

+ or

|- Startfeld <> - Unspecified -
|- Startfeld [new Value] <> - Unspecified -

So i want to trigger every time the value of "Startfeld" is changed and i want not to trigger if the field is empty. But this is my first problem: the "unspecified"-statement doesnt work, the trigger is always triggering. If this would work, we wouldnt have to handle this problem in the script.

kind regards, Jens

Ah I see. You can remove the "Startfeld <> - Unspecified -" part of the rule. This particular piece is checking the value of Startfield before the transaction and because it is looking for non-empty values, the trigger would fire even if it had a value.

The "Startfield [New Value]" portion is all you need since the New Value element is checking the value at the end of the transaction. If the field started NULL but then was given a value, the trigger will not fire (exactly as expected).

Hope that helps!

JensN.
13-Aquamarine
(To:JoeBartlett)

Hi Joseph,

thank you for your answer. But what if an user wants to clear a date-field? Then i would need the "Startfeld [New Value]<> - Unspecified -"-Rule. And then we'll get this error again, or not? So i think we need a solution anyway. Either a catch for the Javascript-Error or a rule which will catch a cleared date-field.

We also couldnt set these date-field as mandatory, because we need the possibility to have these fields sometimes without value.

kind regards, Jens

Hi Jens,

If the user is clearing the date field and you still want this trigger to run, then you would not need to see if Startfeld is unspecified (either the new value or the existing value).

If I understand your use case correctly, I would have the trigger work in this way:

1) if the user is clearing out the startfeld, do something to the zielfeld and anzahl tage fields (it could be clearing them out or setting to a default value... it really depends on your business needs)

2) if the user is NOT clearing out the startfeld, then run your function normally and you shouldn't have the null error (since startfeld has a value).

pseudocode:

function main()

{

var StartDate = issue.getNewFieldValue(fieldDueDate);

if (StartDate == null) {

//do something to handle when user clears the fields...

//StartDate is null, so it can't be used in other calculations

} else {

//startDate is good, so this won't error out

var StartDate2 = new Date().setTime(StartDate.getTime()+(CompValue*24*60*60*1000));

issue.setFieldValue(targetField, StartDate2); // set field to due date

}

}

You could also split this into 2 triggers, each of which has their own rule to handle their own case.

1) Rule to handle trigger 1 handles when startfeld is cleared out

and

|- Type = <Itemtype>

|- Startfeld <> Startfeld [new Value]
|- Startfeld [new Value] == - Unspecified -

2) Rule to handle trigger 2 uses your existing trigger and runs when startfeld is filled in

and

|- Type = <Itemtype>

|- Startfeld <> Startfeld [new Value]
|- Startfeld [new Value] <> - Unspecified -

Hope that helps,

Matt

JensN.
13-Aquamarine
(To:matt_giltaji)

Hi Matt,

thanks for your answer. And no, we didnt want the trigger to run if the Startfeld is cleared, but the trigger-rule

"Startfeld [new Value] <> - Unsepcified -"

doesnt work, so the trigger is running even with a cleared Startfeld. It would be easy if the rule would work

And because the rule doesnt work, we always get the javascript-error. To avoid this error we wanted also to have an If-statement like

"if (StartDate == null)"

like you do in your Pseudo-Code, but the javascript-error comes out of the line before:

"var StartDate = issue.getNewFieldValue(fieldDueDate);".

kind regards, Jens

Hi Jens,

Are you sure the error is coming out in that exact line? Sometimes the error message will give a line number that is 1 or 2 lines away from the real error itself.

Try adding some print statements to the logs around the line to see where it is actually breaking:

print("1");

var StartDate = issue.getNewFieldValue(fieldDueDate);

print("2");

var StartDate = new Date().setTime(StartDate.getTime()+(CompValue*24*60*60*1000));

print("3");

The error message itself "cannot convert null to an object" sounds like it should becoming from StartDate.getTime(), not the issue.getNewFieldValue(), since issue is defined and should not be null.

For the rule not working, try using != instead of <>. It shouldn't make a difference if the field is not multivalued, but Integrity can be quirky like that.


Hope that helps,

Matt

Announcements


Top Tags