Formatting and Adjusting Numbers
I have some Actions on a Save button Behavior for a new form.
After "Save Form" I have a "Update List Item".
My List item ID is
=[[@Web.GetFirstValueForQuery('Purchase Orders', '<Where><Eq><FieldRef Name=Author LookupId=True /><Value Type=User ><UserID/></Value></Eq></Where><OrderBy><FieldRef Name=Created Ascending=False /></OrderBy>', 'ID')]]
...and I then assemble a PO Number
[[For_Vessel.Vessel_Initials]]-[[ID]]
This works fine and gives me a Purchase Order Number like "HA-12"
I'd like to format the [[ID]] so it looks something more like "HA-00012", and ideally, add a specified value, such as "1000" so I can have "HA-01012" and make it compatible with the client's present numbering system.
How would I write the expression for that?
-
Hi Michael,
yes that is actually pretty easy.
In the first case, change the expression type to the "Assignment" and use the following expression:
[[For_Vessel.Vessel_Initials]] + "-" + ('00000' + [[ID]]).slice(-5)Note that there are 5 zeros in the "prepended" zeros ("00000") and .slice() block also needs to be set to -5.
You can do the same with "1" part:
[[For_Vessel.Vessel_Initials]] + "-" + ('01000' + [[ID]]).slice(-5)This will result with HA-01012
Best, Adis
0 -
Thanks for the reply! This seems to be string manipulation though, yes? So the numbers get a little messed up when you go from [[ID]] = 1 to 10 to 100, if I understand correctly?
[[Vessel.Vessel_Initials]] + "-" + ('04000' + 1).slice(-5)
Evaluation succeeded!Value: "HA-40001"
[[Vessel.Vessel_Initials]] + "-" + ('04000' + 10).slice(-5)
Evaluation succeeded!Value: "HA-00010"
[[Vessel.Vessel_Initials]] + "-" + ('04000' + 100).slice(-5)
Evaluation succeeded!Value: "HA-00100"
Is there a way that I can format a number?
0 -
Handle the static part as string and the dynamic leading zeros seperately.
Check this out:
function pad (str, max) {
str = str.toString();
return str.length < max ? pad("0" + str, max) : str;
}
return "HA-04"+pad([[ID]], 3);Results (3, 11, 999): HA-04003, HA-04011, HA-04999
0
Please sign in to leave a comment.
Comments
3 comments