Having had the same requirement we've implemented the following helper function: -
function defaultLookup (entity, destinationPropertyName, sourceCollectionName, options) {
/// <summary>
/// Defaults an entity's lookup property
/// </summary>
/// <param name="entity" type="Object">The entity featuring the lookup property to default</param>
/// <param name="destinationPropertyName" type="String">The lookup property against the entity to default</param>
/// <param name="sourceCollectionName" type="String">The collection from which to source the lookup value</param>
/// <param name="options" type="PlainObject" optional="true">
/// A set of key/value pairs used to select additional configuration options. All options are optional.
/// <br/>- String filter: If supplied, defines the match condition for the required default, otherwise the lookup defaults to the first entry in the source collection
/// </param>
options = options || {}; // Force options to be an object
var source = myapp.activeDataWorkspace.ApplicationData[sourceCollectionName]; // DataServiceQuery
var query = {}; //DataServiceQuery
if (options.filter) {
query = source.filter(options.filter);
} else {
query = source.top(1);
}
query.execute().then(function (result) {
entity[destinationPropertyName] = result.results[0];
});
};
In your case, you'll need to change the ApplicationData to read ProjectData.
This can be called in your screen's created event as follows: -
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 case, screen.Customer should be changed to screen.OrderRequest, "Contact" should be changed to "CustomerName" and "Contacts" should be changed to "ShippingContacts". Also, based on your lookup table having a field called ContactName, the filter string needs to reference ContactName rather than just name.
Alternatively, this helper can be called from your entity created events (in your UserCode script section) as follows: -
myapp.Customer.created = function (entity) {
var defaultValue = "Chris Cook";
var filter = "(Name eq " + msls._toODataString(defaultValue, ":String") + ")";
defaultLookup(entity, "Contact", "Contacts", { filter: filter });
};
In my code example, the main table is called "Customers" and the lookup table is called "Contacts". The "Contact" field in the main table references an entry in the Contacts table. The Contacts table has a field called "Name" and a record with the name set to a value of "Chris Cook" (the defaultValue and filter variables refer to this situation).
The following image shows the screen.Customer property being debugged: -
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…