SSJS - Error When Accessing Date Field - xpages

In SSJS I'm using doc.getDocument().getItemValue(dateFld)[0] to access the date field value.
For one and only one particular document, I'm getting "Error while accessing indexed property #'0' on ojbect class java.util.Vecotr Array index out of range: 0
I checked the field on that document and it has a date-time value; the format is no different from the documents that are working correctly. I even ran SSJS code that did a typeof on the field value and measured its size: it's a vector and its size is 1.
I've tried resetting the field value on the document. I've tried removing the field, then resetting it, to no avail.
This is the second time I've come across this problem concerning a date field. Anyone have any suggestions?

You are getting this error because the item isn't in the backend document. Try using
doc.getDocument().getItemValueString("...")
or any other method that will return a "real" value instead of the Vector crap.
BTW: Don't use the [x] style to access the Vectored value - use .elementAt(x) instead when using the Vector returning methods :-)

I would add the following around your code, to assign a default value if the date field doesn't exits
if(doc.getDocument().hasItem(dateFld)){
//You code when the document exists
}else{
//Assign default value
}

Related

LotusScript - Why do we use array index to access value of text field

I am working on some LotusScript code and came across this syntax to access a text field value: textField(0). I checked this field's properties in the design and it has the type of Text and doesn't allow multiple values. But why do we use the index to access this field value? I need to describe what the code is doing so it makes a difference between "get the value of textField then do something" vs. "get the first element of textField then do something".
The LotusScript expression document.textField is a shorthand form of document.GetItemValue("textField"). As explained in Designer Help, this expression always returns an array of strings for text or text list fields. That's why one has to use document.textField(0), or document.GetItemValue("textField")(0) for that matter, to access the first (and possibly only) element in the returned array.
An important thing to remember is that just because the field properties say that multiple values aren't allowed today, they might have been allowed in the past. Nothing gets "fixed up" in old documents if you change the properties in Domino Designer. Your code always has to consider that possibility.
And for that matter, even when the field properties do say that multiple values aren't allowed, those properties only apply when a user is editing the document using the defined form. An agent or code using any of the Notes APIs can always store multiple values in any text field.
That's why you always access item values through an array. It's not just a matter of "that's how the API works". It's a matter of how the underlying storage works.
And remember: Notes is schemaless. You have to always assume the following:
The item that corresponds to the field might not exist.
The item might be empty even if there's a validation formula that requires it to be filled in.
The item that corresponds to a text field might not actually be a text item.
The item can contain a list even if the multi-values property is not checked.

Suitescript 2.0 update lookup field value

I need to get a lookup fields value from a line item on an SO and then update the corresponding line item in a PO. Fetching the value is not a problem, I'm using
newSite = record.getSublistValue("item", "custcol_site", lineNum)
to get the value - this returns the id of the lookup field object. However when I then try to update the field value on the PO using
loadedTransaction.setSublistValue({
sublistId: "item",
fieldId: "custcol_site",
value: newSite,
line: lineNum
});
Nothing happens, I don't get an error, however the field doesn't update either. How can I update the field using this ID value I've already fetched?
I'm also updating a number of other fields without issue following the same pattern, it's only the lookup field that's not updating successfully, so "lineNum" and fieldIds are not the problem as far as I can tell, I must just not be sending the correct information to update the lookup, however I can't find somewhere to tell me what information to send through.
Inferring from the name loadedTransaction that you have called record.load() to retrieve that reference, you'll also need to then call loadedTransaction.save() to commit your changes to the database.
Are you committing the line item before saving the record ?
recordObj.commitLine('item');

Kentico - Change Pagetype Field Name without blanking out current values

I have a custom page type for Staff and there's a field called Function. I have a WHERE condition in a repeater like this: Function LIKE '%insurance%' and got an error Incorrect syntax near the keyword 'Function'
From the error I guess Function is a reserved keyword, so I changed the field name to BusinessFunction. However right after changing the field name, I noticed all the values for that field become blank. If I changed it back, the values are back to normal. The question: is there a way to change the field name while keeping the values that are already there -- without access to the backend database?
Wrap the word Function in brackets like so should resolve the problem: [Function]
When you say the values of the field are blank do you mean on the display side in the repeater? Have you changed the transformation to use the new field name? Have you updated the Columns property of the repeater to use the new field name vs. the old one? It will not lose all the data, you just need to ensure all the values of the property/field name are updated everywhere including any custom code you might be using for this page type.

Client Script : custom form update field

Is it possible to retain all the data inside a form when the Custom form field is update? Currently, it reloads the page and all data will be gone.
THank you.
You could do :
On fields edit --> save field value to a cookie
And clear all those cookies on save.
That would be breaking normal functionality though...
I would have to test this, but you could make an array with all your field names, and do a validateField function (so it runs before the field is officially changed) where you get the values of every field in your array and store the values in a global array. Then have a fieldChanged function (so it runs after the field has changed) which loops back through resetting those values. This may most efficiently be done with an associative array

Setting default value for OptionSet in CRM 2011 Dialog

I have a requirement to populate the default value for an OptionSet in a Prompt-Response based on a value pulled in from a query within the Dialog. However, the default value field is disabled for both types of OptionSet.
Can anyone confirm if this is the correct behaviour as I can't think of any reason why you wouldn't be able to assign a default to an OptionSet at runtime?
This will mean duplicating the entire Page as I can't even add a Condition statement to conditionally use different Prompt-Responses unless anyone can offer a better solution?
When you do query that means you are getting a set of values, even if it's a single one.
So, yo can pupulate an Option Set from Query but not set a default value.
Anyway, default value field is not for OptionSets it's for text, numeric and DateTime responses.
I am not quite understand your last question.
Could you give more detail.
Long time since the question got asked.
The simplest answer is that you can´t set a default value to the option set.
However another sollution instead of duplicating the page, and that may be useful in a case like this, is a workaround with two queries.
Declare a variable of the type you want to be dynamic in the Query for the Option set.
Set the variable to a value that would give you the default Option set value you´re looking for.
Query for the data that you want to use with the dynamic type you want to use.
Check number of records the query returns.
If number of records is more than 0 you set the value of the variable to the dynamic vaule used in the query
Query for the same data again(!), this time with the variable instead of the previously used dynamic value, and use this one for the Option Set.
The result is: If you get hits from your query, the hits will be shown in the option set. If you don't get hits the "default" value will be shown.
Also don't think there is any reason why you wouldn't be able to set a default value for the Option set. Especially since the page locks up if the query used for the option set returns no hits.
Cheers.
/Henrik
Further to this I have come across a situation today where I have used some fetchxml to query data in a dialog and are now wanting to store that value within a variable but appears I cant and can only use this to show fields within an option set.
I appreciate the xml is returning a record and not a field but there must be away I can return first or default and store the returned value in a variable.

Resources