Thursday, September 20, 2018

Get the value of the custom fields in .NET application


This sample code in C# gets the custom fields value of a record. Here is a sample code and commented explanation on each lines

Sample code snippet in .NET (C#):

NetSuiteService _service = new NetSuiteService();
Master master = new Master();
master.Login(true, _service);

RecordRef customer = new RecordRef();
customer.type = RecordType.customer;
customer.typeSpecified = true;
customer.internalId = "10";

ReadResponse readResponse = _service.get(customer);
//a sample on how to get the value of a field record
Customer cust = (Customer)readResponse.record;
Response.Write("Customer: " + cust.companyName);

//assign the customfield value to a CustomFieldRef array
CustomFieldRef[] cfr = cust.customFieldList;

//loop to all customfields
for (int i = 0; i < cfr.Length; i++)
{
   //on each iteration, test the custom fields record type
   if (cfr[i].GetType() == typeof(BooleanCustomFieldRef))
   {
      //cast the type to the value and get access to internal id and value property
      BooleanCustomFieldRef myval = (BooleanCustomFieldRef)cfr[i];
      Response.Write(myval.value);
   }
   else if (cfr[i].GetType() == typeof(StringCustomFieldRef))
   {
      StringCustomFieldRef myString = (StringCustomFieldRef)cfr[i];
      Response.Write(myString.value);
   }
//continue with other data type.....
}

 

 

1 comment:

  1. Great example, helped me finish my method:

    public static NetSuiteStatus GetSalesStatus(string netSuiteID)
    {
    // ReadyForPayment = custbody_readyforpymnt

    // PaymentApproved = custbody_payment_approved

    // InProduction = custbody_production_began

    // Has Mailed = custbodymaildate

    // SO QTY = custbody_sales_order_quantity

    var custFields = new NetSuiteStatus
    {
    Status = "",
    ReadyForPayment = false,
    PaymentApproved = false,
    InProduction = false,
    SatoriQty = false,
    HasMailed = false,
    Quantity = 0,
    ActualCost = 0,
    TaxTotal = 0
    };


    var netSuiteStatus = MerrillActions.GetSalesOrder(netSuiteID);

    custFields.ActualCost = (long) netSuiteStatus.total;

    custFields.TaxTotal = (long)netSuiteStatus.taxTotal;

    // assign the customFieldList to a CustomFieldRef array
    CustomFieldRef[] cfr = netSuiteStatus.customFieldList;

    // loop to all custom fields
    for (int i = 0; i < cfr.Length; i++)
    {
    switch (cfr[i].scriptId)
    {
    case "custbody_readyforpymnt":
    //cast the type to the field type and get access to internal id and value property
    BooleanCustomFieldRef readyForPayment = (BooleanCustomFieldRef)cfr[i];
    custFields.ReadyForPayment = readyForPayment.value;
    break;

    case "custbody_payment_approved":
    //cast the type to the value and get access to internal id and value property
    BooleanCustomFieldRef paymentApproved = (BooleanCustomFieldRef)cfr[i];
    custFields.PaymentApproved = paymentApproved.value;
    break;

    case "custbody_satori_qty_update":
    //cast the type to the value and get access to internal id and value property
    BooleanCustomFieldRef satoriQty = (BooleanCustomFieldRef)cfr[i];
    custFields.SatoriQty = satoriQty.value;
    break;

    case "custbody_production_began":
    //cast the type to the value and get access to internal id and value property
    BooleanCustomFieldRef inProduction = (BooleanCustomFieldRef)cfr[i];
    custFields.InProduction = inProduction.value;
    break;

    case "custbody_sales_order_quantity":
    //cast the type to the value and get access to internal id and value property
    LongCustomFieldRef quantity = (LongCustomFieldRef)cfr[i];
    custFields.Quantity = quantity.value;
    break;
    }
    }
    return custFields;
    }

    ReplyDelete