尝试从自定义记录字段为客户设置自定义字段值
Trying to Set a Custom Field Value on a Customer, from a Custom Record Field
我正在努力处理一个脚本,该脚本应该:
- 搜索客户列表
- 得到几个Field Values,其中一个用于下一次搜索,另一个是ID
- 搜索自定义记录列表,条件是我刚刚获取的字段之一
- 获取字段值
- 并使用之前获取的客户 ID 将自定义记录字段值分配给客户的自定义字段。
但它在第二次搜索时退出,说由于搜索条件无效,它返回“未定义”。我假设我从第一次搜索中获得的字段在第二次搜索的条件中不起作用?
我的代码在下面 - 它是一个显而易见的代码(像往常一样),还是实际上是错误的方法?
/**
*@NApiVersion 2.x
*@NScriptType ScheduledScript
*/
define(['N/search'],
function getShipSuburbId(search) {
function execute() {
var customerSearchObj = search.create({
type: "customer",
filters:
[
["custentity_store_shipping_suburb","isnotempty",""]
],
columns:
[
search.createColumn({
name: "entityid"
}),
search.createColumn({name: "custentity_store_shipping_suburb"})
]
});
var custSearchResult = customerSearchObj.runPaged({pageSize: 1000});
log.debug({title: "customerSearchObj result count", details: custSearchResult.count});
var custNumPages = custSearchResult.pageRanges.length;
var custAllResults = [];
var i = 0;
while (i < custNumPages) {
custAllResults = custAllResults.concat(custSearchResult.fetch(i).data);
i++;
}
return custAllResults;
for (var j = 0; j < custAllResults.length; j++) {
var currentRecord = custAllResults[j].getValue({
name: "entityid"
});
var shipSub = custAllResults[j].getValue({
name: "custentity_store_shipping_suburb"
});
};
var shipSubIdSearch = search.create({
type: "customrecord_suburb",
filters:
[
["name","is",shipSub]
],
columns:
[
search.createColumn({
name: "internalid",
summary: "MAX",
label: "Internal ID"
})
]
});
var allSubIdResults = shipSubIdSearch.runPaged({pageSize: 1});
log.debug({title: "shipSubIdSearch result count", details: allSubIdResults.count});
var subNumPages = custSearchResult.pageRanges.length;
var subAllResults = [];
var m = 0;
while (m < subNumPages) {
subAllResults = subAllResults.concat(allSubIdResults.fetch(m).data);
m++;
}
return subAllResults;
for (var k = 0; k < subAllResults.length; k++) {
var shipSubId = subAllResults[k].getValue({
name: "internalid"
});
};
var setSuburbId = currentRecord.setValue({
fieldId: 'custentity_shipping_suburb_id',
value: shipSubId
});
return setSuburbId;
}
return {
execute : execute
};
});
下面的新代码
/**
*@NApiVersion 2.x
*@NScriptType ScheduledScript
*/
define(['N/search', 'N/record'],
function getShipSuburbId(search, record) {
function execute() {
var customerSearchObj = search.create({
type: "customer",
filters:
[
["custentity_store_shipping_suburb", "isnotempty", ""]
],
columns:
[
search.createColumn({
name: "entityid"
}),
search.createColumn({name: "custentity_store_shipping_suburb"})
]
}); // The first search, which draws a list of Customers
var custSearchResult = customerSearchObj.runPaged({pageSize: 1000}); // Run paged
log.debug({title: "customerSearchObj result count", details: custSearchResult.count});
var custNumPages = custSearchResult.pageRanges.length;
var custAllResults = [];
var i = 0;
while (i < custNumPages) {
custAllResults = custAllResults.concat(custSearchResult.fetch(i).data);
i++;
}
for (var j = 0; j < custAllResults.length; j++) {
var currentRecord = custAllResults[j].getValue({
name: "entityid"
});
var shipSub = custAllResults[j].getValue({
name: "custentity_store_shipping_suburb"
});
log.debug({title: "currentRecord", details: currentRecord});
log.debug({title: "shipSub", details: shipSub});
// I've left this "for" operation open for the next search - possible issue?
var shipSubIdSearch = search.create({
type: "customrecord_suburb",
filters:
[
["name", "is", shipSub]
],
columns:
[
search.createColumn({
name: "internalid",
summary: "MAX",
label: "Internal ID"
})
]
}); // Second search. This should only return one result each time it is run
var subIdRun = shipSubIdSearch.run();
log.debug({title: "subIdRun result count", details: subIdRun.count});
var shipSubId = subIdRun.each(
function (result) {
log.debug({
title: "Fetch ID",
details: result.getValue({name: "internalid"})
})
return true;
});
log.debug({title: "shipSubId result", details: shipSubId});
var myRecord = record.load({
type: 'customer',
id: currentRecord
}); // Load the Customer record, based on the id fetched in the first search
log.debug({title: "myRecord", details: myRecord});
myRecord.setValue({
fieldId: 'custentity_shipping_suburb_id',
value: shipSubId
}); // And set the value of the Custom field, based on value from second search
}
}
return {
execute : execute
};
});
以及新脚本执行日志的截图:
我试图编辑您的一些代码以使您更接近答案。我留下了解释您需要采取的步骤的评论。
试试这个,告诉我进展如何!
/**
*@NApiVersion 2.1
*@NScriptType ScheduledScript
*/
define(['N/search', "N/record"], function (search, record) {
function execute() {
let customers = [];
let storeShippingSuburbIds = [];
let storeShippingSuburbId;
let searchCustomers = search.create({
type: "customer",
filters:
[
["custentity_store_shipping_suburb", "isnotempty", ""]
],
columns:
[
search.createColumn({name: "entityid"}),
search.createColumn({name: "custentity_store_shipping_suburb"})
]
});
var pagedData = searchCustomers.runPaged({pageSize: 1000});
pagedData.pageRanges.forEach(function (pageRange) {
let page = pagedData.fetch({index: pageRange.index});
page.data.forEach(function (result) {
customers.push([
result.getValue({name: "entityid"}),
result.getValue({name: "custentity_store_shipping_suburb"})
])
storeShippingSuburbIds.push(result.getValue({name: "custentity_store_shipping_suburb"}));
return true;
});
});
/*
* I think you want the search operator of anyof here.
*/
search.create({
type: "customrecord_suburb",
filters:
[
["name", "anyof", storeShippingSuburbIds]
],
columns:
[
search.createColumn({
name: "internalid",
summary: "MAX",
label: "Internal ID"
})
]
}).run().each(function (result) {
storeShippingSuburbId = result.getValue(result.columns[0]);
});
/*
* You'll need to use record.load() here or record.submitFields(), depending on a few things.
* But, it won't work as it is because it doesn't know what the "current record" is.
*/
let myRecord = record.load();
myRecord.setValue({
fieldId: 'custentity_shipping_suburb_id',
value: storeShippingSuburbId
});
myRecord.save();
}
return {
execute: execute
};
});
我发现进行多级搜索的最佳方法是将每次搜索的结果收集到一个数组中。
一旦我处理完一次搜索的结果,然后一个一个地处理数组的每个元素,无论是搜索、查询、编辑等。
这样做的好处包括:
- 如果是预定脚本,这允许我在第一次搜索后检查治理使用情况,并在必要时使用结果数组作为参数重新安排。
- 在继续其他操作之前处理其他逻辑,例如数组元素的排序或公式。
- 可能会加快处理速度,尤其是当您不需要对第一次搜索的某些结果进行任何进一步操作时。
- 这只是个人喜好,但它可以更轻松地将脚本元素拆分为单独的函数并保持可读性和逻辑顺序。
我正在努力处理一个脚本,该脚本应该:
- 搜索客户列表
- 得到几个Field Values,其中一个用于下一次搜索,另一个是ID
- 搜索自定义记录列表,条件是我刚刚获取的字段之一
- 获取字段值
- 并使用之前获取的客户 ID 将自定义记录字段值分配给客户的自定义字段。
但它在第二次搜索时退出,说由于搜索条件无效,它返回“未定义”。我假设我从第一次搜索中获得的字段在第二次搜索的条件中不起作用?
我的代码在下面 - 它是一个显而易见的代码(像往常一样),还是实际上是错误的方法?
/**
*@NApiVersion 2.x
*@NScriptType ScheduledScript
*/
define(['N/search'],
function getShipSuburbId(search) {
function execute() {
var customerSearchObj = search.create({
type: "customer",
filters:
[
["custentity_store_shipping_suburb","isnotempty",""]
],
columns:
[
search.createColumn({
name: "entityid"
}),
search.createColumn({name: "custentity_store_shipping_suburb"})
]
});
var custSearchResult = customerSearchObj.runPaged({pageSize: 1000});
log.debug({title: "customerSearchObj result count", details: custSearchResult.count});
var custNumPages = custSearchResult.pageRanges.length;
var custAllResults = [];
var i = 0;
while (i < custNumPages) {
custAllResults = custAllResults.concat(custSearchResult.fetch(i).data);
i++;
}
return custAllResults;
for (var j = 0; j < custAllResults.length; j++) {
var currentRecord = custAllResults[j].getValue({
name: "entityid"
});
var shipSub = custAllResults[j].getValue({
name: "custentity_store_shipping_suburb"
});
};
var shipSubIdSearch = search.create({
type: "customrecord_suburb",
filters:
[
["name","is",shipSub]
],
columns:
[
search.createColumn({
name: "internalid",
summary: "MAX",
label: "Internal ID"
})
]
});
var allSubIdResults = shipSubIdSearch.runPaged({pageSize: 1});
log.debug({title: "shipSubIdSearch result count", details: allSubIdResults.count});
var subNumPages = custSearchResult.pageRanges.length;
var subAllResults = [];
var m = 0;
while (m < subNumPages) {
subAllResults = subAllResults.concat(allSubIdResults.fetch(m).data);
m++;
}
return subAllResults;
for (var k = 0; k < subAllResults.length; k++) {
var shipSubId = subAllResults[k].getValue({
name: "internalid"
});
};
var setSuburbId = currentRecord.setValue({
fieldId: 'custentity_shipping_suburb_id',
value: shipSubId
});
return setSuburbId;
}
return {
execute : execute
};
});
下面的新代码
/**
*@NApiVersion 2.x
*@NScriptType ScheduledScript
*/
define(['N/search', 'N/record'],
function getShipSuburbId(search, record) {
function execute() {
var customerSearchObj = search.create({
type: "customer",
filters:
[
["custentity_store_shipping_suburb", "isnotempty", ""]
],
columns:
[
search.createColumn({
name: "entityid"
}),
search.createColumn({name: "custentity_store_shipping_suburb"})
]
}); // The first search, which draws a list of Customers
var custSearchResult = customerSearchObj.runPaged({pageSize: 1000}); // Run paged
log.debug({title: "customerSearchObj result count", details: custSearchResult.count});
var custNumPages = custSearchResult.pageRanges.length;
var custAllResults = [];
var i = 0;
while (i < custNumPages) {
custAllResults = custAllResults.concat(custSearchResult.fetch(i).data);
i++;
}
for (var j = 0; j < custAllResults.length; j++) {
var currentRecord = custAllResults[j].getValue({
name: "entityid"
});
var shipSub = custAllResults[j].getValue({
name: "custentity_store_shipping_suburb"
});
log.debug({title: "currentRecord", details: currentRecord});
log.debug({title: "shipSub", details: shipSub});
// I've left this "for" operation open for the next search - possible issue?
var shipSubIdSearch = search.create({
type: "customrecord_suburb",
filters:
[
["name", "is", shipSub]
],
columns:
[
search.createColumn({
name: "internalid",
summary: "MAX",
label: "Internal ID"
})
]
}); // Second search. This should only return one result each time it is run
var subIdRun = shipSubIdSearch.run();
log.debug({title: "subIdRun result count", details: subIdRun.count});
var shipSubId = subIdRun.each(
function (result) {
log.debug({
title: "Fetch ID",
details: result.getValue({name: "internalid"})
})
return true;
});
log.debug({title: "shipSubId result", details: shipSubId});
var myRecord = record.load({
type: 'customer',
id: currentRecord
}); // Load the Customer record, based on the id fetched in the first search
log.debug({title: "myRecord", details: myRecord});
myRecord.setValue({
fieldId: 'custentity_shipping_suburb_id',
value: shipSubId
}); // And set the value of the Custom field, based on value from second search
}
}
return {
execute : execute
};
});
以及新脚本执行日志的截图:
我试图编辑您的一些代码以使您更接近答案。我留下了解释您需要采取的步骤的评论。
试试这个,告诉我进展如何!
/**
*@NApiVersion 2.1
*@NScriptType ScheduledScript
*/
define(['N/search', "N/record"], function (search, record) {
function execute() {
let customers = [];
let storeShippingSuburbIds = [];
let storeShippingSuburbId;
let searchCustomers = search.create({
type: "customer",
filters:
[
["custentity_store_shipping_suburb", "isnotempty", ""]
],
columns:
[
search.createColumn({name: "entityid"}),
search.createColumn({name: "custentity_store_shipping_suburb"})
]
});
var pagedData = searchCustomers.runPaged({pageSize: 1000});
pagedData.pageRanges.forEach(function (pageRange) {
let page = pagedData.fetch({index: pageRange.index});
page.data.forEach(function (result) {
customers.push([
result.getValue({name: "entityid"}),
result.getValue({name: "custentity_store_shipping_suburb"})
])
storeShippingSuburbIds.push(result.getValue({name: "custentity_store_shipping_suburb"}));
return true;
});
});
/*
* I think you want the search operator of anyof here.
*/
search.create({
type: "customrecord_suburb",
filters:
[
["name", "anyof", storeShippingSuburbIds]
],
columns:
[
search.createColumn({
name: "internalid",
summary: "MAX",
label: "Internal ID"
})
]
}).run().each(function (result) {
storeShippingSuburbId = result.getValue(result.columns[0]);
});
/*
* You'll need to use record.load() here or record.submitFields(), depending on a few things.
* But, it won't work as it is because it doesn't know what the "current record" is.
*/
let myRecord = record.load();
myRecord.setValue({
fieldId: 'custentity_shipping_suburb_id',
value: storeShippingSuburbId
});
myRecord.save();
}
return {
execute: execute
};
});
我发现进行多级搜索的最佳方法是将每次搜索的结果收集到一个数组中。
一旦我处理完一次搜索的结果,然后一个一个地处理数组的每个元素,无论是搜索、查询、编辑等。
这样做的好处包括:
- 如果是预定脚本,这允许我在第一次搜索后检查治理使用情况,并在必要时使用结果数组作为参数重新安排。
- 在继续其他操作之前处理其他逻辑,例如数组元素的排序或公式。
- 可能会加快处理速度,尤其是当您不需要对第一次搜索的某些结果进行任何进一步操作时。
- 这只是个人喜好,但它可以更轻松地将脚本元素拆分为单独的函数并保持可读性和逻辑顺序。