Do not allow Contact association if Max Contacts Allowed value is exceeded on the Account record

Поділитися
Вставка
  • Опубліковано 11 вер 2024

КОМЕНТАРІ • 13

  • @user-pz5ys9mj2q
    @user-pz5ys9mj2q 8 місяців тому +1

    From this video I experienced that I'm new that company and working in already existing project, this is because of your selected content and the way of your delivery, because it's differed from video to video. Thank you.

  • @shridharkatkati3113
    @shridharkatkati3113 Місяць тому +1

    I suppose this will not work when inserting contacts in bulk, as trigger executes in batches. How will you know the number of contacts tagged to an account when, in bulk insert operation there might be multiple contacts with same account id which might exeed the max_contacts_allowed__c value on account object.

    • @salesforcemakessense
      @salesforcemakessense  Місяць тому

      Hi, this use case will obviously be credible for scenarios within limits.
      In any case there is a chance child records or number of records go beyond expectations/limits, it is recommended to go via Batch apex.

  • @user-co6wi9pk8p
    @user-co6wi9pk8p 10 місяців тому +1

    Note: Very Very Important point
    please don't write the code inside an already existing method instead start by creating a fresh method because for beginners it becomes very complex to understand the code

    • @salesforcemakessense
      @salesforcemakessense  10 місяців тому +1

      Feedback notes 😄
      Let me know if theres something you want me to help you explain. I can give a try.

    • @user-pz5ys9mj2q
      @user-pz5ys9mj2q 8 місяців тому +1

      @user-co6wi9pk8p
      No need of giving note bro, if you follow the content from starting video of apex, you your self realized that, it's in step by step enhancement.
      It's a part of enhancing the subject.

    • @salesforcemakessense
      @salesforcemakessense  8 місяців тому +1

      @@user-pz5ys9mj2q true

  • @yashaswish9532
    @yashaswish9532 Рік тому +1

    Hi Himanshu, I did a lil modification in code. Is the following correct?
    public void conmethod(list nlcon){
    set accId = new set();
    map conacc = new map();
    for(contact c : nlcon){
    if(c.AccountId !=null){
    accId.add(c.AccountId);
    conacc.put(c.id,c.AccountId);
    }
    }
    map macc= new map([select id, MaxCon__c,(select id from contacts) from Account where id in : accId]);
    map accConCount = new map();
    for(id accId : macc.keyset()){
    integer conCount = (macc.get(id).contacts).size();
    accConCount.put(id,conCount);
    }
    for(contact c : nlcon){
    if(accConCount.get(conacc.get(c.id))>=macc.get(conacc.get(c.id)).MaxCon__c){
    c.addError('max Contacts exceeded');
    }
    else{
    macc.get(conacc.get(c.id)).MaxCon__c=macc.get(conacc.get(c.id)).MaxCon__c+1;
    }
    }
    }