1

I am trying to implement same article as "Autofill column values of a list based on another list column value" Autofill column values of a list based on another list column value but unable to get the results as it's shown. I am working on SPOnline. Do I have to add any other libraries. Please help

I am using survey list rather than custom list.

  • I'm assuming you are talking about this other question, right?. If that is the case, could you please edit the question to add a link to said question - right now it is a little unclear what "article" you are referring to. Also, I fear that currently your question is still lacking additional info that could help tacking down the problem you are facing. Do the lSharePoint logs or the browser developer console signal anything noteworthy? – SPArchaeologist-様 Jan 23 at 15:48
  • I am working on survey list instead of custom list and trying to autofill survey list – Neha Jan 23 at 16:15
1

Try using the following scripts:

Note: Change list name and fields to yours:

<script language="javascript" src="https://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
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
        var ItemIDField= $("select[title='lookup-cl02']");
        ItemIDField.change(function () { Populate(); });
    });
}); 

var ListItem;
function Populate() {
    var ItemID = $("select[title='lookup-cl02']").val();
    var clientContext = new SP.ClientContext.get_current();
    var SourceList = clientContext.get_web().get_lists().getByTitle('cl02');
    ListItem = SourceList.getItemById(ItemID);
    clientContext.load(ListItem);
    clientContext.executeQueryAsync(Function.createDelegate(this,this.Succed),Function.createDelegate(this,this.Failed));
} 

function Succed(sender, args) {
    $("input[title='columnx']").val(ListItem.get_item("columnX"));
    var myDate = new Date(ListItem.get_item("PublishDate"));
    console.log(myDate);
    var myFormattedDate = myDate.format("MM/dd/yyyy");
    $("input[title='PublishDate']").val(myFormattedDate);
} 

function Failed(sender, args) {
    alert('Error. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>

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.