Best method of auto filling

Discussions about Forms Designer for SharePoint 2013 / 2016 and Office 365.
Locked
User avatar
Roo
Posts: 107
Joined: Tue Oct 24, 2017

24 Oct 2017

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 1633 times

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

24 Oct 2017

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.
Cheers

User avatar
Roo
Posts: 107
Joined: Tue Oct 24, 2017

25 Oct 2017

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

User avatar
Roo
Posts: 107
Joined: Tue Oct 24, 2017

25 Oct 2017

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?

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

25 Oct 2017

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);
});
Cheers

User avatar
Nikita Kurguzov
Posts: 889
Joined: Mon Jul 03, 2017

25 Oct 2017

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;
});
Cheers

User avatar
Roo
Posts: 107
Joined: Tue Oct 24, 2017

26 Oct 2017

Awesome. Works exactly how I needed it.

Thanks Nikita.

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 2 guests