cancel
Showing results for 
Search instead for 
Did you mean: 

Personas 2.0: How to change a date a year early using script?

Former Member
0 Kudos

Hi Personas 2.0 experts,

I am stacked in a problem to manipulate a date value using Personas 2.0:

After run iw28, at the field "Created on", one can set the date of today by clicking the calendar icon besides, say the today's date will be set as 19.01.2015 that is shown in the field afterwards.

Now what I want to do is to change this date one year before so that it will become 19.01.2014. Does anyone have any idea how to do such date changing using a script button in Personas 2.0 (Note: I don't want to do script recording way, but some way of using script to manipulate the value of that field)?

Thanks a lot in advance!

Dong Zhu

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

You need to do this in a JavaScript action. This should do:


var d = new Date();

args.lastyear = d.getDate() + "." + (d.getMonth()+1) + "." + (d.getFullYear()-1);

Then just use a Past Value action to put "lastyear" in the field you want.

If you want to manipulate the date value in the field, rather than always starting with today, then copy the value out first and pick out the year with a JS string operation and manipulate accordingly.

I don't think there's a way to do date calculations outside of JavaScript.

Steve.

Former Member
0 Kudos

Hi Steve,

Thanks for the quick reply.

I did try that way, but the Copy Value action did not put right result into a var inside a script button. I got the value "null" instead. I am in doubt if that field is a special field as its value is from the calendar component. Changing the fetched date has clearier logic but more difficult to do. Set the current date from directly script is more easier to do by JS. Anyway, maybe I did not do all the things correctly, why I am asking here to see if you have successfully done similar thing before with that field.

Thanks again, I am going to give another try.

Dong

Former Member
0 Kudos

There's nothing special about those fields in IW28 - they are just text fields that happen to have a calendar as F4 help. Here's some JavaScript to break apart a copied date value and decrement the year:


lastdot = args.olddate.lastIndexOf(".");

oldyear = parseInt( args.olddate.substr(lastdot+1, 4));

newyear = oldyear - 1;

args.newdate = args.olddate.substr(0,lastdot) + "." + newyear;

Before this you need a Copy Value action to copy the date into "olddate" and a Paste Value action afterwards to paste "newdate" back into the same field.

Steve.

Former Member
0 Kudos

Hi Steve,

Thanks for the tip. The problem is solved.

Manipulating the date string by JS is working. My essential problem was that I put all the manipulation script in a SB on the "home screen", and for some strange reason, the value fetched from the field was null, even I used the args.var and call it all inside one SB. I don't know why the value is not stored during one SB.

So the right solution is: always put a handling hidden SB on the screen where the SB makes changes of fields. Then call the press action from another SB on the home screen - then the values are preserved during the process.

Best regards,

Dong

Former Member
0 Kudos

I've seen that behaviour before, but not for a while. If you use "Refresh Screen" in all the right places you shouldn't have to resort to this.

I believe it is a bug and you should probably report it to OSS.

Steve.

Former Member
0 Kudos

Hi Steve,

Thanks. I will do some more investigation. 🙂

By the way, just some thought to discuss with you - as Personas is meant to be a good GUI tool to use, it would be a lot nice and easy if some of the hidden things (eg, the "global" vars claimed by args.xxx) can be visualised to the developers - eg, if there can be a storage widegt in Personas, where it is a singlton instance but can be placed at any screens, so that one can open it to set up all the global vars there, and can access them in the JS codes. I used variant UI tools for developing UI apps. That way is more straight forward.

Now it makes sense of local vars are "declared" inside script buttons (which can be treated as a function scope), but it is not obvious that those declared by args.xx having "global" scope. Anyway, we cannot do more about them, but live with the conditions.

Br,


Dong

Former Member
0 Kudos

If you are prepared to live with the restriction that it only works in a web browser, then you can store values in the global JavaScript "Window" object and they will then be accessible to scripts in any subsequent transactions, until the browser window is refreshed. See my blog for more details.

I say "only works in a web browser" because, although Personas v2 is only web based anyway, Personas v3 runs in the native Windows and Java SAPguis. There is no global "Window" object in the native GUIs so this trick won't work there.

Steve.

Answers (0)