Automatically select a newly created record in a queue

  • 1
  • Question
  • Updated 3 years ago
There are two models, one containing a list of existing records used to populate a queue and one used in a popup to create new records. When the queue is clicked, the corresponding item is opened in a page include.

There is a Model action that requeries the "List" model whenever the "New" model is saved, that way the newly created record appears in the queue.

But how do you automatically select the newly created record so that it's visible in the page include?
Photo of JD Bell

JD Bell, Senior Product Engineer

  • 2,996 Points 2k badge 2x thumb

Posted 4 years ago

  • 1
Photo of JD Bell

JD Bell, Senior Product Engineer

  • 2,996 Points 2k badge 2x thumb
This is possible using a JavaScript snippet. After saving the "New" model, the newly created record's Id field will be automatically updated, giving us the Id of the new record. After refreshing the "List" model, we can use a bit of javascript to search the Queue for the corresponding item, then mimic a mouse click event with jQuery. Whatever actions are associated with the queue item will then be executed.

Here's the JavaScript:

var $ = skuid.$,
    // Get the ID of the newly created item from the "New Item" Model. You can
    // get this ID however is appropriate for your particular implementation.
    newItemModel = skuid.$M( 'NewContact' ),
    newItemId = newItemModel ? newItemModel.getFieldValue( newItemModel.getFirstRow(), 'Id' ) : false,
    // Get the Queue containing the item to select by the Queue's UniqueID.
    queueElm = skuid.$C('ContactsQueue'),
    queueObj = queueElm ? queueElm.element.data('object') : false,
    // Get the items that are currently visible in the queue. Can't do much if
    // the item isn't visible.
    items = queueObj ? queueObj.editor.lists[0].visibleItems : [];
    
if (!newItemId || !items) return;
    
$.each( items, function(index,item){
    if ( item.row.Id === newItemId ){
        // If the newly created item is found in the queue, then simulate a
        // mouse click on it. Whatever actions you have setup (PageInclude,
        // drawer, etc) will be executed.
        item.element.click();
        // Stop searching the queue...
        return false;
    }
});

Just call this snippet after refreshing the "List" model (so that the newly created record will be in the queue) but before you clear out the "New" model (so that you can still get the new record Id). Also, you need to make sure that your "List" model is sorted in such a way that newly created records will be visible when the model is re-queried. If the item isn't visible in the Queue, then it can't be clicked (and this snippet will just be ignored).
Photo of Brian Keller

Brian Keller

  • 148 Points 100 badge 2x thumb
This works great!  Would it be possible to also scroll the queue to the selected item as well?