Wednesday, January 23, 2019

Add Estimate via Web Services (C#)

Below is a sample code to add stand alone Estimate via web services:

       public virtual void addEstimate()
        {
            Estimate estimate = new Estimate();
            // Set customer entity

            _out.writeLn("\nPlease enter the following customer information. " + "Note that some fields have already been populated. ");
            _out.write("Customer entity name: ");

            CustomerSearch custSearch = new CustomerSearch();
            SearchStringField customerEntityID = new SearchStringField();
            customerEntityID.@operator = SearchStringFieldOperator.@is;
            customerEntityID.operatorSpecified = true;
            customerEntityID.searchValue = _out.readLn();

            CustomerSearchBasic custBasic = new CustomerSearchBasic();
            custBasic.entityId = customerEntityID;

            custSearch.basic = custBasic;

            // Search for the customer entity
            SearchResult res = _service.search(custSearch);

            if (res.status.isSuccess)
            {
                if (res.recordList != null && res.recordList.Length == 1)
                {
                    RecordRef customer = new RecordRef();
                    customer.type = RecordType.customer;
                    customer.typeSpecified = true;
                    System.String entID = ((Customer)(res.recordList[0])).entityId;
                    customer.name = entID;
                    customer.internalId = ((Customer)(res.recordList[0])).internalId;
                    estimate.entity = customer;

                    // set the transaction date and status
                    estimate.tranDate = new System.DateTime();
                   

                    // Enter the internal ID for inventory items to be added to the SO
                    _out.writeLn("\nPlease enter the internal ID values for INVENTORY ITEMS seperated by commas (do not enter discount or subtotal items).");
                    _out.write("Item(s): ");
                    System.String itemKeys = _out.readLn();
                    System.String[] itemKeysArray = itemKeys.Split(new Char[] { ',' });

                    EstimateItem[] estimateItemArray = new EstimateItem[itemKeysArray.Length];

                    // Create the correct sales order items and populate the
                    // quantity
                    for (int i = 0; i < itemKeysArray.Length; i++)
                    {
                        RecordRef item = new RecordRef();
                        item.type = RecordType.inventoryItem;
                        item.typeSpecified = true;
                        item.internalId = itemKeysArray[i];
                        estimateItemArray[i] = new EstimateItem();
                        estimateItemArray[i].item = item;

                        _out.writeLn("\nPlease enter quantity for " + itemKeysArray[i]);
                        _out.write("Quantity: ");
                        System.Double quantity = System.Double.Parse(_out.readLn());
                        estimateItemArray[i].quantity = quantity;
                        estimateItemArray[i].quantitySpecified = true;
                    }

                    EstimateItemList estimateItemList = new EstimateItemList();
                    estimateItemList.item = estimateItemArray;

                    estimate.itemList = estimateItemList;

                    WriteResponse writeRes = _service.add(estimate);
                    if (writeRes.status.isSuccess)
                    {
                        _out.writeLn("\nEstimate created successfully\nEstimate Internal ID=" + ((RecordRef)writeRes.baseRef).internalId);
                    }
                    else
                    {
                        _out.error(getStatusDetails(writeRes.status));
                    }
                }
                else
                {
                    _out.writeLn("\nSales order is not created because 0 or more than 1 customer records found for the entityID given");
                }
            }
            else
            {
                _out.error(getStatusDetails(res.status));
            }
        }

No comments:

Post a Comment