c# - NetSuite 使用 Web 服务在 C# 中为 SelectCustomFieldRef 选择自定义列表中的值

标签 c# web-services api netsuite

为此苦苦挣扎了几个小时。如何引用 SelectCustomFieldRef 并使用下拉框中的字符串选择值?

我们有一个单选的自定义字段。 自定义字段实现自定义列表。

我们在运行时拥有的唯一信息如下。

  • 我们使用的自定义字段类型:单选
  • 自定义字段的名称是什么。
  • 所选值的名称是什么。

我们不知道我们需要的是。

  • 自定义列表ID
  • 自定义列表的内部 ID
  • 所选值的 ID

我们知道我们需要 SelectCustomFieldRef 对象 (scfr)。

  • scfr.internalId = [自定义字段名称。例如:custbody_myfield_singleselect]
  • scfr.value.typeId = [自定义列表的内部 ID。例如:303]
  • scfr.value.internalId = [所需选定值的 ID]

由于我们拥有自定义字段的 internalId,因此我们需要了解一种方法来确定与该字段相关联的列表、它在 NetSuite 中的 ID 以及所需所选字段的 ID。这需要务实地确定,以便它可以动态地供其他用户使用。

最佳答案

经过多次试验和磨难,我们想出了一个可行的解决方案。很多方法已经被提取到其他功能中,但我会尝试在这里为您提供它的基本框架。

代码着重于尝试尽可能少地调用 NetSuite。这假设您将在多个事务中重复使用其中的大部分数据。

    // NetSuite Session
    // _service is constructed elsewhere in code.

    // Global Values
    Dictionary<string, CustomizationRef> _Dictionary_CustomListRef = new Dictionary<string, CustomizationRef>();
    Dictionary<string, CustomList> _Dictionary_CustomList = new Dictionary<string, CustomList>();
    Dictionary<string, TransactionBodyCustomField> _Dictionary_TransactionBodyCustomField = new Dictionary<string, TransactionBodyCustomField>();

    private CustomFieldRef NetSuite_CreateSelectCustomFieldRef(string sName, string sValue)
    {
        //List or Record Type reference
        SelectCustomFieldRef custbody_field = new SelectCustomFieldRef();
        custbody_field.internalId = sName;
        custbody_field.value = new ListOrRecordRef();

        // Get the Targeted List that we point to in NetSuite
        custbody_field.value.typeId = NetSuite_getTransactionBodyCustomFieldListInternalId(sName);

        // Get ID of List Value from our targeted list in NetSuite
        custbody_field.value.internalId = NetSuite_getCustomListValueID(custbody_field.value.typeId, sValue);

        return custbody_field;
    }

    private string NetSuite_getTransactionBodyCustomFieldListInternalId(string sID)
    {
        string sReturnValue = string.Empty;

        TransactionBodyCustomField tbCustomField = NetSuite_getTransactionBodyCustomField(sID);

        if (tbCustomField != null)
        {
            sReturnValue = tbCustomField.selectRecordType.internalId;
        }

        return sReturnValue;
    }

    private TransactionBodyCustomField NetSuite_getTransactionBodyCustomField(string sID)
    {
        TransactionBodyCustomField tbCustomField = null;

        if (!_Dictionary_TransactionBodyCustomField.TryGetValue(sID, out tbCustomField))
        {
            // Gets a specific custom body object
            CustomizationRef cref = new CustomizationRef();
            cref.internalId = sID;
            cref.scriptId = sID;
            cref.type = RecordType.transactionBodyCustomField;
            cref.typeSpecified = true;
            ReadResponse res = _service.get(cref);

            if (res.status.isSuccess)
                tbCustomField = res.record as TransactionBodyCustomField;

            _Dictionary_TransactionBodyCustomField.Add(sID, tbCustomField);
        }

        return tbCustomField;
    }

    private bool NetSuite_TryGetCustomList(string sCustomListInternalID, out CustomList custList)
    {
        custList = null;
        bool bSuccess = false;

        if (!_Dictionary_CustomList.TryGetValue(sCustomListInternalID, out custList))
        {
            if (_Dictionary_CustomListRef.Count == 0)
                initializeCustomListDictionary();

            CustomizationRef crCustomList = null;

            if (_Dictionary_CustomListRef.TryGetValue(sCustomListInternalID, out crCustomList))
            {
                RecordRef recRef = new RecordRef();
                recRef.internalId = crCustomList.internalId;
                recRef.type = RecordType.customList;
                recRef.typeSpecified = true;
                ReadResponse readResp = _service.get(recRef);

                if (readResp.status.isSuccess)
                    custList = readResp.record as CustomList;

                _Dictionary_CustomList.Add(sCustomListInternalID, custList);
            }
        }

        if (custList == null)
            bSuccess = false;
        else
            bSuccess = true;

        return bSuccess;
    }

    private string NetSuite_getCustomListScriptID(string sCustomListInternalID)
    {
        string sReturnValue = string.Empty;
        CustomList custList;

        if (NetSuite_TryGetCustomList(sCustomListInternalID, out custList))
            sReturnValue = custList.scriptId;

        return sReturnValue;
    }

    private string NetSuite_getCustomListValueID(string sCustomListInternalID, string sValue)
    {
        string sReturnValue = string.Empty;
        CustomList custList;

        if (NetSuite_TryGetCustomList(sCustomListInternalID, out custList))
        {
            CustomListCustomValue[] clValueList = custList.customValueList.customValue;

            foreach(CustomListCustomValue clValue in custList.customValueList.customValue)
            {
                if (clValue.value.ToUpper() == sValue.ToUpper())
                {
                    sReturnValue = clValue.valueId.ToString();
                    break;
                }
            }
        }

        return sReturnValue;
    }

    private void initializeCustomListDictionary()
    {
        _Dictionary_CustomListRef.Clear();

        // Gets all of the custom lists
        CustomizationType ct = new CustomizationType();
        ct.getCustomizationType = GetCustomizationType.customList;
        ct.getCustomizationTypeSpecified = true;

        GetCustomizationIdResult res = _service.getCustomizationId(ct, true);

        foreach (CustomizationRef cref in res.customizationRefList)
            _Dictionary_CustomListRef.Add(cref.internalId, cref);
    }

希望这段代码对您有所帮助,并祝您好运。

~编辑~

此外,当以另一种方式返回(提取值)时,这里是如何将 ID 变回选定的值文本。 customField对象是从交易的“options”字段中获取的CustomFieldRef(这是一个CustomFieldRefs的数组)。

    SelectCustomFieldRef selectCustFieldRef = customField as SelectCustomFieldRef;
    sCustFieldRefId = selectCustFieldRef.internalId;
    sCustFieldRefValue = NetSuite_getCustomListValueFromID(selectCustFieldRef.value.typeId, long.Parse(selectCustFieldRef.value.internalId));

    private string NetSuite_getCustomListValueFromID(string sCustomListInternalID, long lValue)
    {
        string sReturnValue = string.Empty;
        CustomList custList;

        if (NetSuite_TryGetCustomList(sCustomListInternalID, out custList))
        {
            CustomListCustomValue[] clValueList = custList.customValueList.customValue;

            foreach (CustomListCustomValue clValue in custList.customValueList.customValue)
            {
                if (clValue.valueId == lValue)
                {
                    sReturnValue = clValue.value;
                    break;
                }
            }
        }

        return sReturnValue;
    }

关于c# - NetSuite 使用 Web 服务在 C# 中为 SelectCustomFieldRef 选择自定义列表中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18899488/

相关文章:

java - jmeter中soap web服务的xpath断言

javascript - Json 到 csv 转换器(IE 8+)

ruby - 未初始化的常量 Net::HTTPS (NameError)

c# - 在 C# 中操作关键流缓冲区的最有效和可靠的方法是什么?

c# - 模拟 Windows 的浏览器流量

c# - ASP.NET 404 http模块

php - 使用 PayPal API 获取所有交易

c# - 调用 httpClient.PostAsync 返回 null

c# - 使用 NetComm dll 发送屏幕截图到主机

c# - 将 Node JS 转换为 C# 以加载 WAV 格式的音频并删除一些不必要的 header