0

I need to implement Copy option on newform. I have a form that has a field as "existing issue id" which is a lookup to issue ids of the same list.

On selection of id on form, I need to prefill fields with the values of existing issue.

How do I achieve it without workflow?

5

Try to do the following:

  • Edit your new form,
  • Add Script Editor web part,
  • Add the below script.

[Script]

<script language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script language="javascript">

$(document).ready(function () { 
// the lookup field in the new form
var 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> <FieldRef Name=\'ID\' /> <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),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>

Output

enter image description here

All detail steps, I have mentioned at AUTOFILL LIST FORM FIELDS BASED ON LOOKUP SELECTION IN SHAREPOINT

|improve this answer|||||
  • This is nice and thanks for efforts. But I do not have two lists. I am working on a single list. What change is required then? – Sanjana Chhabira Jan 11 '18 at 17:21
  • You are welcome :) @SanjanaChhabira Just set the current list name at this line instead Issues var IssueList = clientContext.get_web().get_lists().getByTitle('Issues'); – Mohamed El-Qassas MVP Jan 11 '18 at 17:27
  • I did it. But not working. Also, on this line - $("input[title='Issue Name']").val(LItem.get_item("Title")); ---- I can use any column I want to set, right? like mobile number etc... – Sanjana Chhabira Jan 11 '18 at 17:33
  • Issue Name is the field that you need to set, Title is the field that you need to get, If you find any difficulties to edit the code, Please share your columns name and your current list name to modify it for you! – Mohamed El-Qassas MVP Jan 11 '18 at 17:36
  • Ohh it worked. Thanks a lot :) One more query... How to copy person field and lookup fields in same way? It just gives values object Object. – Sanjana Chhabira Jan 11 '18 at 17:37
1

best solution that i would think of that i have done in the past is to create a new newform.aspx based off the original. Then use Javascript to run when an item is selected to query a sharepoint list to pull the information that you need and then fill all the fields in the form.

its not entirely complex but takes a little coding to get it working. To create the new form you would do that in sharepoint designer. as for the code you can either do it in SPD aswell or goto the form in sharepoint and edit the page and add a script editor.

so to recap.

1) create a new form. set it as default.

https://social.technet.microsoft.com/wiki/contents/articles/23955.sharepoint-2013-building-custom-forms.aspx

2) use the browser, goto the new form and hit f12... in developer tools get the id of the control. and in javascript get the value when selected like:

How to show an alert whenever an Item is selected in SharePoint drop down choice menu Field in a SharePoint list

3) create another function that is called from the one above passing the value to query a list to get all the data that you need:

https://msdn.microsoft.com/en-us/library/office/hh185007(v=office.14).aspx

4) when you get the values from above put them into the controls via its ID.

https://stackoverflow.com/questions/5700471/set-value-of-input-using-javascript-function

sorry its not one full solution. had a long day but above should work perfectly fine.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.