//Consulta o o último Id por objeto na tabela customizada protected int ConsultarUltimoId(string nomeObjeto) { int ultimo = 0; try { oCompService = oCompany.GetCompanyService(); oGeneralService = oCompService.GetGeneralService(nomeObjeto); try { oGeneralData = (SAPbobsCOM.GeneralData)oGeneralService.GetDataInterface(GeneralServiceDataInterfaces.gsGeneralData); GeneralCollectionParams oGeneralCol = oGeneralService.GetList(); List <int> lista = new List <int>(); foreach (GeneralDataParams item in oGeneralCol) { string aux = item.GetProperty("Code").ToString(); if (aux != "*") { lista.Add(Convert.ToInt32(aux)); } } lista.Sort(); ultimo = lista.Last(); } catch (Exception e) { //Não existe registro no BD if (e.HResult == -2028) { return(ultimo); } //Registro Duplicado else if (e.HResult == -2035) { throw e; } } } catch (Exception e) { throw e; } return(ultimo); }