populating field value from another list with javascript

Tyler Lachaussie 21 Reputation points
2022-07-06T18:47:23.153+00:00

I have a list called 'quotes' and list called 'autoquotenumber'. 'autoquotenumber' only has one item id=1 which is used to keep track of the quote numbers. I would like to retrieve a column from 'numbers' list whose column name is 'lastnumber' and use it in my quotes list to populate a field called 'quote number' when entering a new item.

I have this functioning with a flow perfectly, however the time it takes for the flow to kick off is too long sometimes a couple of minutes. I would like to try to get this working with javascript if possible.

Unfortunately I'm still learning a lot about this, so any help I can get is appreciated.

I've been trying to piece some things together, and thought i might be getting close with this:

var clientContext = null;  
var web = null;  
ExecuteOrDelayUntilScriptLoaded(AutoGenerate, "sp.js");  
  
function AutoGenerate() {  
    clientContext = new SP.ClientContext.get_current();  
    web = clientContext.get_web();  
    var list = web.get_lists().getById(_spPageContextInfo.pageListId);  
    var camlQuery = new SP.CamlQuery();  
    camlQuery.set_viewXml('<View><Query></Query></View>');  
    this.listItems = list.getItems(camlQuery);  
    clientContext.load(listItems);  
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onListItemsLoadSuccess),  
        Function.createDelegate(this, this.onQueryFailed));  
}  
  
function onListItemsLoadSuccess(sender, args) {  
    var itemId = 1;     
    var targetListItem;  
    var clientContext = new SP.ClientContext();   
    var targetList = clientContext.get_web().get_lists().getByTitle('AutoQuoteNumberTest');  
    targetListItem = targetList.getItemById(itemId);  
    var AutoIncremental = "Quote-" + clientContext.load(targetListItem, 'lastnumber');  
    $("input[title^='Quote Number']").val(AutoIncremental);  
    $("input[title^='Quote Number']").attr('disabled', 'disabled');  
}  
  
function onQueryFailed(sender, args) {  
    alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());  
}  

Unfortunately the lastnumber is showing as undefined

218322-quote.jpg

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,203 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,289 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,246 Reputation points
    2022-07-07T06:52:16.5+00:00

    Hi @Tyler Lachaussie ,

    Per my test, I changed your code and successfully got the value of lastnumber, I have put the code in the attachment, please take note and refer to it.

    I also recommend you can use the LookUp column to populating field value from another list, please refer to the steps:

    1.Click Add column in 'quotes' list.
    Type: Lookup
    Select a list as a source: autoquotenumber
    Select a column from the list above: lastnumber
    218465-image.png
    2.Then ,when you add a new item in the "quotes" list, the 'quote number' is populated with the 'lastnumber' value from the "autoquotenumber" list.
    218482-image.png


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    0 comments No comments

  2. Tyler Lachaussie 21 Reputation points
    2022-07-07T13:14:26.537+00:00

    I'm trying to actually build a sequential number for each item. Because of this the lookup does not work. i actually messed up in my original post and left that information out. I have two columns in my autoquotenumber list: lastnumber and nextnumber. I would pull nextnumber which is a calculated column in the autoquotenumber list as lastnumber+1. Then basically what I do is update the item with a flow so that nextnumber becomes lastnumber and the sequential number keeps going.

    I will try your code @Tong Zhang_MSFT

    My second question would be is there a way to update the autoquotenumber list item with javascript on successful save of the form to avoid using my flow so that nextnumber replaces lastnumber. As I mentioned earlier the flow takes too long to kick off since we moved to sharepoint online. With on premise sharepoint workflows kicked off immediately and we did not have any issues. Our employees using this list like to work too fast, and any delay in the generation of this number creates duplicate quote numbers.

    0 comments No comments

  3. Tyler Lachaussie 21 Reputation points
    2022-07-07T15:00:04.463+00:00

    @Tong Zhang_MSFT the code works fine. It does take 30 seconds or so to load the form however. Is there any reason this might be loading so slow with that code? I am not sure it will work for me if it is so slow to open to form.

    0 comments No comments

  4. Tyler Lachaussie 21 Reputation points
    2022-07-07T15:31:22.55+00:00

    @Tong Zhang_MSFT disregard my comment about the speed. Your code has some scripts in it that were not available on my server. Once I removed them everything works as expected...

    Now to figure out if I can update lastnumber with nextnumber from my autoquotenumber list on save without the flow...


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.