Create Contact records based on Create N Contacts field on the Account record

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ก.ย. 2024
  • In this video, we will be showing you how to use Apex Triggers to create contact records based on the "Create N Contacts" field on the Account record.
    This is a powerful tool that allows you to automate the process of creating multiple contact records at once, saving you time and effort. By using Apex Triggers, you can easily set up a trigger that will automatically create contact records based on the number entered in the "Create N Contacts" field.
    If you want to continue learning Salesforce (for free), Subscribe Salesforce Makes Sense here
    / @salesforcemakessense
    To ensure a smooth learning experience,
    I have curated playlists that will help you prepare for interviews, train on specific domains and boost your Salesforce journey.
    The Complete Administrator Course
    • Learn Salesforce Admin...
    30 Scenario Based Questions:
    • 30 Salesforce Scenario...
    Learn Salesforce in Bytes - Short videos on Salesforce features
    • Salesforce Bytes
    Learn Salesforce With Me - Topic based Explanatory Videos
    • Learn Salesforce with Me
    65 Salesforce Interview Questions:
    • 65 Interview Questions...
    100 Salesforce Interview Questions:
    • 100 Salesforce Intervi...
    If you would like to acknowledge my efforts and want me to continue training and mentoring, you can support me here.
    www.buymeacoff...
    For any kind of questions, training & mentorship, queries & concerns,
    Feel free to drop a mail at
    salesforcemakessense@gmail.com
    #apextriggers #triggers #salesforcedevelopment
    That’s all from this video. See you in the next video. Till then, breathe long and keep learning :)

ความคิดเห็น • 32

  • @sagarr7940
    @sagarr7940 11 หลายเดือนก่อน +1

    Loving the Trigger playlist @Salesforce Makes Sense

  • @sushmast9906
    @sushmast9906 ปีที่แล้ว +1

    Amazing Explaination...!

    • @salesforcemakessense
      @salesforcemakessense  ปีที่แล้ว +1

      Glad it was helpful

    • @sushmast9906
      @sushmast9906 ปีที่แล้ว +3

      @@salesforcemakessense
      If I delete one record out of 4..the number should update to 3 in account object
      how to write a trigger for this one
      Pls share the code

  • @p_Arm6119
    @p_Arm6119 5 วันที่ผ่านมา +1

    What about CPU time out limit If you enter 10000 record ?

    • @salesforcemakessense
      @salesforcemakessense  5 วันที่ผ่านมา

      @@p_Arm6119 in such scenarios please go woth batch apex instead when you know the volume is high

  • @parveenparveen9384
    @parveenparveen9384 ปีที่แล้ว +1

    Thank you so much for these examples and clear explanation. I have a doubt in this example. Don't you think we have to do update DML operation within for loop. it will not work, if there are multiple accounts. As you tested, for single account we didn't notice the bug. after executing the for loop, conList holds last account's contact list. Please correct me if my understanding is wrong.

    • @salesforcemakessense
      @salesforcemakessense  ปีที่แล้ว +1

      Parveen, a DML inside for loop is never suggested. As it might end up in governor limit exception. In cases where you want to work with more than one data, you use Maps and Lists and that solves the problem.

    • @parveenparveen9384
      @parveenparveen9384 ปีที่แล้ว +1

      @@salesforcemakessense , Thank you so much for the quick reply. I agree, I will try with Maps. Did make video on Governor Limit?

    • @salesforcemakessense
      @salesforcemakessense  ปีที่แล้ว +1

      @@parveenparveen9384 Sure, will make one soon! Glad the videos are helpful :)

  • @SatishYadav-tv2qn
    @SatishYadav-tv2qn ปีที่แล้ว +1

    can you do this question using map single for loop

    • @salesforcemakessense
      @salesforcemakessense  ปีที่แล้ว +1

      The inner for loop is just for the number of contacts that need to be created. So, you will need two loops in this case. So, its okay to use nested loops. It won't be a problem.

  • @vaibhavshyamkuwar4154
    @vaibhavshyamkuwar4154 6 หลายเดือนก่อน +1

    Make video for update and delete also sir

    • @salesforcemakessense
      @salesforcemakessense  6 หลายเดือนก่อน

      Hi Vaibhav, take a look at the entire playlist. It has 30 use cases. Once you are done, let me know if there’s anything more needed 🙂

    • @vaibhavshyamkuwar4154
      @vaibhavshyamkuwar4154 6 หลายเดือนก่อน

      @@salesforcemakessense sure!!

  • @divyasreemareedu7105
    @divyasreemareedu7105 ปีที่แล้ว +1

    can the code be written without nested for loops ? Please help

    • @salesforcemakessense
      @salesforcemakessense  ปีที่แล้ว

      The inner for loop is just for the number of contacts that need to be created. So, you will need two loops in this case. So, its okay to use nested loops. It won't be a problem.

  • @samikshadangre7726
    @samikshadangre7726 ปีที่แล้ว +1

    Hi, I am trying it for after update scenario but its creating N contacts on already created contact. If anyone knows, please tell

    • @salesforcemakessense
      @salesforcemakessense  11 หลายเดือนก่อน

      You must have associated them to the contactId instead of accountId

  • @sourabhadekar5259
    @sourabhadekar5259 4 หลายเดือนก่อน +1

    But nested for lop is not best practice right?

    • @salesforcemakessense
      @salesforcemakessense  4 หลายเดือนก่อน

      100% Correct but this use case is not just for best practices.
      I will encourage you try writing this using collection and a single for loop and continue watching the playlist for more use cases 🙃

  • @ravigrover1923
    @ravigrover1923 ปีที่แล้ว

    Great Example Bro, Thanks for sharing this amazing example. I am not able to think of the logic for the update Create_N_Contacts__c use case. Please help if possible.

    • @ravigrover1923
      @ravigrover1923 ปีที่แล้ว +3

      HI Himanshu,
      I tried to write the Update scenario but not sure if this is the right way. It is working fine in both the cases when Create_N_Contacts__c is increased from 2 to 10 and in decrease from 10 to 2 for example.
      I am first checking if Create_N_Contacts__c is getting modified as well as its not null in the Trigger.new. Then getting a list of all contacts for that account in contact List "conToDelete" from soql. Then delete them to get rid of them entirely. Then do the same thing again as you explained.
      Here is the code:
      public static void handleActivitiesAfterUpdate(List newAccounts, Map oldAccMap){
      List conToInsert = new List();
      for (Account accRecord : newAccounts){
      if (accRecord.Create_N_Contacts__c != oldAccMap.get(accRecord.Id).Create_N_Contacts__c && accRecord.Create_N_Contacts__c != null){
      List conToDelete = [SELECT id, Firstname, Lastname, AccountId
      FROM Contact
      where AccountId = :accRecord.Id];
      delete conToDelete;
      for (Integer i = 0; i < accRecord.Create_N_Contacts__c; i++){
      Contact con = new Contact();
      con.Lastname = accRecord.Name + i;
      con.AccountId = accRecord.Id;
      conToInsert.add(con);
      }
      }
      }
      if (!conToInsert.isEmpty()){
      insert conToInsert;
      }

    • @salesforcemakessense
      @salesforcemakessense  9 หลายเดือนก่อน

      Sounds about right to me! :)

  • @goku6986
    @goku6986 6 หลายเดือนก่อน +1

    Is it possible to do this, without using Nested For loop?

    • @salesforcemakessense
      @salesforcemakessense  6 หลายเดือนก่อน

      I will suggest trying it using map instead of for loop. Take a look at the playlist and once you are confortable trying on your own, give it a try and let me know how does it go 🙂

    • @goku6986
      @goku6986 6 หลายเดือนก่อน

      @@salesforcemakessense I bought and watched you Udemy playlist few days back then today I came to know its available in youtube for free😂.By the way videos are really good.

  • @gauravjoshi3879
    @gauravjoshi3879 10 หลายเดือนก่อน +1

    I am not able to use system.debug in apex Triggers? Plzz ans

    • @salesforcemakessense
      @salesforcemakessense  10 หลายเดือนก่อน

      You should be able to. Can you show the error you are getting Gaurav?