Base solution for your next web application
Open Closed

Add Sub-OrganizationUnit using OrganizationUnit Extended #3182


User avatar
0
inovatech created

I made an extension of the OrganizationUnit entity, including new fields in the AbpOrganizationUnit table, and it is working very well except for the inclusion of the Sub-Units. The root Organization Unit´s are being added normally. If I do not pass the parentId parameter, the inclusion occurs, but as root Organization Unit.

My extension class is (Cad_Deposito): <span style="color:#BF0000"> public class Cad_Deposito : OrganizationUnit, IEndereco, IFlgInativo, IFullAudited { public const int MaxLengthDescr = 500; public const int MaxLengthCEP = 9; public const int MaxCodIBGE = 9999999; public const int MaxNumEndereco = 99999; public const int MaxSglUFEndereco = 2; public const int MaxCodCNPJ = 18;

    [Required]
    [MaxLength(MaxCodCNPJ)]
    public virtual string CodCNPJ { get; set; }

    [Required]
    [MaxLength(MaxLengthDescr)]
    public virtual string BairroEndereco { get; set; }

    [Required]
    [MaxLength(MaxLengthCEP)]
    public virtual string CEPEndereco { get; set; }

    [Required]
    [MaxLength(MaxLengthDescr)]
    public virtual string CidadeEndereco { get; set; }

    [Required]
    public virtual int CodIBGEEndereco { get; set; }

    [MaxLength(MaxLengthDescr)]
    public virtual string ComplEndereco { get; set; }

    [Required]
    [Range(0, MaxNumEndereco)]
    public virtual int NumEndereco { get; set; }

    [Required]
    [MaxLength(MaxLengthDescr)]
    public virtual string RuaEndereco { get; set; }

    [Required]
    [MaxLength(MaxSglUFEndereco)]
    public virtual string SiglaUFEndereco { get; set; }

    [Required]
    public virtual bool FlgInativo { get; set; }

    public long? FlgInativoUserId { get; set; }

    public DateTime? FlgInativoTime { get; set; }

    [Required]
    public virtual bool FlgMatriz { get; set; }

    public virtual ICollection&lt;Cad_DepositoContato&gt; Contatos { get; set; }

    /// &lt;summary&gt;
    /// Indica se a Organization Unit é P = Planta, A = Armazém, D = Depositante
    /// &lt;/summary&gt;
    [Required]
    public string FlgTipoOrganizationUnit { get; set; }

    [Required]
    public virtual int TipoDocumentacaoId { get; set; }
    [ForeignKey("TipoDocumentacaoId")]
    public virtual Cad_Tipo_Documentacao Cad_Tipo_Documentacao { get; set; }
}&lt;/span&gt;

In OrganizationUnitAppService I changed the "CreateOrganizationUnit":

<span style="color:#BF0000"> public async Task<OrganizationUnitDto> CreateOrganizationUnit(CreateOrganizationUnitInput input) {

        string lFlgTipoOU = "";

        if (input.ParentId.ToString().Length == 0) { lFlgTipoOU = "P"; };
        if (input.ParentId.ToString().Length == 5) { lFlgTipoOU = "A"; };
        if (input.ParentId.ToString().Length == 11) { lFlgTipoOU = "D"; };
        

        var organizationUnit = new Cad_Deposito()
        {
            TenantId = AbpSession.TenantId,
            DisplayName = input.DisplayName,
            ParentId = input.ParentId,
            CodCNPJ = input.CodCNPJ,
            BairroEndereco = input.BairroEndereco,
            CEPEndereco = input.CEPEndereco,
            CidadeEndereco = input.CidadeEndereco,
            CodIBGEEndereco = input.CodIBGEEndereco,
            ComplEndereco = input.ComplEndereco,
            NumEndereco = input.NumEndereco,
            RuaEndereco = input.RuaEndereco,
            SiglaUFEndereco = input.SiglaUFEndereco,
            FlgInativo = input.FlgInativo,
            FlgMatriz = input.FlgMatriz,
            FlgTipoOrganizationUnit = lFlgTipoOU,
            TipoDocumentacaoId = input.TipoDocumentacaoId

        };

        await _organizationUnitManager.CreateAsync(organizationUnit);
        await CurrentUnitOfWork.SaveChangesAsync();

        return organizationUnit.MapTo&lt;OrganizationUnitDto&gt;();
    }&lt;/span&gt;

But, the error in the Audit Log is: <span style="color:#BF0000"> System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. em System.Data.Entity.Internal.InternalContext.SaveChangesAsync(CancellationToken cancellationToken) em System.Data.Entity.Internal.LazyInternalContext.SaveChangesAsync(CancellationToken cancellationToken) em Abp.EntityFramework.AbpDbContext.<SaveChangesAsync>d__34.MoveNext() na D:\Halil\GitHub\aspnetboilerplate\src\Abp.EntityFramework\EntityFramework\AbpDbContext.cs:linha 204 --- Fim do rastreamento de pilha do local anterior onde a exceção foi gerada --- em System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) em System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) em Abp.EntityFramework.Uow.EfUnitOfWork.<SaveChangesInDbContextAsync>d__23.MoveNext() na D:\Halil\GitHub\aspnetboilerplate\src\Abp.EntityFramework\EntityFramework\Uow\EfUnitOfWork.cs:linha 209 --- Fim do rastreamento de pilha do local anterior onde a exceção foi gerada --- em System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) em System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) em Abp.EntityFramework.Uow.EfUnitOfWork.<SaveChangesAsync>d__14.MoveNext() na D:\Halil\GitHub\aspnetboilerplate\src\Abp.EntityFramework\EntityFramework\Uow\EfUnitOfWork.cs:linha 80 --- Fim do rastreamento de pilha do local anterior onde a exceção foi gerada --- em System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) em System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) em System.Runtime.CompilerServices.TaskAwaiter.GetResult() em Inovatech.INVTC.Organizations.OrganizationUnitAppService.<CreateOrganizationUnit>d__10.MoveNext() na C:\INVTC_aspnetzero_v1_12 - Copia\Inovatech.INVTC.Application\Organizations\OrganizationUnitAppSer... </span> This is the input: <span style="color:#BF0000">{ "input": { "parentId": 64, "displayName": "Santos", "codCNPJ": "95.959.595/9595-95", "bairroEndereco": "Alto da Mooca", "cepEndereco": "03178-120", "cidadeEndereco": "São Paulo", "codIBGEEndereco": 3550308, "complEndereco": null, "numEndereco": 0, "ruaEndereco": "Rua Vladimir Jorge", "siglaUFEndereco": "SP", "flgInativo": false, "flgMatriz": false, "flgTipoOrganizationUnit": null, "tipoDocumentacaoId": 1 } }</span>

Please, help me !!!


7 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    As I see FlgTipoOrganizationUnit field is required but you send null for it in your input. By the way, you can write all of your questions to <a class="postlink-local" href="https://forum.aspnetboilerplate.com/viewforum.php?f=5">viewforum.php?f=5</a>, we answer there faster.

    Thanks.

  • User Avatar
    0
    inovatech created

    I fill in the value of the field inside the CreateOrganizationUnit routine.

    <span style="color:#BF0000">String lFlgTipoOU = "";

    If (input.ParentId.ToString (). Length == 0) {lFlgTipoOU = "P"; }; If (input.ParentId.ToString (). Length == 5) {lFlgTipoOU = "A"; }; If (input.ParentId.ToString (). Length == 11) {lFlgTipoOU = "D"; }; ... FlgTipoOrganizationUnit = lFlgTipoOU,</span>

    Creating a root OrganizationUnit is working correctly. Only the sub-OrganizationUnit is experiencing a problem. I think it's something in relation to the Code field, because only when I pass the ParentId field does the error occur.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you send your project to <a href="mailto:[email protected]">[email protected]</a>, I will take a look at it. I think it will be faster to understand reason in that way.

    Thanks.

  • User Avatar
    0
    inovatech created

    Hi,

    I sent my project via email as you requested. Please help me and thanks for your attention.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Thank you for sharing your project. In this code block you set value of "lFlgTipoOU".

    if (input.ParentId.ToString().Length == 0) { lFlgTipoOU = "P"; };
    if (input.ParentId.ToString().Length == 5) { lFlgTipoOU = "A"; };
    if (input.ParentId.ToString().Length == 11) { lFlgTipoOU = "D"; };
    

    But when you save the first organization unit, it's Id will be 1. So, when you save the second/sub organization unit, lenght of it's parentId will be 1.

    In that case, none of your above if statements will hit and value of "lFlgTipoOU" will be empty string and your save operation will fail.

    I don't know your bussiness logic but you need to set a value to "lFlgTipoOU" if it is required on your entity.

    Thanks.

  • User Avatar
    0
    inovatech created

    How did I not see it !!! ?? It was to test the length of the ParentId Code, not the ParentId itself. :oops: :geek: Thank you very much!!! Now everything is working.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Great :)