SAP B1 SDK DI API cheet sheet

 

  1. Post AP Service Invoice


 public bool PostAPServiceInvoice(DataRow dr)

        {

            string NumatCard = string.Empty;

            try

            {


                //,DataRow drH,DataRow drR   

                string oErrorMsg = String.Empty;

                int oErrorCode = 0;


                string Invseries = string.Empty;

                string BranchName = string.Empty;

                string value = string.Empty;

                int rowNo = 0;


                string basetype = string.Empty;

                string baseRef = string.Empty;

                string baseline = string.Empty;



                DataRow drPostData = dr;



                if (!Utilities.Application.Company.InTransaction)

                    Utilities.Application.Company.StartTransaction();

                SAPbobsCOM.Documents oDocAPInvoiceService = (SAPbobsCOM.Documents)Utilities.Application.Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oPurchaseInvoices);



                oDocAPInvoiceService.BPL_IDAssignedToInvoice = Convert.ToInt32(drPostData[0].ToString());

                oDocAPInvoiceService.CardCode = drPostData[2].ToString();//setting business partner


                //oDocAPInvoiceService.NumAtCard = drPostData[0][3].ToString();//setting customer reference Number


                //oDocAPInvoiceService.Project = drPostData[1].ToString();//setting Project code

                oDocAPInvoiceService.DocDate = Convert.ToDateTime(drPostData[8].ToString());//setting Posting date

                                                                                            // oDocAPInvoiceService.DocDueDate = Convert.ToDateTime(drPostData[0][6].ToString());//setting due date

                oDocAPInvoiceService.TaxDate = Convert.ToDateTime(drPostData[8].ToString());//setting document date

                oDocAPInvoiceService.DocType = SAPbobsCOM.BoDocumentTypes.dDocument_Service;

                // oDocAPInvoiceService.UserFields.Fields.Item("U_SVISOR").Value = drPostData[0][26].ToString().Trim();//setting supervisor details

                oDocAPInvoiceService.UserFields.Fields.Item("U_SiteType").Value = drPostData[3].ToString();//setting site Type

                oDocAPInvoiceService.UserFields.Fields.Item("U_SiteID").Value = drPostData[1].ToString();//setting Project code

                //foreach (DataRow dr1 in drPostData)

                //{

                Utilities.Application.SBO_Application.RemoveWindowsMessage(SAPbouiCOM.BoWindowsMessageType.bo_WM_TIMER, true);


                oDocAPInvoiceService.Lines.SetCurrentLine(rowNo);//setting row Line Num

                oDocAPInvoiceService.Lines.TaxCode = dr[10].ToString().Trim();//setting TaxCode

                oDocAPInvoiceService.Lines.AccountCode = dr[12].ToString().Trim();//setting TaxCode

                oDocAPInvoiceService.Lines.LineTotal = Convert.ToDouble(dr[11].ToString().Trim());//setting TaxCode

                oDocAPInvoiceService.Lines.LocationCode = Convert.ToInt32(dr[15].ToString().Trim());//setting Location

                oDocAPInvoiceService.Lines.ItemDescription = dr[5].ToString().Trim();

                oDocAPInvoiceService.Lines.ProjectCode = drPostData[1].ToString();//setting Project code

                oDocAPInvoiceService.WithholdingTaxData.WTCode = Convert.ToString(dr[16].ToString().Trim());//setting TDS

                oDocAPInvoiceService.WithholdingTaxData.Add();


                SAPbobsCOM.Recordset recordsetSAC = null;

                string sqlSAC = "Select ISNULL(b.AbsEntry,'') as SAC from OACT a inner join OSAC b on a.U_SACCODE=b.ServCode Where AcctCode='" + dr[12].ToString().Trim() + "'";

                Utilities.ExecuteSQL(ref recordsetSAC, sqlSAC);

                oDocAPInvoiceService.Lines.SACEntry = Convert.ToInt32(recordsetSAC.Fields.Item(0).Value);

                // oDocAPInvoiceService.Lines.UserFields.Fields.Item("U_IRNNO").Value = dr[15].ToString().Trim();

                //oDocAPInvoiceService.Lines.UserFields.Fields.Item("U_BTSR").Value = dr[16].ToString().Trim();

                //oDocAPInvoiceService.Lines.UserFields.Fields.Item("U_BUPC").Value = dr[17].ToString().Trim();

                //oDocAPInvoiceService.Lines.UserFields.Fields.Item("U_BMRP").Value = dr[18].ToString().Trim();

                //oDocAPInvoiceService.Lines.UserFields.Fields.Item("U_BTEXDT").Value = dr[19].ToString().Trim();

                // oDocAPInvoiceService.Lines.UserFields.Fields.Item("U_FITEM").Value = dr[21].ToString().Trim();

                // oDocAPInvoiceService.Lines.Add();

                rowNo++;

                //Utilities.ShowWarningMessage("Processing row" + rowNo);

                //}


                int returnCode = oDocAPInvoiceService.Add();

                if (returnCode != 0)

                {

                    // If process is in transaction, then the transaction is rolled back. 

                    if (Utilities.Application.Company.InTransaction)

                        Utilities.Application.Company.EndTransaction(SAPbobsCOM.BoWfTransOpt.wf_RollBack);


                    Utilities.Application.Company.GetLastError(out oErrorCode, out oErrorMsg);

                    string updateDocEntry = @"Update [@CIN_RBS1] Set U_PERROR='" + oErrorMsg + "' Where LineID='" + dr[13] + "' and DocEntry='" + dr[14] + "'";

                    SAPbobsCOM.Recordset recordsetupdate = null;

                    Utilities.ExecuteSQL(ref recordsetupdate, updateDocEntry);


                    _Form.Freeze(false);

                    return false; // Returning false 

                }

                else

                {

                    Form.Freeze(false);

                    if (Utilities.Application.Company.InTransaction)

                        Utilities.Application.Company.EndTransaction(SAPbobsCOM.BoWfTransOpt.wf_Commit);

                    string DocEntry = Utilities.Application.Company.GetNewObjectKey();

                    string updateDocEntry = @"Update [@CIN_RBS1] Set U_IDENT='" + DocEntry + "' Where LineID='" + dr[13] + "' and DocEntry='" + dr[14] + "'";

                    SAPbobsCOM.Recordset recordsetupdate = null;

                    Utilities.ExecuteSQL(ref recordsetupdate, updateDocEntry);

                    string updatePERRor = @"Update [@CIN_RBS1] Set U_PERROR='' Where LineID='" + dr[13] + "' and DocEntry='" + dr[14] + "'";

                    SAPbobsCOM.Recordset recordsetupdatePERROR = null;

                    Utilities.ExecuteSQL(ref recordsetupdatePERROR, updatePERRor);


                    return true;

                }


                //return true;

            }

            catch (Exception ex)

            {

                string updateDocEntry = @"Update [@CIN_RBS1] Set U_PERROR='" + ex.Message + "' Where LineID='" + dr[13] + "' and DocEntry='" + dr[14] + "'";

                SAPbobsCOM.Recordset recordsetupdate = null;

                Utilities.ExecuteSQL(ref recordsetupdate, updateDocEntry);

                _Form.Freeze(false);

                return false;

            }

        }


  1. Post Capitalization Document


private void CreateCP(int ctr)

        {

            try

            {

                string oErrorMsg = String.Empty;

                int oErrorCode = 0;


                _mtMatrix.GetLineData(ctr);

                Int32 ID = Convert.ToInt32(((SAPbouiCOM.EditText)_mtMatrix.Columns.Item("0").Cells.Item(ctr).Specific).Value);

                SAPbobsCOM.AssetDocument oAssetDocument;

                SAPbobsCOM.AssetDocumentService oAssetService;

                SAPbobsCOM.CompanyService oCompanyService;

                oCompanyService = Utilities.Application.Company.GetCompanyService();

                oAssetService = (SAPbobsCOM.AssetDocumentService)oCompanyService.GetBusinessService(SAPbobsCOM.ServiceTypes.AssetCapitalizationService);

                oAssetDocument = (SAPbobsCOM.AssetDocument)oAssetService.GetDataInterface(SAPbobsCOM.AssetDocumentServiceDataInterfaces.adsAssetDocument);



                SAPbobsCOM.AssetDocumentService AssetService = (SAPbobsCOM.AssetDocumentService)oCompanyService.GetBusinessService(SAPbobsCOM.ServiceTypes.AssetCapitalizationService);

                SAPbobsCOM.AssetDocument AssetDocument = AssetService.GetDataInterface(SAPbobsCOM.AssetDocumentServiceDataInterfaces.adsAssetDocument);

                SAPbobsCOM.AssetDocumentLine line = AssetDocument.AssetDocumentLineCollection.Add();

                SAPbobsCOM.AssetDocumentAreaJournal journalEn = AssetDocument.AssetDocumentAreaJournalCollection.Add();


                string date = _Form.DataSources.UserDataSources.Item("CaptDt").ValueEx;

                AssetDocument.AssetValueDate = DateTime.ParseExact(date, "yyyyMMdd", null);


                journalEn.DepreciationArea = "*";


                string bplId = Convert.ToString(dataTable.GetValue("Branch", ID - 1).ToString());

                string _sql6 = "select Series from NNM1 where ObjectCode='1470000049' and BPLId='" + bplId + "' and Locked='N'";

                Utilities.ExecuteSQL(ref _recodst111, _sql6);


                AssetDocument.Series = Convert.ToInt32(_recodst111.Fields.Item(0).Value);

                // string bplId = Convert.ToString(_Form.DataSources.UserDataSources.Item("Branch").ValueEx);

                AssetDocument.BPLID = Convert.ToInt32(bplId);

                line.AssetNumber = Convert.ToString(dataTable.GetValue("FItemCode", ID - 1).ToString());


                String capamt = _Form.DataSources.UserDataSources.Item("CaptAmt").ValueEx;

                line.TotalLC = double.Parse(_Form.DataSources.UserDataSources.Item("CaptAmt").ValueEx);

                line.Quantity = double.Parse(_Form.DataSources.UserDataSources.Item("CaptQty").ValueEx);

                string rem = dataTable.GetValue("Remarks", ID - 1).ToString();

                line.Remarks = dataTable.GetValue("Remarks", ID - 1).ToString();

                journalEn.JournalRemarks = dataTable.GetValue("Remarks", ID - 1).ToString();


                String SiteID = dataTable.GetValue("SiteID", ID - 1).ToString();

                line.Project = dataTable.GetValue("SiteID", ID - 1).ToString();

                AssetService.Add(AssetDocument);

                SAPbobsCOM.Recordset oRs2 = (SAPbobsCOM.Recordset)Utilities.Application.Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);

                oRs2.DoQuery("Select Max(DocEntry)[DocEntry] from OACQ");

                int CapDocEntry = oRs2.Fields.Item("DocEntry").Value;

                int Origin = 1470000049;

                String _sql = @"Select T0.TransId From OJDT T0 Where T0.TransType = '" + Origin + "' and T0.CreatedBy = '" + CapDocEntry + "'  ";

                Utilities.ExecuteSQL(ref _recordSet, _sql);


                String _sqlup = @"update T0 set T0.Project ='" + SiteID + "' from JDT1 T0  where T0.TransId='" + Convert.ToString(_recordSet.Fields.Item("TransId").Value) + "'";

                Utilities.ExecuteSQL(ref _recordSet, _sqlup);

                Utilities.ShowSucessMessage("Capitalization Done");

                if (Utilities.Application.Company.InTransaction == true)

                    Utilities.Application.Company.EndTransaction(SAPbobsCOM.BoWfTransOpt.wf_Commit);

            }


            catch (Exception ex)

            {

                Utilities.ShowErrorMessage(ex.Message); // Showing error message

                if (Utilities.Application.Company.InTransaction == true)

                    Utilities.Application.Company.EndTransaction(SAPbobsCOM.BoWfTransOpt.wf_RollBack);

                throw;


            }

        }


  1. Post Goods Receipt with batch/ Serial and Bin Concept

   private void GoodsReceiptPost(int ctr)

        {

            int oErrorCode;

            string oErrorMsg;

            try

            {

                _mtMatrix.GetLineData(ctr);

                Int32 ID = Convert.ToInt32(((SAPbouiCOM.EditText)_mtMatrix.Columns.Item("0").Cells.Item(ctr).Specific).Value);


                string bplId = Convert.ToString(dataTable.GetValue("Branch", ID - 1).ToString());

                String ItemCode = Convert.ToString(dataTable.GetValue("INVItemCode", ID - 1).ToString());


                String FItemCode = Convert.ToString(dataTable.GetValue("FItemCode", ID - 1).ToString());

                string ItemName = Convert.ToString(dataTable.GetValue("ItemDescription", ID - 1).ToString());

                double Quantity = Convert.ToDouble(dataTable.GetValue("Quantity", ID - 1));

                double Price = Convert.ToDouble(dataTable.GetValue("Price", ID - 1).ToString());

                double Openqty;

                double.TryParse(Form.DataSources.UserDataSources.Item("OpenQty").Value.ToString(), out Openqty);

                String DocEntry = dataTable.GetValue("DocEntry", ID - 1).ToString();

                String Batch = dataTable.GetValue("AssetBatchSerialNo", ID - 1).ToString();

                string WhsCode = Convert.ToString(dataTable.GetValue("WhsCode", ID - 1).ToString());

                String SiteType = Convert.ToString(dataTable.GetValue("SiteType", ID - 1).ToString());

                string siteid1 = Convert.ToString(dataTable.GetValue("AssetClass", ID - 1).ToString());


                if (dataTable.GetValue("DocType", ID - 1).ToString() == "GoodIssue")

                {

                    string _sql1 = @"Select OITL.DocLine , OITL.DocType From OITL where ItemCode = '" + ItemCode + "' and DocEntry = '" + DocEntry + "' and DocType = '" + 60 + "' ";

                    Utilities.ExecuteSQL(ref _recordSet11, _sql1);

                }

                if (dataTable.GetValue("DocType", ID - 1).ToString() == "APInvoice")

                {

                    string _sql1 = @"Select OITL.DocLine, OITL.DocType From OITL where ItemCode = '" + ItemCode + "' and DocEntry = '" + DocEntry + "' and DocType = '" + 18 + "' ";

                    Utilities.ExecuteSQL(ref _recordSet11, _sql1);

                }

                // string a= _recordSet11.Fields.Item(1).Value;

                Objtyp = Convert.ToString(_recordSet11.Fields.Item(1).Value);

                DocLine1 = Convert.ToString(_recordSet11.Fields.Item(0).Value);

                _sql = "select Series from NNM1 where ObjectCode='59' and BPLId='" + bplId + "' and Locked='N'";

                Utilities.ExecuteSQL(ref _recodst111, _sql);


                if (!Utilities.Application.Company.InTransaction)

                    Utilities.Application.Company.StartTransaction();


                SAPbobsCOM.Documents goodsReceipt = Utilities.Application.Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oInventoryGenEntry);

                goodsReceipt.DocDate = DateTime.Now;//dataTable.GetValue("DocDate", ID-1);

                goodsReceipt.Series = Convert.ToInt32(_recodst111.Fields.Item(0).Value);

                goodsReceipt.UserFields.Fields.Item("U_SiteType").Value = SiteType;

                goodsReceipt.UserFields.Fields.Item("U_SiteID").Value = siteid1;

                goodsReceipt.UserFields.Fields.Item("U_DocKey").Value = DocEntry;

                goodsReceipt.UserFields.Fields.Item("U_DocType").Value = dataTable.GetValue("DocType", ID - 1).ToString();

                goodsReceipt.UserFields.Fields.Item("U_BtchSer").Value = Batch;

                goodsReceipt.UserFields.Fields.Item("U_FItmCode").Value = ItemCode;

                goodsReceipt.UserFields.Fields.Item("U_GRNQty").Value = Openqty;

                goodsReceipt.Comments = dataTable.GetValue("Remarks", ID - 1).ToString();


                goodsReceipt.BPL_IDAssignedToInvoice = Convert.ToInt32(bplId);

                goodsReceipt.Lines.SetCurrentLine(0);

                goodsReceipt.Lines.ItemCode = ItemCode;

                goodsReceipt.Lines.ItemDescription = ItemName;

                goodsReceipt.Lines.Quantity = Openqty;

                goodsReceipt.Lines.Price = Price;

                goodsReceipt.Lines.WarehouseCode = WhsCode;


                _sql = "select CONVERT(NVARCHAR(50),GETDATE(),112) 'CDate' ";

                Utilities.ExecuteSQL(ref _recordSet, _sql);

                string CurrentDate = _recordSet.Fields.Item(0).Value;

                int count = 0;

                if (IsBatchManaged(ItemCode))

                {

                    Batch = "IBB" + CurrentDate + "~";

                    _sql = "select max(SUBSTRING (DistNumber,13,CHARINDEX('~',DistNumber))) 'count' from OBTN where ItemCode='" + ItemCode + "' and  LEFT( DistNumber ,12)='" + Batch + "' ";

                    Utilities.ExecuteSQL(ref _recordSet, _sql);

                    if (string.IsNullOrWhiteSpace(_recordSet.Fields.Item(0).Value))

                        count = 1;

                    else

                        count = Convert.ToInt32(_recordSet.Fields.Item(0).Value);

                    Batch = Batch + (count + 1);


                    goodsReceipt.Lines.BatchNumbers.SetCurrentLine(0);

                    goodsReceipt.Lines.BatchNumbers.BatchNumber = Batch;

                    goodsReceipt.Lines.BatchNumbers.Quantity = Openqty;

                    goodsReceipt.Lines.BatchNumbers.Add();

                }

                else if (IsSerialManaged(ItemCode))

                {

                    Batch = "IBS" + CurrentDate + "~";

                    _sql = "select max(SUBSTRING (DistNumber,13,CHARINDEX('~',DistNumber))) 'count' from OSRN where ItemCode='" + ItemCode + "' and  LEFT( DistNumber ,12)='" + Batch + "' ";

                    Utilities.ExecuteSQL(ref _recordSet, _sql);

                    if (string.IsNullOrWhiteSpace(_recordSet.Fields.Item(0).Value))

                        count = 1;

                    else

                        count = Convert.ToInt32(_recordSet.Fields.Item(0).Value);

                    Batch = Batch + (count + 1);


                    goodsReceipt.Lines.SerialNumbers.SetCurrentLine(0);

                    goodsReceipt.Lines.SerialNumbers.InternalSerialNumber = Batch;

                    goodsReceipt.Lines.SerialNumbers.Quantity = 1;

                    goodsReceipt.Lines.SerialNumbers.Add();

                }


                SAPbobsCOM.Recordset _recodst = null;

                _sql = @"EXEC GetBINLoction '" + Objtyp + "','" + ItemCode + "','" + DocEntry + "','" + WhsCode + "','" + Batch + "','" + DocLine1 + "'";

                Utilities.ExecuteSQL(ref _recodst, _sql);

                int ctrBin = 0;

                int ktr = _recodst.RecordCount;

                if (_recodst.RecordCount > 0)

                {

                    while (!_recodst.EoF)

                    {

                        goodsReceipt.Lines.BinAllocations.SetCurrentLine(ctrBin);

                        //goodsReceipt.Lines.BinAllocations. = Convert.ToInt32(_recodst.Fields.Item("Whse").Value);

                        goodsReceipt.Lines.BinAllocations.BinAbsEntry = Convert.ToInt32(_recodst.Fields.Item("BinAbs").Value);

                        goodsReceipt.Lines.BinAllocations.Quantity = Openqty;

                        goodsReceipt.Lines.BinAllocations.SerialAndBatchNumbersBaseLine = 0;

                        goodsReceipt.Lines.BinAllocations.Add();

                        ctrBin++;

                        _recodst.MoveNext();

                    }

                }


                goodsReceipt.Lines.Add();

                int retVal = goodsReceipt.Add();

                if (retVal != 0)

                {


                    // If process is in transaction, then the transaction is rolled back. 

                    if (Utilities.Application.Company.InTransaction)

                        Utilities.Application.Company.EndTransaction(SAPbobsCOM.BoWfTransOpt.wf_RollBack);


                    Utilities.Application.Company.GetLastError(out oErrorCode, out oErrorMsg);

                    Utilities.ShowErrorMessage(oErrorMsg); // Showing error message


                    // return false; // Returning false 

                }

                else

                {

                    if (Utilities.Application.Company.InTransaction)

                        Utilities.Application.Company.EndTransaction(SAPbobsCOM.BoWfTransOpt.wf_Commit);

                    Utilities.ShowSucessMessage("GRN posted successfully.");

                }

            }

            catch (Exception ex)

            {

                _Form.Freeze(false);

                Utilities.ShowErrorMessage(ex.Message); // Showing error message

                //return false;

            }

        }


        #endregion


        #region  IsBatchManaged

        public bool IsBatchManaged(string ItemCode)

        {

            string oSql = String.Empty;

            string oValue = String.Empty;

            try

            {

                if (Constants.ServerType == SAPbobsCOM.BoDataServerTypes.dst_HANADB)

                {

                    _sql = "SELECT IFNULL(\"ManBtchNum\", '') FROM \"OITM\" WHERE \"ItemCode\" = '" + ItemCode + "'";

                }

                else

                {

                    _sql = "SELECT ISNULL(ManBtchNum, '') FROM OITM WHERE ItemCode = '" + ItemCode + "'";

                }


                Utilities.ExecuteSQL(ref _recordSet, _sql);


                if (_recordSet.RecordCount > 0)

                {

                    oValue = Convert.ToString(_recordSet.Fields.Item(0).Value);

                    if (!String.IsNullOrEmpty(oValue) && oValue.ToUpper() == "Y")

                        return true;

                    else

                        return false;

                }

                return false;

            }

            catch (Exception ex)

            {

                Utilities.ShowErrorMessage(ex.Message);

                return false;

            }

        }

        #endregion


        #region  IsSerialManaged

        public bool IsSerialManaged(string ItemCode)

        {

            string oSql = String.Empty;

            string oValue = String.Empty;

            try

            {

                if (Constants.ServerType == SAPbobsCOM.BoDataServerTypes.dst_HANADB)

                {

                    _sql = "SELECT IFNULL(\"ManBtchNum\", '') FROM \"OITM\" WHERE \"ItemCode\" = '" + ItemCode + "'";

                }

                else

                {

                    _sql = "SELECT ISNULL(ManSerNum, '') FROM OITM WHERE ItemCode = '" + ItemCode + "'";

                }

                Utilities.ExecuteSQL(ref _recordSet, _sql);


                if (_recordSet.RecordCount > 0)

                {

                    oValue = Convert.ToString(_recordSet.Fields.Item(0).Value);


                    if (!String.IsNullOrEmpty(oValue) && oValue.ToUpper() == "Y")

                        return true;

                    else

                        return false;

                }

                return false;

            }

            catch (Exception ex)

            {

                Utilities.ShowErrorMessage(ex.Message);

                return false;

            }

        }

  1. Post Asset Master Item

private void PostAssetMaster(int ctr)

        {

            try

            {

                string oErrorMsg = String.Empty;

                int oErrorCode = 0;

                //int rowcount = 1;

                if (_Form.DataSources.UserDataSources.Item("DcTyp").ValueEx == "All")

                {

                    _DocType = "All";

                }

                else if (_Form.DataSources.UserDataSources.Item("DcTyp").ValueEx == "GoodIssue")

                {

                    _DocType = "GoodIssue";

                }

                else if (_Form.DataSources.UserDataSources.Item("DcTyp").ValueEx == "APInvoice")

                {

                    _DocType = "APInvoice";

                }

                else if (_Form.DataSources.UserDataSources.Item("DcTyp").ValueEx == "JE")

                {

                    _DocType = "JE";

                }

                SAPbobsCOM.Recordset _recSetMax = (SAPbobsCOM.Recordset)Utilities.Application.Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);

                Int32 ID = Convert.ToInt32(((SAPbouiCOM.EditText)_mtMatrix.Columns.Item("0").Cells.Item(ctr).Specific).Value);

                _DocType = dataTable.GetValue("DocType", ID - 1).ToString();

                _Circle = _Form.DataSources.UserDataSources.Item("Circle").ValueEx;

                _SiteID = _Form.DataSources.UserDataSources.Item("SiteId").ValueEx;

                _SiteName = _Form.DataSources.UserDataSources.Item("SiteNm").ValueEx;


                SAPbobsCOM.Items oItem = (SAPbobsCOM.Items)Utilities.Application.Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oItems);

                {



                    SAPbobsCOM.Recordset oRs11 = (SAPbobsCOM.Recordset)Utilities.Application.Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);

                    _sql = "select DocType from OPCH where DocEntry='" + dataTable.GetValue("DocEntry", ID - 1).ToString() + "'";

                    Utilities.ExecuteSQL(ref oRs11, _sql);

                    //_mtMatrix.AddRow(1, ctr);

                    //_mtMatrix.GetLineData(ctr);

                    oItem.UserFields.Fields.Item("U_ObjType").Value = dataTable.GetValue("DocType", ID - 1).ToString();// _Form.DataSources.UserDataSources.Item("DocType").ValueEx;

                    oItem.UserFields.Fields.Item("U_DocEntry").Value = dataTable.GetValue("DocEntry", ID - 1).ToString();

                    if (_DocType == "JE" || (_DocType == "APInvoice" && oRs11.Fields.Item(0).Value == "S"))

                    {

                        oItem.UserFields.Fields.Item("U_ItemCode").Value = dataTable.GetValue("FItemCode", ID - 1).ToString();

                    }

                    else

                    {

                        oItem.UserFields.Fields.Item("U_ItemCode").Value = dataTable.GetValue("INVItemCode", ID - 1).ToString();

                    }

                    String qww = Form.DataSources.UserDataSources.Item("OpenQty").ValueEx.ToString();

                    oItem.UserFields.Fields.Item("U_Qty").Value = Form.DataSources.UserDataSources.Item("OpenQty").ValueEx.ToString();

                    oItem.UserFields.Fields.Item("U_BtchSer").Value = dataTable.GetValue("AssetBatchSerialNo", ID - 1).ToString();

                    DocTp = dataTable.GetValue("DocType", ID - 1).ToString();

                    _sql = "select   [dbo].[GETMAXREGNOO]('" + DocTp + "')";

                    Utilities.ExecuteSQL(ref _recSetMax, _sql);


                    SAPbobsCOM.Recordset oRs2 = (SAPbobsCOM.Recordset)Utilities.Application.Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);

                    String ItemCode = dataTable.GetValue("INVItemCode", ID - 1).ToString();

                    oRs2.DoQuery("Select CAST(RIGHT(OITB.ItmsGrpNam,1) as int),OSAC.AbsEntry From OITM Left Join OITB ON OITM.ItmsGrpCod= OITB.ItmsGrpCod Left Join OSAC on OITM.SACEntry = OSAC.AbsEntry  where  ItemCode = '" + ItemCode + "' ");


                    if (oRs2.Fields.Item(0).Value == 1)

                    {

                        oItem.ItemClass = SAPbobsCOM.ItemClassEnum.itcMaterial;

                        oItem.ItemsGroupCode = 133;

                    }

                    else if (oRs2.Fields.Item(0).Value == 2)

                    {

                        oItem.ItemClass = SAPbobsCOM.ItemClassEnum.itcService;

                        oItem.ItemsGroupCode = 134;

                        oItem.SACEntry = Convert.ToInt32(oRs2.Fields.Item(1).Value);

                    }

                    else

                    {

                        oItem.ItemClass = SAPbobsCOM.ItemClassEnum.itcService;

                        oItem.ItemsGroupCode = 134;

                        oItem.ServiceCategoryEntry = 1;

                        oItem.SACEntry = 1;

                    }

                    //oItem.ItemClass = SAPbobsCOM.ItemClassEnum.itcMaterial;

                    oItem.ForeignName = dataTable.GetValue("INVItemCode", ID - 1).ToString();

                    oItem.ItemCode = _recSetMax.Fields.Item(0).Value;  //_Form.DataSources.UserDataSources.Item("FAItmCd").ValueEx;

                    oItem.ItemName = dataTable.GetValue("ItemDescription", ID - 1).ToString();

                    if (dataTable.GetValue("GST", ID - 1).ToString() == "Y")

                    {

                        oItem.GSTRelevnt = SAPbobsCOM.BoYesNoEnum.tYES;

                    }

                    else if (dataTable.GetValue("GST", ID - 1).ToString() == "N")

                    {

                        oItem.GSTRelevnt = SAPbobsCOM.BoYesNoEnum.tNO;

                    }

                    if (!string.IsNullOrWhiteSpace(dataTable.GetValue("Location", ID - 1).ToString()))

                    {

                        oItem.Location = Convert.ToInt32(dataTable.GetValue("Location", ID - 1).ToString());

                    }

                    oItem.SalesItem = SAPbobsCOM.BoYesNoEnum.tNO;

                    oItem.AssetClass = dataTable.GetValue("AssetClass", ID - 1).ToString();

                    oItem.AssetGroup = dataTable.GetValue("AssetGroup", ID - 1).ToString();

                    if (dataTable.GetValue("MaterialType", ID - 1).ToString() == "1")

                    {

                        oItem.MaterialType = SAPbobsCOM.BoMaterialTypes.mt_RawMaterial;

                    }

                    else if (dataTable.GetValue("MaterialType", ID - 1).ToString() == "2")

                    {

                        oItem.MaterialType = SAPbobsCOM.BoMaterialTypes.mt_GoodsInProcess;

                    }


                    else if (dataTable.GetValue("MaterialType", ID - 1).ToString() == "3")

                    {

                        oItem.MaterialType = SAPbobsCOM.BoMaterialTypes.mt_FinishedGoods;

                    }

                    if (dataTable.GetValue("TaxCategories", ID - 1).ToString() == "R")

                    {

                        oItem.GSTTaxCategory = SAPbobsCOM.GSTTaxCategoryEnum.gtc_Regular;

                    }

                    else if (dataTable.GetValue("TaxCategories", ID - 1).ToString() == "E")

                    {

                        oItem.GSTTaxCategory = SAPbobsCOM.GSTTaxCategoryEnum.gtc_Exempt;

                    }

                    else if (dataTable.GetValue("TaxCategories", ID - 1).ToString() == "N")

                    {

                        oItem.GSTTaxCategory = SAPbobsCOM.GSTTaxCategoryEnum.gtc_NilRated;

                    }

                    if (!string.IsNullOrWhiteSpace(dataTable.GetValue("AbsEntry", ID - 1).ToString()))

                    {

                        oItem.ChapterID = Convert.ToInt32(dataTable.GetValue("AbsEntry", ID - 1).ToString());

                    }

                    oItem.ItemType = SAPbobsCOM.ItemTypeEnum.itFixedAssets;

                    //_mtMatrix.SetLineData(ctr);

                    //oItem.Add();

                }

                int retVal = oItem.Add();

                if (retVal != 0)

                {


                    if (Utilities.Application.Company.InTransaction)

                        Utilities.Application.Company.EndTransaction(SAPbobsCOM.BoWfTransOpt.wf_RollBack);


                    Utilities.Application.Company.GetLastError(out oErrorCode, out oErrorMsg);


                    Utilities.ShowErrorMessage(oErrorMsg);

                    Form.Freeze(false);

                }


                else

                {

                    Utilities.ShowSucessMessage("Fixed Asset Created Successfully");

                }

            }

            catch (Exception ex)

            {

                Form.Freeze(false);

                Utilities.ShowErrorMessage(ex.Message);

            }

            finally

            { }

        }


  1. Updating Child Table in User Defined Object (UDO) Using SAP B1 General Service

SAPbobsCOM.GeneralService oGeneralService = null;

SAPbobsCOM.GeneralData oGeneralData = null;

SAPbobsCOM.GeneralDataParams oGeneralParams = null;

SAPbobsCOM.CompanyService sCmp = null;

SAPbobsCOM.GeneralData oChild = null;

SAPbobsCOM.GeneralDataCollection oChildren = null;

sCmp = Main.Mahi_Company.GetCompanyService(); 


oRs = ((SAPbobsCOM.Recordset)(Main.Mahi_Company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)));

string selquery = "select " + '"' + "DocEntry" + '"' + ",'1' as " + '"' + "LineId" + '"' + ",'0' as " + '"' + "VisOrder" + '"' + ",'SQ_AC' as " + '"' + "Object" + '"' + ",null as " + '"' + "LogInst" + '"' + ",'Y' as " + '"' + "U_Checked" + '"' + "," + '"' + "U_DocNum" + '"' + "," + '"' + "U_SQSeries" + '"' + "," + '"' + "U_SQLineId" + '"' + "," + '"' + "U_iCode" + '"' + "," + '"' + "U_iName" + '"' + "," + '"' + "U_LineId" + '"' + " from " + '"' + "@SQAC_H" + '"';


oRs.DoQuery(selquery);


try

{                                

 oGeneralService = sCmp.GetGeneralService("Mahi_H");  // Main UDO Name

 for (int i = 1; i <= 6; i++)

 {

 // Get UDO record                

 oGeneralParams = ((SAPbobsCOM.GeneralDataParams)(oGeneralService.GetDataInterface(SAPbobsCOM.GeneralServiceDataInterfaces.gsGeneralDataParams)));

 oGeneralParams.SetProperty("DocEntry", oRs.Fields.Item("DocEntry").Value);      //Primary Key

 oGeneralData = oGeneralService.GetByParams(oGeneralParams);

 

// Add lines on UDO Child Table                

 oChildren = oGeneralData.Child("Mahi_D1");  // Child Table Of Main UDO


// Create data for rows in the child table                

oChild = oChildren.Add();      // Adds Blank Line in Child Table          

oChild.SetProperty("U_SQLineId", oRs.Fields.Item("U_SQLineId").Value);

//oChild.SetProperty("VisOrder", oRs.Fields.Item("VisOrder").Value);

oChild.SetProperty("U_Checked", oRs.Fields.Item("U_Checked").Value);

oChild.SetProperty("U_DocNum", oRs.Fields.Item("U_DocNum").Value);

oChild.SetProperty("U_SQSeries", oRs.Fields.Item("U_SQSeries").Value);

oChild.SetProperty("U_SQLineId", oRs.Fields.Item("U_SQLineId").Value);

oChild.SetProperty("U_iCode", oRs.Fields.Item("U_iCode").Value);

oChild.SetProperty("U_iName", oRs.Fields.Item("U_iName").Value);

                                    

//Update the UDO Record                

oGeneralService.Update(oGeneralData);   // If Child Table doesnot have any record it will create else update the existing one

oRs.MoveNext();

}

}

catch (Exception ex)

{

Main.Mahi_Application.MessageBox(ex.Message, 1, "OK", null, null);

}