Page 1 of 1

Best method of auto filling

Posted: 24 Oct 2017
by Roo
I am very new to your SFD program and have found it a delight to use.

I'm trying to auto fill the current user name and Current date into the user and date fields when they tick a checkbox.

signoff.jpg
signoff.jpg (82.31 KiB) Viewed 1634 times

Re: Best method of auto filling

Posted: 24 Oct 2017
by Nikita Kurguzov
Hello, Roo!
You can try code like this:

Code: Select all

fd.field('Checkbox').change(function(){
	if(fd.field('Checkbox').value()){
	  	fd.field('By').value(_spPageContextInfo.userLoginName);
		fd.field('Date').value(new Date());
	}
	else{
		fd.field('By').value(null);
		fd.field('Date').value(null);
	}
});
Change field names to internal names of your fields and it should work.

Re: Best method of auto filling

Posted: 25 Oct 2017
by Roo
Thanks for your quick reply.

I've put this code into my form, but with success (I have substituted the internal names)

The field that I am looking to update are user and date fields - Will that cause issues?

I will carry on trying and never give up :D

Re: Best method of auto filling

Posted: 25 Oct 2017
by Roo
It did work - Thanks Nikita.

I set the two name and date fields as read only as I didn't want users to change things!!

Once I made the fields writable it was ok.

I guess I could do this in the code?

Re: Best method of auto filling

Posted: 25 Oct 2017
by Nikita Kurguzov
I guess I could do this in the code?
Yeah, exactly, it would be better to do it in the code. Make them readonly when you open the form, then set readonly to false prior to changing values and back to true once new values are set.

Something like this should work:

Code: Select all

fd.field('Title').readonly(true);

fd.field('Checkbox').change(function(){
	fd.field('Title').readonly(false);
	
	if(fd.field('Checkbox').value()){
		fd.field('Title').value('New Title');
	}
	else{
		fd.field('Title').value(null);
	}
	
	fd.field('Title').readonly(true);
});

Re: Best method of auto filling

Posted: 25 Oct 2017
by Nikita Kurguzov
One last thing, you also need to have all fields set to Readonly:false when you save an item, otherwise these values won't save correctly.

So, add something like this in the end of JavaScript editor:

Code: Select all

fd.onsubmit(function(){
  fd.field('By').readonly(false);
  fd.field('Date').readonly(false);
  return true;
});

Re: Best method of auto filling

Posted: 26 Oct 2017
by Roo
Awesome. Works exactly how I needed it.

Thanks Nikita.