Suitescript – 在脚本中使用动态创建的 “fieldId”

huangapple go评论62阅读模式
英文:

Suitescript - Using a dynamically created "fieldId" in the script

问题

我想根据结果动态创建一个基于字段ID的字段。我有一个非常基本的脚本,展示了我试图做的事情。它继续不检索信息。我尝试使用 + 而不是 concat ('custitem' + testtext + '')。当我记录字段名时,它正确显示为 'custitemdoe_jane'。

您可以看到脚本以两种不同的方式使用公式。这可行吗?

英文:

I want to dynamically create a field id based upon results. I have a very basic script showing what I am trying to do. It continues to not retrieve information. I have tried using + instead of concat (''custitem' + testtext + '''). When I log the field name - it shows correctly 'custitemdoe_jane'.

You can see the script shows using the formula in two different ways. Can this be done??

 /**
 * This script looks for RTAs - updates the purchase Order and the CT record
 * 
 * @NApiVersion 2.x
 * @NScriptType ScheduledScript
 * @NModuleScope SameAccount
 */
 
define(['N/file', 'N/search', 'N/record', 'N/format', 'N/email'],
    	
function(file, search, record, format, email) {
	
	function execute(scriptContext) {
    var texty = '\'custitem';
	var testtext = 'doe_jane';
    var texty3 = '\'';
	var fieldname = texty.concat(testtext);
    var fieldname = fieldname.concat(texty3);
	
    log.debug('fieldname', fieldname);

// end result here is 'custitemdoe_jane' with the quotes

	var itemfix = record.load({
		type: record.Type.INVENTORY_ITEM, 
		id: 488, 
        isDynamic: false
		});
		   	
        var values = itemfix.getText({fieldId: fieldname});   
        var values2 = itemfix.getValue({fieldId: fieldname});    
        log.debug('values', values);
        log.debug('values', values2);

       var values3 = itemfix.getText({fieldId: fieldname.concat(texty3)});   
        var values4 = itemfix.getValue({fieldId: fieldname.concat(texty3)});    
        log.debug('values', values3);
        log.debug('values', values4);

// all of these above are blank

		itemfix.save({
			enableSourcing: true
		});    			
	   	}
		
return {execute: execute};
	
});

答案1

得分: 1

你的主要问题是字段名称中可能不包含引号。因此,你需要使用custitemdoe_jane而不是'custitemdoe_jane'

只要你知道自己在做什么(我未能理解你的代码目的),要根据var testtext中的更改生成fieldname,至少需要以下代码:

而不是:

var texty = ''custitem'';
var testtext = ''doe_jane'';
var texty3 = '''';
var fieldname = texty.concat(testtext);
var fieldname = fieldname.concat(texty3);

可以使用以下代码(这些是为了更清晰而进行的最小修改):

var texty = 'custitem';
var testtext = 'doe_jane';
var fieldname = texty + testtext;

这样,你将获得一个至少是有效名称的字段名称,即custitemdoe_jane

英文:

Your main problem, is that a field may not have quotes in its name. So you need to have custitemdoe_jane instead of 'custitemdoe_jane'.

Provided you know, what you are doing (I have failed to get the purpose of your code), to generate fieldname based on a value changed in the var testtext you need at least:

instead of:

var texty = '\'custitem';
var testtext = 'doe_jane';
var texty3 = '\'';
var fieldname = texty.concat(testtext);
var fieldname = fieldname.concat(texty3);

have this (these are minimal modifications, for clarity):

var texty = 'custitem';
var testtext = 'doe_jane';
var fieldname = texty + testtext;

this way you'll have 'custitemdoe_jane' as a field name, which is at least a valid name.

答案2

得分: 1

或者您可以使用模板字面量,这是一种现代方法,通过将Apiversion切换为2.1并像这样操作。

const prefix = 'custitem_';

let fieldId = `${prefix}${itemfix.getText({fieldId: fieldname})}`;

无论如何我会在创建基于它的fieldId之前检查这个示例中的*itemfix.getText({fieldId: fieldname})*是否返回一个值
英文:

or you can use template Literals which is a modern approach by switching Apiversion to 2.1 and do it like this.

const prefix = 'custitem_'

let fieldId = `${prefix}${itemfix.getText({fieldId: fieldname});}`

In any case i would check if this for instance itemfix.getText({fieldId: fieldname}) returns a value before i create a fieldId based on it.

huangapple
  • 本文由 发表于 2023年5月25日 05:59:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定