HTML Client Lightswitch: Assign value to modal picker upon screen initialization

After conducting a considerable amount of research on this issue, I have found that none of the examples provided are helpful or applicable. My goal is to have the Details Picker display a specific name when the Add screen loads, instead of requiring the user to select it every time. I believe this can be accomplished, but my JavaScript skills are not up to par.

Any assistance would be greatly appreciated. Below is an example:

In this scenario, the name is stored in a table and can be searched for in the modal picker/details picker. However, manually adding it each time is both time-consuming and tedious if it's needed 50% of the time.

A text box can usually be manipulated using contentItem.value or element.innerText after pressing post render on each item. Unfortunately, this approach does not work with this type of control, resulting in an error message.

Here is some useful information that might assist:

  • ProjectData (datasource)
  • Main screen references OrderRequest
  • The foreign key link references ShippingContact, and CustomerName is searchable in the DetailsPicker

Do I need to substitute anything in the top function based on the answer provided below? Additionally, regarding the second part of the code where you mentioned

defaultLookup(screen.Customer, "Contact", "Contacts",
what should be included here?

This is an example of what I'm attempting to change, although unfortunately, it isn't working as intended:

var defaultValue = "Test User";
    var filter = "(ContactName eq " + msls._toODataString(defaultValue, ":String") + ")";
    defaultLookup(screen.OrderRequest, "ContactName", "ShippingContacts", { filter: filter });

Answer №1

After facing a similar requirement, we implemented a helpful function that did the job: -

function defaultLookup (entity, destinationProperty, sourceCollection, options) {
    /// <summary>
    /// Sets a default lookup property for an entity
    /// </summary>
    /// <param name="entity" type="Object">The entity with the lookup property to set</param>
    /// <param name="destinationProperty" type="String">The property to set as the default lookup on the entity</param>
    /// <param name="sourceCollection" type="String">The collection from which to get the default value for the lookup</param>
    /// <param name="options" type="PlainObject" optional="true">
    /// A set of key/value pairs used for additional configuration options. All options are optional.
    /// <br/>- String filter: Defines the match condition for the required default, if not supplied, the lookup defaults to the first entry in the source collection
    /// </param>
    options = options || {}; // Ensure options is an object
    var source = myapp.activeDataWorkspace.ApplicationData[sourceCollection]; // DataServiceQuery
    var query = {}; //DataServiceQuery
    if (options.filter) {
        query = source.filter(options.filter);
    } else {
        query = source.top(1);
    }
    query.execute().then(function (result) {
        entity[destinationProperty] = result.results[0];
    });
};

If you need it for your case, change ApplicationData to ProjectData.

You can invoke this in the created event of your screen like so: -

myapp.AddEditCustomer.created = function (screen) {
    var defaultValue = "Chris Cook";
    var filter = "(Name eq " + msls._toODataString(defaultValue, ":String") + ")";
    defaultLookup(screen.Customer, "Contact", "Contacts", { filter: filter });
};

In your scenario, ensure screen.Customer is changed to screen.OrderRequest, "Contact" becomes "CustomerName," and "Contacts" turns into "ShippingContacts." Also, if your lookup table has a field named ContactName, adjust the filter string to reference ContactName specifically.

Alternatively, call this helper function in your entity's created events (in the UserCode script section) like this: -

myapp.Customer.created = function (entity) {
    var defaultValue = "Chris Cook";
    var filter = "(Name eq " + msls._toODataString(defaultValue, ":String") + ")";
    defaultLookup(entity, "Contact", "Contacts", { filter: filter });
};

In this code snippet, the main table is "Customers" and the lookup table is "Contacts." The "Contact" field in Customers references an entry in Contacts, where a record with "Name" set to "Chris Cook" exists (defaultValue and filter variables address this situation).

The below image illustrates the debugging of the screen.Customer property: -

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The only thing visible on my project is the homepage, void of any buttons or additional pages

After completing this school project, I believed that everything was done correctly. However, as I faced issues with the code, I decided to seek help and share my app.js and bin section for clarification. Starting it with npm on the localhost as shown in ...

Having trouble with my ASP.Net OnClick Subs not functioning as desired

I have implemented onClick handlers to process button clicks that query SQL. The issue I am facing is that these queries sometimes take between 10 to 30 seconds to return a response. To prevent click-stacking during this time, I disabled the buttons. Howev ...

What is the method for setting a title within a for-loop when using vue.js in the given scenario?

<template v-for="item in job"> <tr> <td v-for="i in item.stage" :style="getStyle(i.status.name)" title="[[ i.node.name ]]" > <b>[[ i.node.name ]]</b> </td> </tr> </template> ...

The PHP-generated form is not able to submit via AJAX

My select element includes ROWID in the value along with a name in the value field. <select name="opcoes" onchange="showInfo(this.value)"> <option value=''>Select</option> <option value=6>A</option> <option valu ...

Is it possible to verify if a function is invoked using Jest, Typescript, and ts-jest in action?

Currently, I'm testing the functionality of this code snippet: src/helpers/CommentHelper.ts: export default class CommentHelper { gitApiObject: GitApi.IGitApi ; constructor(gitApiObject: GitApi.IGitApi) { this.gitApiObject = gi ...

Align images at the center of a division using the Bootstrap framework

I'm facing an issue with centering social network icons under a div in my login form while keeping it responsive. Can someone please assist me with this problem? Please help me!!. .row { background: #f8f9fa; margin-top: 20px; } .col { bor ...

Having trouble with JSON parsing in Promise execution

I am in the process of developing a Promise with the objective of adding up any numbers discovered within an array or JSON object. The add() function is designed to receive a series of URLs as a string input and calculate the total sum of those URLs. Her ...

How can the Java ScriptEngine utilize values from the Java side?

Within my Java program, I am calling a custom JavaScript program: File userJSFile=...; javax.script.ScriptEngineManager mgr=new ScriptEngineManager(); javax.script.ScriptEngine scripEngine= mgr.getEngineByExtension("js"); Object result=scripEngine.eval(n ...

What methods are commonly used to calculate the bitsPerSecond rate for media recording?

Is there a specific formula that combines frames per second and resolution to determine the bits per second for video encoding? I'm struggling to figure out the appropriate values to use for specifying the bits per second for 720p, 1080p, and 4k video ...

Tips for customizing the appearance of various classes in Bootstrap 4

I am struggling to personalize the Bootstrap switch due to the middle toggler limitation. For customization purposes, I aim to increase the width and height of the switch to ensure visibility across various resolutions. My approach involves utilizing clas ...

Are there any missing (or "optional") expressions in handlebars.js?

Currently, I am developing a build script using Node.js. To summarize, the script carries out the following tasks: Prompting the user for essential information such as the project name and description Cloning a template from a Git repository Renaming fil ...

Step-by-step guide on repairing a countdown clock using JavaScript, HTML, and

I'm having issues with my JS on my website. I am new to this and currently in the process of setting up a website. I want to add a timer to show when the site will be active. It seems like there is an error somewhere in the JS code that I can't ...

How can I access the data variables from a JSON request within a function?

My task involves loading multiple JSON files from an array called bunchOfData, with each file having a corresponding URL. How can I access my variable "myI" within the processData function? for(var i = 0; i < bunchOfData.length; i++){ $.getJS ...

Steps for adding a class to an element in VueJS

https://codepen.io/nuzze/pen/yLBqKMY Here's my issue at hand: I currently have the following list stored in my Vue data: { name: 'Camp Nou', id: 'campNou' }, { name: 'Abran cancha', id: 'abranCancha ...

Pattern for either one or two digits with an optional decimal point in regular expressions

Currently, I'm utilizing for input masking. I am specifically focusing on the "patterns" option and encountering difficulties while trying to create a regex expression for capturing 1 or 2 digits with an optional decimal place. Acceptable inputs: ...

Having trouble with the page redirection issue? Here's how you can troubleshoot and resolve

My goal is to restrict access to both the user home and admin dashboard, allowing access only when logged in. Otherwise, I want to redirect users to the login or admin login page. import { NextResponse } from 'next/server' import { NextRequest } ...

Is it possible to determine the cursor index using getSelection() while hovering over text in a Quill.js editor?

In my web project, I am using the Quill.js rich text editor. I am familiar with the quill.getSelection() method for retrieving the cursor index. However, I would like to retrieve the cursor index without requiring user interaction, such as clicking. Specif ...

Creating a fresh JSON object while making a glob request in Express/Node.js

As a newcomer to JavaScript, I'm grappling with an issue related to storing the state of an object and the intricacies of nested function calls. My task involves iterating through a directory filled with numerous JSON files, loading them one by one, ...

How can the parameters -i -u "clientId:" be interpreted when translating a curl command into Ajax?

As I work on integrating a 3rd party API into my website, I am currently in the testing phase using Postman (the Chrome Extension) before proceeding to write my AngularJS Service with $http. However, there is one aspect of the process that has me puzzled. ...

Preventing Wizard from Redirecting to the Following Page

i am currently using a wizard with two-panel form for performing data CRUD operations through ajax and asp.net. Currently facing an issue: How can I prevent the wizard from automatically moving to the next page upon successful submission? Here's the ...