I have the following script task in a bpm.
This is responsible for taking the results of this api and inserting the records into a Creatio object.
Script task:
var baseUrl = "https://angelbaraldo.creatio.com"; var authUrl = baseUrl + "/ServiceModel/AuthService.svc/Login"; var apiUrl = baseUrl + "/0/rest/CustomStockService/GetNoConciliadosData"; var username = "MyUser"; var password = "MyPassword"; // Cuerpo de autenticación var authBody = new { UserName = username, UserPassword = password }; var authBodyJson = Newtonsoft.Json.JsonConvert.SerializeObject(authBody); // Solicitud de autenticación var authRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(authUrl); authRequest.Method = "POST"; authRequest.ContentType = "application/json; charset=utf-8"; using (var streamWriter = new System.IO.StreamWriter(authRequest.GetRequestStream())) { streamWriter.Write(authBodyJson); } string authCookies = null; string bpmcsrf = null; // Obtener la respuesta de autenticación using (var authResponse = (System.Net.HttpWebResponse)authRequest.GetResponse()) { authCookies = authResponse.Headers["Set-Cookie"]; // Buscar el token BPMCSRF en las cookies var cookies = authResponse.Headers.GetValues("Set-Cookie"); if (cookies != null) { foreach (var cookie in cookies) { if (cookie.Contains("BPMCSRF")) { bpmcsrf = cookie.Split('=')[1].Split(';')[0]; } } } if (string.IsNullOrEmpty(authCookies) || string.IsNullOrEmpty(bpmcsrf)) { throw new Exception("Autenticación fallida: no se obtuvieron cookies o token BPMCSRF."); } } // Solicitud autenticada a la API var apiRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(apiUrl); apiRequest.Method = "GET"; apiRequest.ContentType = "application/json"; apiRequest.Headers["ForceUseSession"] = "true"; apiRequest.Headers["Cookie"] = authCookies; apiRequest.Headers["BPMCSRF"] = bpmcsrf; // Obtener respuesta de la API string apiResponseContent; using (var apiResponse = (System.Net.HttpWebResponse)apiRequest.GetResponse()) { using (var streamReader = new System.IO.StreamReader(apiResponse.GetResponseStream())) { apiResponseContent = streamReader.ReadToEnd(); } } // Parsear los datos de la API var jsonRecords = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>>>(apiResponseContent); // Procesar los registros (como en tu código original) var cantidadInsertados = 0; foreach (var record in jsonRecords) { var tipo = record.ContainsKey("Tipo") ? record["Tipo"]?.ToString() : null; var letter = record.ContainsKey("Letter") ? record["Letter"]?.ToString() : null; var cardCode = record.ContainsKey("CardCode") ? record["CardCode"]?.ToString() : null; var nroComprobante = record.ContainsKey("Nro Comprobante") ? record["Nro Comprobante"]?.ToString() : null; var nroOv = record.ContainsKey("Nro OV") ? record["Nro OV"]?.ToString() : null; var pymentGroup = record.ContainsKey("PymntGroup") ? record["PymntGroup"]?.ToString() : null; var docDate = record.ContainsKey("DocDate") ? record["DocDate"]?.ToString() : null; var vencimiento = record.ContainsKey("Vencimiento") ? record["Vencimiento"]?.ToString() : null; var docCur = record.ContainsKey("DocCur") ? record["DocCur"]?.ToString() : null; var docRate = record.ContainsKey("DocRate") ? record["DocRate"] : null; var debe = record.ContainsKey("Debe") ? record["Debe"] : null; var haber = record.ContainsKey("Haber") ? record["Haber"] : null; var debeUsd = record.ContainsKey("Debe USD") ? record["Debe USD"] : null; var haberUsd = record.ContainsKey("Haber USD") ? record["Haber USD"] : null; // Verificar si el registro ya existe var esq = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "AgbCuentaCteSocioNegocios"); var idColumn = esq.AddColumn("Id"); esq.Filters.Add(esq.CreateFilterWithParameters(FilterComparisonType.Equal, "AgbCuemtaCteNumeroComprobante", nroComprobante)); var entityCollection = esq.GetEntityCollection(UserConnection); // Consultar la cuenta relacionada var esq2 = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "Account"); var accountIdColumn = esq2.AddColumn("Id"); esq2.Filters.Add(esq2.CreateFilterWithParameters(FilterComparisonType.Equal, "AgbCodigoSocioDeNegocios", cardCode)); var accountCollection = esq2.GetEntityCollection(UserConnection); Guid? accountId = accountCollection.Count == 1 ? accountCollection[0].GetTypedColumnValue<Guid>(accountIdColumn.Name) : (Guid?)null; // Insertar el registro si no existe if (entityCollection.Count == 0 && !string.IsNullOrEmpty(nroComprobante)) { var newEntity = UserConnection.EntitySchemaManager.GetInstanceByName("AgbCuentaCteSocioNegocios").CreateEntity(UserConnection); newEntity.SetDefColumnValues(); newEntity.SetColumnValue("AgbCuemtaCteNumeroComprobante", nroComprobante); newEntity.SetColumnValue("AgbCuentaCteCodigoSn", cardCode); newEntity.SetColumnValue("AgbCuentaCteCondicionPago", pymentGroup); newEntity.SetColumnValue("AgbCuentaCteCuentas", accountId); newEntity.SetColumnValue("AgbCuentaCteDebe", debe); newEntity.SetColumnValue("AgbCuentaCteDebeUsd", debeUsd); newEntity.SetColumnValue("AgbCuentaCteFechaDocumento", docDate); newEntity.SetColumnValue("AgbCuentaCteFechaVencimiento", vencimiento); newEntity.SetColumnValue("AgbCuentaCteHaber", haber); newEntity.SetColumnValue("AgbCuentaCteHaberUsd", haberUsd); newEntity.SetColumnValue("AgbCuentaCteLetra", letter); newEntity.SetColumnValue("AgbCuentaCteMoneda", docCur); newEntity.SetColumnValue("AgbCuentaCteNumeroOv", nroOv); newEntity.SetColumnValue("AgbCuentaCteTipoCambio", docRate); newEntity.SetColumnValue("AgbCuentaCteTipoDocumento", tipo); newEntity.Save(); // Incrementar el contador de registros insertados cantidadInsertados++; } } // Establecer el valor de registros insertados en la variable del proceso Set<int>("CantidadInsertados", cantidadInsertados); return true;
But when I run the bpm I get this error
Error:
System.Net.WebException: The remote server returned an error: (401) Unauthorized. at System.Net.HttpWebRequest.GetResponse() at Terrasoft.Core.Process.AgbProcess_93e521fAngelBaraldo1MethodsWrapper.ScriptTask1Execute(ProcessExecutingContext context) at Terrasoft.Core.Process.ProcessScriptTask.InternalExecute(ProcessExecutingContext context) at Terrasoft.Core.Process.ProcessFlowElement.CallInternalExecute(ProcessExecutingContext context) at Terrasoft.Core.Process.ProcessFlowElement.ExecuteItem(ProcessExecutingContext context) at Terrasoft.Core.Process.ProcessFlowElement.Execute(ProcessExecutingContext context)
Like
1 comments
22:23 Dec 27, 2024
Are you trying to send requests from Creatio to the same Creatio environment?
If that's so, it's really strange implementation. I think it's much better to call the service logic directly rather then send an HTTP request to the endpoint and parse the response json.
the mock code would look like this
var service = new CustomStockServiceHelper(userConnection); var response = service.GetNoConciliadosData();
I assumed that you use a CustomStockServiceHelper in CustomStockService to retrieve the data.
DM me if you need more help.
Best regards,
Yurii
Show all comments