I have a form (NewForm.aspx) that I would like to have fields auto-populate based on a “single line of text” filed information.
To further explain, I want to be able to enter an account number into my “Account Number” single line of text filed, and upon doing that, the other fields (Customer and Contact Person)will auto populate with the Customer Name (which is the name of the business) and the contact person (which will contain either a persons name, or email address).
For example:
I go to my list and press “New Item”, which then displays the NewForm.aspx to fill out.
I want to be able to type in an account number, lets say 44656723, into the “Account Number” field
After doing that the Customer field auto-populates with, for example, “Microsoft” and the Contact Person field also auto-populates with “Bill Gates” or “BigDawgBillGates@microsoft.com”.
The info (name or email) being auto-populated into the “Contact Person” field will be based on what we have on file from where this data needs to be pulled from. I’m guessing it’ll be another list or something.
Hi, Wayne.
You may want to check if Mark Rackley’s Lookup fields blog post will work for you. Check out the YouTube video on his page.
https://info.paitgroup.com/blog/set-lookup-fields-in-edit-forms-for-large-lists-in-sharepoint
Also, you may want to try if PowerApps can achieve your requirement if you’re already using Office 365, or use a 3rd party tool that can create advanced forms. Good luck!
After some more research, I am thinking about possibly taking this a different direction and using lookup fields. One thing I would like to do though is change the primary lookup field “Account” to a text box rather than a drop-down. I have done a horrendous amount of research on this and I cant seem to find an actual solution for this. This way, I can simply type in an account number into the NewForm, press “Save” and all of the other info (Customer, Contact Person/Email, etc.) would auto-populate on the list. I can achieve this right now, it’s just that on the NewForm, “Account” is a drop-down that shows all of the account numbers rather than a text box to type in the account numbers. If someone used a drop-down and had 100+ account numbers, you can see how that would be a pain to deal with. Any ideas how to change the primary lookup field “Account” to a text box rather than a drop-down?
- Open your first list.
- Click on list tab > Form Web Part > Default New Form.
- The new form is now ready for edit, click on Add web part.
- Click on Media and Content > Add Script editor web part.
- Click on Edit Snippet > add the below code.
<
script
language
=
"javascript"
<br />
src
=
"
http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js
"
><br />
</script
>
<
script
language
=
"javascript"
>
$(document).ready(function
() {//
the lookup field in the new formvar
IssueIDField= $("select[title='Issue ID']");IssueIDField.change(function
() { Populate(); });});
var
ListItem;function
Populate() {var
IssID = $("select[title='Issue ID']").val();var
clientContext = new SP.ClientContext.get_current();var
IssueList = clientContext.get_web().get_lists().getByTitle('Issues');var
camlQuery = new SP.CamlQuery();camlQuery.set_viewXml('<
View
><
Query
><
Where
><
Eq
>
<span style="font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', <br />
Courier, <br />
monospace;"><span style="font-size: medium;"><</span></span>FieldRef
Name='ID'
/> <br />
<Value
Type='Text'>'
+ IssID +
'</Value
></
Eq
></
Where
></
Query
><
RowLimit
>20</
RowLimit
></
View
>');
ListItem
= IssueList.getItems(camlQuery);clientContext.load(ListItem);
clientContext.executeQueryAsync(Function.createDelegate(this,this.Succed),<br />
Function.createDelegate(this,this.Failed));}
function
Succed(sender, args) {var
listItemEnumerator = ListItem.getEnumerator();while
(listItemEnumerator.moveNext()) {var
LItem = listItemEnumerator.get_current();$("input[title='Issue
Name']").val(LItem.get_item("Title"));}
}
function
Failed(sender, args) {alert('Error.
' + args.get_message() + 'n' + args.get_stackTrace());}
</
script
>
- Change the ‘Issue ID’ based on the lookup field name in your new form
1 var IssueIDField= $("select[title='Issue ID']");
- Again, do the same, to get the value of the selected lookup value.
1 var IssID = $("select[title='Issue ID']").val();
- In function “Succeed”, change the “Issue Name” to the field name that you would set it in the current new form, and change the “Title” with the field that you would get from the second list based on the Lookup selected value
1 $("input[title='Issue Name']").val(LItem.get_item("Title"));
Note: the field name that set in this line “LItem.get_item(“Title”)” is the Internal Field Name, it’s not the displayed name, so if you have field Called for example “M Qassas” it should be “M_x0020_Qassas“
Repeat, the above line in case you have more than one field need to be populated.
Ive followed Valibhav’s instructions but for me its still not working. When I select a value from the dropdown, my fields are not populated. Im on SPO.