Toggle Side Panel

  • Home
  • Articles
    • All Articles
    • Blogs
    • Videos
    • Infographics
  • Consultants
    • Salesforce Product Expertise
      • Top Salesforce ConsultantsTop Salesforce Consultants
      • Marketing Cloud ConsultantsMarketing Cloud Consultants
      • Service Cloud ConsultantsService Cloud Consultants
      • Experience Cloud ConsultantsExperience Cloud Consultants
      • Analytics Cloud ConsultantsAnalytics Cloud Consultants
    • Salesforce Industry Expertise
      • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
      • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
      • Health Cloud ConsultantsHealth Cloud Consultants
      • Commerce Cloud ConsultantsCommerce Cloud Consultants
      • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
    • Salesforce Experts by Location
      • USATop Salesforce Consultants in USA
      • IndiaTop Salesforce Consultants in India
      • AustraliaTop Salesforce Consultants in Australia
      • United KingdomTop Salesforce Consultants in UK
      • CanadaTop Salesforce Consultants in Canada
  • Webinars
  • Contact Us
  • Discussions
More options
    Sign in Sign up
    • Home
    • Articles
      • All Articles
      • Blogs
      • Videos
      • Infographics
    • Consultants
      • Salesforce Product Expertise
        • Top Salesforce ConsultantsTop Salesforce Consultants
        • Marketing Cloud ConsultantsMarketing Cloud Consultants
        • Service Cloud ConsultantsService Cloud Consultants
        • Experience Cloud ConsultantsExperience Cloud Consultants
        • Analytics Cloud ConsultantsAnalytics Cloud Consultants
      • Salesforce Industry Expertise
        • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
        • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
        • Health Cloud ConsultantsHealth Cloud Consultants
        • Commerce Cloud ConsultantsCommerce Cloud Consultants
        • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
      • Salesforce Experts by Location
        • USATop Salesforce Consultants in USA
        • IndiaTop Salesforce Consultants in India
        • AustraliaTop Salesforce Consultants in Australia
        • United KingdomTop Salesforce Consultants in UK
        • CanadaTop Salesforce Consultants in Canada
    • Webinars
    • Contact Us
    • Discussions
    Close search

    Activity › Forums › Salesforce® Discussions › How to create unique contacts in Salesforce depending on the value of number of location field in account object?

    Tagged: Account Object, Contact, Contact Record, Fields in Salesforce, Location Field, Salesforce Trigger, Update

    • Salesforce® Discussions

      How to create unique contacts in Salesforce depending on the value of number of location field in account object?

      Posted by Shaharyar on August 16, 2017 at 6:29 AM

      I have created a trigger on account object and want to insert or update number of contacts depending on the value of number of location field in Account Object.

      While using List<Contacts> it’s working and gives list of contacts with same name.

      But i want to use Set<Contact> to get unique contacts????

       

      #trigger

      trigger numberOfLocations on Account (after insert, After Update) {

      Set<contact> listContact = new Set<contact>();//When I replace Set<Contact> With List<Contact> it’s working and gives the list of contacts which is not unique.
      map<id,decimal> mapAcc=new map<id,decimal>();
      for(Account acc:trigger.new){
      mapAcc.put(acc.id,acc.NumberofLocations__c);
      }
      if(mapAcc.size()>0 && mapAcc!=null){
      for(Id accId:mapAcc.keyset()){
      for(integer i=0;i<mapAcc.get(accId);i++){
      contact newContact=new contact(AccountId=accId,LastName=’MyContact’);// I don’t want to use ‘MyContact’+i bcz i want to check the functionality of Set<Contact>
      listContact.add(newContact);
      }
      }
      }
      if(listContact.size()>0 && listContact!=null)
      upsert listContact;
      }

       

      #error

      DML requires SObject or SObject list type: Set<Contact>

      • This discussion was modified 8 years, 9 months ago by  Shaharyar.
      • This discussion was modified 8 years, 9 months ago by  Forcetalks.
      • This discussion was modified 8 years, 9 months ago by  Forcetalks.
      Satya replied 8 years, 4 months ago 4 Members · 3 Replies
      • Account Object
      • Contact
      • Contact Record
      • Fields in Salesforce
      • Location Field
      • Salesforce Trigger
      • Update
    • 3 Replies
    • shariq

      Member
      August 16, 2017 at 3:03 PM

      Hi Shaharyar,

      You can’t use Set of Contacts because contact’s records may have same value for LastName field.

      For Example :-

      Set <Contact> setCon = new Set<Contact>();

      Contact con = new Contact(LastName = ‘test’);

      Contact con1 = new Contact(LastName = ‘test’);

      insert con;      insert con1;

      setCon.add(con);    setCon.add(con1);

       

      ‘setCon’ Set will only contain contact ‘con’ as LastName field is same for both sobjects.

      And for DML Operation You can use only sobjects or list of sobjects.

      Hope this helps.

    • [adinserter block='9']
    • Shubham

      Member
      August 17, 2017 at 7:26 AM

      Hello Shaharyar

      Try this,

      trigger numberOfLocations on Account (After Update) {

      Set<contact> setContact = new Set<contact>();
      map<id,decimal> mapAcc=new map<id,decimal>();
      List<Contact> listContact = new List<Contact>();
      for(Account acc:trigger.new){
      mapAcc.put(acc.id,acc.NumberofLocations__c);
      }
      if(mapAcc.size()>0 && mapAcc!=null){
      for(Id accId:mapAcc.keyset()){
      for(integer i=0;i<mapAcc.get(accId);i++){
      contact newContact=new contact(AccountId=accId,LastName=’MyContact’);
      setContact.add(newContact);
      }
      }
      }
      listContact.addAll(setContact);
      insert listContact;
      System.debug(‘Contact = ‘ + listContact);

      }

    • Satya

      Member
      January 9, 2018 at 9:03 AM

      Hi Shaharyar,
      The Set class is not an iterable object type, and cannot participate in several common functions yet, including DML operations. Convert the Set to a List, instead.

    Log In to reply.

    • Public
    • All Members
    • My Connections
    • Only Me
    • Public
    • All Members
    • My Connections
    • Only Me
    • Public
    • All Members
    • My Connections
    • Only Me
    Footer Forcetalks logo

    support@forcetalks.com

    • twitterx

    Quick Links

    Advertise with Us

    Salesforce® Articles

    Dreamforce 2023

    Top Salesforce® Bloggers 2023

    Top Salesforce Consultants

    Get Listed

    Company

    Contact Us

    About Us

    Privacy Policy

    Terms & Conditions

    InsightHub

    Salesforce Blogs

    Salesforce Videos

    Salesforce Groups

    Salesforce Jobs

    © 2026 - Forcetalks ● All Rights Reserved

    Salesforce® is a trademark of Salesforce® Inc. No claim is made to the exclusive right to use “Salesforce”. Any services offered within the Forcetalks website/app are not sponsored or endorsed by Salesforce®.

    Try AuditMyCRM - It is a Salesforce CRM Audit tool which comprehensively scans your Salesforce org and gives you the list of errors or warnings you need to take care of.
    We use cookies to enhance your browsing experience. Please see our privacy policy if you'd like more information on our use of cookies.