Copying Fields With Checkbox

How about this:

I have an app with business locations. I need to have a physical address field group and a separate mailing address field group.

I also would like to have a checkbox the user can check if the mailing address is the same as the physical address, that will copy the text from the physical address fields and place those value sin the mailing address fields.

What am I doing wrong here?

 

(function() {
"use strict";

var MAILINGSAMEASPHYSICAL = 'mailingsameasphysical'; //checkbox field code - Only once checkbox, "Yes"
var PSTREETNUMBER = 'physicaladdressstreetnumber'; //Physical Address Street Number field code
var MSTREETNUMBER = 'mailingaddressstreetnumber'; //Mailing Address Street Number field code
//Skipping the other fields for berevity

//Event types that trigger the script when the checkbox is checked
//Not when the user saves the form, but when they check the checkbox
var events = ["app.record.create.change." + MAILINGSAMEASPHYSICAL, "app.record.edit.change." + MAILINGSAMEASPHYSICAL];

kintone.events.on(events, function(event) {

var record = event.record;

var mailingSameVal = record[MAILINGSAMEASPHYSICAL].value; //Get the value of the checkboxes

var pStreetNumberStr = record.physicaladdressstreetnumber.value; //Value of the physical address street number
var mStreetNumberStr = record.mailingaddressstreetnumber.value; //Value of the mailing address street number
//Skipping the other field for berevity

if (mailingSameVal.indexOf('Yes') >= 0) { //If the value of the Yes checkbox >= 0
mStreetNumberStr = pStreetNumberStr; //Take the value of the physical address street number and copy it into the value of the mailin gaddress street number
//Skipping the other field for berevity

} else { //If the value of the yes checkbox is not changed to >= 0
mStreetNumberStr = null; //Make the value of the mailing address street number blank
//Skipping the other field for berevity
}

return event;
});
}());

 

I have reviewed your code and noticed that you were trying to copy the value of the physical address street number to the mailing address street number at the following line.

mStreetNumberStr = pStreetNumberStr;

However, this does not change the field value because it only puts the value in the variable and the value of the event object is not overwritten.

*If you look at the contents of the event before the return event, you can see that the value has not changed.

This is a JS constraint, but when you put a variable in a variable and pass it by a type other than an object(I think that it is a string type because it is an address.), the original value is not overwritten.

With your code, a variable is put in a variable a few times,but it is passed in a way that's not an object at the following line,
so it ends up that an event object is not overwritten.

var mStreetNumberStr = record.mailingaddressstreetnumber.value;

Therefore, I would suggest changing the following two lines as below to make it work.

Current: var mStreetNumberStr = record.mailingaddressstreetnumber.value;
Change to: var mStreetNumberStr = record.mailingaddressstreetnumber;

Current: mStreetNumberStr = pStreetNumberStr;
Change to: mStreetNumberStr.value = pStreetNumberStr;

Regarding the above restrictions, the details are described below.
https://codeburst.io/explaining-value-vs-reference-in-javascript-647a975e12a0

Got it! Thanks! You guys are amazing!