[英]How to search posts of specific custom post type by a custom field value and then “filter” the results by a custom field checkbox value?
[英]Trying to Set a Custom Field Value on a Customer, from a Custom Record Field
我正在為一個應該執行以下操作的腳本而苦苦掙扎:
但它在第二次搜索中退出,說由於無效的搜索條件而返回“未定義”。 我假設我從第一次搜索中獲得的字段在第二次搜索的標准中不起作用?
我的代碼在下面 - 它是一個明顯的代碼(像往常一樣),還是 go 的錯誤方式?
/**
*@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
};
});
我發現處理多級搜索的最佳方法是將每次搜索的結果收集到一個數組中。
一旦我處理完一次搜索的結果,然后一個一個地處理數組的每個元素,無論是搜索、查詢、編輯等。
這樣做的好處包括:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.