Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NetSuite Selecting a value in a Custom List for a SelectCustomFieldRef in C# using web-services

Struggling for hours on this. How do you reference a SelectCustomFieldRef and select a value using the string from the drop down box?

We have a Custom Field that is a Single Select. The Custom Field implements a custom List.

The only information we have at run time is the following.

  • What type of custom field we are working with: Single Select
  • What the name of the Custom Field is.
  • What the name of the selected value is.

What we do not know that we need is.

  • ID of the Custom List
  • Intenral ID of theCustom List
  • ID of the Selected Value

What we know we need for a SelectCustomFieldRef object (scfr).

  • scfr.internalId = [Custom Field Name. ex: custbody_myfield_singleselect]
  • scfr.value.typeId = [Internal ID of the Custom List. ex: 303]
  • scfr.value.internalId = [ID of the Desired Selected Value]

Since we have the internalId of the Custom Field what we need to learn is a way to determine what list is associated with that field, what its ID is in NetSuite, and what the ID is of the desired selected field. This needs to be determined pragmatically so that it can be dynamically used for other users.

like image 561
cepatt Avatar asked Nov 02 '22 14:11

cepatt


1 Answers

After much trial and tribulation we have come up with a solution that works. A lot of the methods have be extracted into other functions, but I will try and provide the base framework for it here for you.

The code focused on trying to make as few calls out to NetSuite as possible. This assumed that you would be re-using much of this data in multiple transactions.

    // 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);
    }

I hope that this code snippet has helped you out and wish the best of luck.

~EDIT~

Additionally when going back the other way (extracting a value) here is how to turn the ID back into the selected value text. The customField object is a CustomFieldRef obtained from the "options" field of the transaction (which is an array of 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;
    }
like image 65
cepatt Avatar answered Nov 08 '22 02:11

cepatt