Text parameter in to a Lookup

I am developing a business process that takes data from an API and inserts it into an object.
I have some parameters created in it and I need to make some improvements to be able to insert them.
The parameters in question are the following, UserName = Text and UserApi = Lookup (Based on Contact)
I am trying to relate the username with the userapi lookup so that later this userapi is inserted into the contact lookup in the destination object:
// Obtener la fecha de la API, puede ser un string o "false"
string fechaApi = Get("FechaApertura");
string XStudioUsuarios = Get("XStudioUsuarios");
string ExpectedRevenue = Get("ExpectedRevenue");
string UserName = Get("UserName");
Guid UserApi = Get("UserApi");

// Declarar la variable para almacenar la nueva fecha
DateTime fechaNueva;
int XStudioUsuariosInt;
int ExpectedRevenueInt;

if (!string.IsNullOrEmpty(UserName) && UserName != "false") {
   Set("UserApi.Name", UserName);
}

if (fechaApi != "false") {
   // Intentar parsear el string de fecha a DateTime
   if (DateTime.TryParse(fechaApi, out fechaNueva)) {
       // Establecer la nueva fecha si el parseo fue exitoso
       Set("FechaAperturaNueva", fechaNueva);
   }
}

if (XStudioUsuarios != "false") {
   // Intentar parsear el string a int
   if (int.TryParse(XStudioUsuarios, out XStudioUsuariosInt)) {
       // Establecer el nuevo valor si el parseo fue exitoso
       Set("XStudioUsuariosInt", XStudioUsuariosInt);
   }
}

if (ExpectedRevenue != "false") {
   // Intentar parsear el string a int
   if (int.TryParse(ExpectedRevenue, out ExpectedRevenueInt)) {
       // Establecer el nuevo valor si el parseo fue exitoso
       Set("ExpectedRevenueInt", ExpectedRevenueInt);
   }
}

return true;

Like 0

Like

3 comments

One of the options is to use the ORM. The idea here is to first check if the received text value is already present in the lookup or not. To do that perform a simple select from the entity of the lookup and check if there are values there that are the same as the received text value. Examples of retrieving data can be found here. If the value is present in the lookup - set the Id of that lookup record for your main record lookup column (as described in the examples here).

 

If the value doesn't exist in the lookup - create it and then set the Id of the created lookup record as the value for the lookup column in your main record.

Oleg Drobina,

Thanks Oleg, what you describe is exactly what I need to do, but I still don't know how I can do it.

Oleg i do this Code with the information than you gave me:


string fechaApi = Get<string>("FechaApertura");
string XStudioUsuarios = Get<string>("XStudioUsuarios");
string ExpectedRevenue = Get<string>("ExpectedRevenue");
string UserName = Get<string>("UserName");
Guid UserApi = Get<Guid>("UserApi");


DateTime fechaNueva;
int XStudioUsuariosInt;
int ExpectedRevenueInt;

if (!string.IsNullOrEmpty(UserName) && UserName != "false") {
    
   var esq = new EntitySchemaQuery(UserConnection.EntitySchemaManager, "Contact");
    var idColumn = esq.AddColumn("Id");
    esq.Filters.Add(esq.CreateFilterWithParameters(FilterComparisonType.Equal, "Name", UserName));
    var entityCollection = esq.GetEntityCollection(UserConnection);
    
    // Verificar si se encontraron resultados
    if (entityCollection.Count > 0) {
        var entity = entityCollection[0];

        var userApiLookup = new {
           Value = entity.GetTypedColumnValue<Guid>(Id.Name),
           DisplayValue = entity.GetTypedColumnValue<string>(Name.Name)
       };
    
        // Establecer el objeto Lookup en UserApi
        Set<Guid>("UserApi", userApiLookup);
    } 
}

if (fechaApi != "false") {
   // Intentar parsear el string de fecha a DateTime
   if (DateTime.TryParse(fechaApi, out fechaNueva)) {
       // Establecer la nueva fecha si el parseo fue exitoso
       Set<DateTime>("FechaAperturaNueva", fechaNueva);
   }
}

if (XStudioUsuarios != "false") {
   // Intentar parsear el string a int
   if (int.TryParse(XStudioUsuarios, out XStudioUsuariosInt)) {
       // Establecer el nuevo valor si el parseo fue exitoso
       Set<int>("XStudioUsuariosInt", XStudioUsuariosInt);
   }
}

if (ExpectedRevenue != "false") {
   // Intentar parsear el string a int
   if (int.TryParse(ExpectedRevenue, out ExpectedRevenueInt)) {
       // Establecer el nuevo valor si el parseo fue exitoso
       Set<int>("ExpectedRevenueInt", ExpectedRevenueInt);
   }
}

return true;
 

Show all comments