通过在打开窗体的 URL 中指定值来设置新记录的默认值。 默认情况下,窗体设置这些值,但用户可以在保存记录之前更改这些值。
传递参数以设置列记录值
注释
Xrm.Navigation.使用 openForm 函数将参数值传递给窗体以设置列值。 有关示例,请参阅 示例:使用 Xrm.Navigation.openForm 打开新窗口。
使用 URL 地址打开新窗体时,请在 extraqs 参数中包含参数来设置列值。 必须满足以下要求:
- 对您传入的
extraqs参数进行编码。 要对参数进行编码,请使用 encodeURIComponent。 若要使用特殊字符(如=或&参数值中),请对它们进行双重编码。 例如,若要设置为nameA=B&C,请使用extraqs=name%3DA%253DB%2526C。 - 查询字符串参数的名称必须匹配或包含表的列的名称。
- 传递的值必须有效。
- 该值不能是脚本。
- 任何尝试传递无效的参数或值都会导致错误。
- 对于布尔列,请使用整数值
0或1,或文本值true或false来设置值。 - 对于 DateTime 列,请使用日期的文本值。
示例:设置字符串列的值
以下示例将新帐户记录的 Name 列的值设置为“新建帐户”。
参数的未编码值为 extraqsname=New Account.
/main.aspx?etn=account&extraqs=name%3DNew%20Account&pagetype=entityrecord
设置查阅列的值
下表描述了五种类型的查找列。 有关使用查阅列的示例,请参阅 示例:设置查阅列的值 和 示例:使用 Xrm.Navigation.openForm 打开新窗口。
| 查找类型 | 说明 |
|---|---|
| 简便查找 | 对一种类型的表的单一引用。 |
| 客户查询 | 对账户或联系人记录的单个引用。 |
| 所有者查找 | 对团队或系统用户记录的单个引用。 |
| 参与者列表查找 | 对多个表的多个引用。 |
| 有关查找 | 对多个表的单个引用。 |
使用查询字符串参数在表单上设置查找值时,适用以下准则:
对于简单查找,请设置要在查找中显示的值和文本。 使用带有列名称的后缀
name来设置文本的值。请勿使用任何其他参数。
对于客户和所有者查询,请设定值和名称,就像为简单查询设定值一样。 此外,使用后缀
type指定表的类型。 可接受的值为account、contact、systemuser和team。您无法设置参与者列表或“关于”查找的值。
示例:设置查找列的值
若要设置查阅列的值,请使用数据值和名称值,并且仅在客户或所有者查找的情况下,指定相应列的类型值。 以下示例将所有者列设置为名为“Mark Folkerts”的用户。
参数的未编码值为 extraqs “ownerid={B8C6E040-656E-DF11-B414-00155DB1891A}&owneridname=Mark Folkerts&owneridtype=systemuser”。
/main.aspx?etn=lead&pagetype=entityrecord&extraqs=ownerid%3D%7bB8C6E040-656E-DF11-B414-00155DB1891A%7d%26owneridname%3DMark%20Folkerts%26owneridtype%3Dsystemuser
以下示例将主要联系人列设置为名为“Yvonne McKay(sample)”的用户。参数的未编码值为 extraqs “primarycontactid={43b58571-eefa-e311-80c1-00155d2a68c4}&primarycontactidname=Yvonne McKay (sample)”。
/main.aspx?etn=account&pagetype=entityrecord&extraqs=primarycontactid%3D%7B43b58571-eefa-e311-80c1-00155d2a68c4%7D%26primarycontactidname%3DYvonne%20McKay%20(sample)
注释
对于如下所示的简单查找,无需设置类型值。
示例:设置日期列的值
以下示例将新机会的 Est.Close Date 列设置为 2011 年 1 月 31 日。 参数的未编码值为 extraqsestimatedclosedate=01/31/11.
/main.aspx?etn=opportunity&extraqs=estimatedclosedate%3D01%2F31%2F11&pagetype=entityrecord
示例:设置选择列的值
若要设置 Choice 列的值,请设置选项的整数值。 以下示例将 “角色 ”列值设置为新联系人记录中的“决策人”。
参数的未编码值为 extraqsaccountrolecode=1.
/main.aspx?etn=contact&extraqs=accountrolecode%3D1&pagetype=entityrecord
示例:设置选项列的值
若要设置 “选项” 列的值,请为用于打开窗体的 URL 中的选项指定整数值。 例如,若要设置“霍比斯”列的选项,参数extraqshobbies=[1,3,4]的未编码值为 。
/main.aspx?etn=contact&extraqs=hobbies%3D%5B1%2C3%2C4%5D&pagetype=entityrecord
示例:使用 Xrm.Navigation.openForm 打开新窗口
以下 openNewContactExample 函数设置多个不同列的默认值,并演示如何使用该 Xrm.Navigation.openForm 函数。 它等效于使用 window.open 该方法的上一个示例。
/**
* Opens a new contact form with pre-populated default values
* @returns {Promise<void>}
*/
async function openNewContactExample() {
// Define form parameters with default values
const formParameters = {
parentcustomerid: "2878282E-94D6-E111-9B1D-00155D9D700B",
parentcustomeridname: "Contoso",
parentcustomeridtype: "account",
address1_addresstypecode: "3", // Primary address type
description: "Default values for this record were set programmatically.",
donotemail: "1" // Do not allow emails
};
// Configure form options
const entityFormOptions = {
entityName: "contact"
};
try {
const result = await Xrm.Navigation.openForm(entityFormOptions, formParameters);
console.log("Form opened successfully:", result);
} catch (error) {
console.error("Failed to open contact form:", error);
throw error; // Re-throw to allow caller to handle if needed
}
}
示例:使用 window.open 打开新窗口
以下openNewContactExample函数设置多个不同列的默认值,并演示如何使用 extraqs 对参数的值进行编码。 如果使用 window.open 方法,则可以控制打开的窗口的功能。
/**
* Opens a new contact form in a new window with pre-populated default values
* @returns {Window|null} Reference to the opened window or null if blocked
*/
function openNewContactExample() {
// Define form parameters with default values
const formParameters = {
parentcustomerid: "{F01F3F6D-896E-DF11-B414-00155DB1891A}",
parentcustomeridname: "Contoso",
parentcustomeridtype: "account",
address1_addresstypecode: "3", // Primary address type
description: "Default values for this record were set programmatically.",
donotemail: "1" // Do not allow emails
};
// Build query string using URLSearchParams for better handling
const params = new URLSearchParams(formParameters);
const extraqs = params.toString();
// Configure window features
const windowFeatures = "location=no,menubar=no,status=no,toolbar=no";
// Build the URL
const baseUrl = "/main.aspx";
const url = `${baseUrl}?etn=contact&pagetype=entityrecord&extraqs=${encodeURIComponent(extraqs)}`;
try {
// Open the window
const newWindow = window.open(url, "_blank", windowFeatures, false);
if (!newWindow) {
console.warn("Window opening was blocked by the browser");
return null;
}
return newWindow;
} catch (error) {
console.error("Failed to open contact form window:", error);
throw error;
}
}