Skip to main content
Known Participant
August 30, 2017
Answered

Help with grabbing a number from 1 place and adding it to another with a number behind it.

  • August 30, 2017
  • 1 reply
  • 1379 views

So, i got a button which grabs a employee number from a field and adding it to another field. I want my from to add a number behind the number it just grabbed, but so far i can only get it to add the number to current number instead of putting it behind it. Everything else i've done is wrking (Got an alert if the field the button grabs data from is empty and got it to grab the number and paste it in the field) but from here im lost. Everything i've tried just aint working. This is my current code.

var f = this.getField("Medarbejder nr").valueAsString;

var v = this.getField("Løbenummer").valueAsString;

var msg = "Udfyld medarbejder nummer først";

if (this.getField("Medarbejder nr").valueAsString=="") {

this.getField("Løbenummer").valueAsString=="" && app.alert(msg,1)}

else {

    this.getField("Løbenummer").value = util.printf(this.getField("Medarbejder nr").value +1);

}

I understand the +1 in the last line is what is wrong, but i dont know what to replace it with.

Thanks.

Edit: My boss just informed me that he only want the last 3 digits of the field "Medarbejder nr" then add 3 numbers behind it starting with 001. Help with this aswell would be greatly appriciated.

This topic has been closed for replies.
Correct answer try67

So you want that if the "Løbenummer" field is empty it should be the last three digits of "Medarbejder nr", followed by "001", and otherwise its value should increase by one?

Yes and no. If "Løbenummer" is empty and they click the button it should take the last three numbers of "Medarbejder nr" and add 001 to it in the end. Next time they click the button it should put 002 instead of 001 in the end. So a counter. If "Medarbejder nr" is empty they button should do nothing at all, which is why i had an alert in my script telling them to fill "Medarbejder nr" first. Sorry for the danish in the code, i really appriciate your help.

I tried making a textfield with a counter there goes up by 1 everytime the button is clicked and it works, but it removes the 0s in the beginning (If i put 001 in the field and click the button it changes it to 2)


Try this code:

if (this.getField("Løbenummer").valueAsString=="") {

    var f = this.getField("Medarbejder nr").valueAsString;

    if (f=="") {

        app.alert("Insert your error message here.");

        this.getField("Løbenummer").value = "";

    } else {

        var medarbejdersplit = f.substr(-3);

        this.getField("Løbenummer").value = (medarbejdersplit + "001");

    }

} else {

    this.getField("Løbenummer").value = Number(this.getField("Løbenummer").valueAsString)+1;

}

1 reply

try67
Community Expert
Community Expert
August 30, 2017

I don't understand if you're asking about adding/subtracting a value to a number, or about adding text before/after another text...

The util.printf command in your code is useless, since you're missing the parameter that tells it in what way to format the number, so it just returns the same thing, only as a string. Instead of doing that just use the field's valueAsString property.

Known Participant
August 30, 2017

I have 2 fields, "Medarbejder nr" and "Løbenummer" , "Løbenummer" has to be a part of "Medarbejder nr" and 3 digits after that starting with 001. Right now i've gotten it to split whatever is in "Medarbejder nr" so i only get the last 3 digits of that but im having trouble getting the script to add 001 after those 3 digits ive gotten from "Medarbejder nr"

"Medarbejder nr" looks like this: 15129

I want "Løbenummer" to be like this: 129001 when they click my button. I can only get it to be: 1291 and its not going up when i click the button (Every click should make it go up to 129002, 129003, etc.)

Current code:

var f = this.getField("Medarbejder nr").valueAsString;

var v = this.getField("Løbenummer").valueAsString; 

var msg = "Udfyld medarbejder nummer først";

var medarbejdersplit = f.substring(2)

if (this.getField("Medarbejder nr").valueAsString=="") {

this.getField("Løbenummer").valueAsString=="" && app.alert(msg,1)}

else { 

    this.getField("Løbenummer").value = (medarbejdersplit +1);

}

try67
Community Expert
Community Expert
August 30, 2017

- You need to use strings instead of numbers. So instead of this:

this.getField("Løbenummer").value = (medarbejdersplit +1);

Use:

this.getField("Løbenummer").value = (medarbejdersplit + "001");

So you want that if the "Løbenummer" field is empty it should be the last three digits of "Medarbejder nr", followed by "001", and otherwise its value should increase by one? If so, use this code instead of what you currently have:

var f = this.getField("Medarbejder nr").valueAsString;

var medarbejdersplit = f.substr(-3);

if (this.getField("Løbenummer").valueAsString=="") {

    this.getField("Løbenummer").value = (medarbejdersplit + "001");

} else {

    this.getField("Løbenummer").value = Number(this.getField("Løbenummer").valueAsString)+1;

}