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
  • Marketplace
  • Advertise With Us
  • 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
    • Marketplace
    • Advertise With Us
    • 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, 5 months ago by  Shaharyar.
      • This discussion was modified 8 years, 5 months ago by  Forcetalks.
      • This discussion was modified 8 years, 5 months ago by  Forcetalks.
      Satya replied 8 years 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.

    • 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

    application solution

    Popular Salesforce Blogs

    Strategies For Nonprofits To Navigate Their Way Through 2021 | Salesforce Guide

    Blog in Salesforce, Salesforce Products

    2020 will be engraved and etched in everyone's minds and hearts for the generations and centuries to come for the surprising, uncertain, and unprecedented turn…

    Big Data, communities, Company, COVID-19, Customers
    Apphienz Jun 1, 2021
    1,371  Views
    SSO Implementation of two Salesforce Orgs in Lightning and Classic view

    SSO Implementation between two Salesforce Orgs in Lightning and Classic view

    Blog in Salesforce Implementation, Salesforce Security

    We can implement SSO for both Lighting and Classic view with the help of SAML. There is no difference between security protocol for both. Requirement:…

    Classic View, Lightning View, Salesforce Classic, Salesforce Implementation, Salesforce Lightning
    Neha Mar 23, 2018
    9,769  Views

    Top Reasons to Learn Salesforce

    Blog in Salesforce Certifications

    Salesforce is the number one CRM (customer relationship management) and cloud-based platform that enables companies to manage, organize and track their internal information effectively and…

    Salesforce App Builder, Salesforce Certification, Salesforce Commerce Cloud, Salesforce Course, Salesforce Forums
    Simplilearn Jun 21, 2019
    4,828  Views

    Popular Salesforce Videos

    Real-time Scenario: Custom Rollup Summaries Using Triggers | Salesforce Tutorial

    Real-time Scenario: Custom Rollup Summaries Using Triggers | Salesforce Tutorial

    Video in Salesforce Apex

    Watch this video to learn all about Custom Rollup Summaries Using Triggers using the real-time scenarios. If you have any doubts or concerns do let…

    Salesforce Training, Salesforce Tutorial, Salesforce Apex, Salesforce Triggers, salesforce
    Rupal Kakkar Jul 8, 2022
    2,934  Views
    A Year of Innovation and Growth for Kizzy Consulting - Top Salesforce Partner!

    A Year of Innovation and Growth for Kizzy Consulting - Top Salesforce Partner!

    Video in Salesforce Stories

    2023 was an amazing year for Kizzy Consulting - Top Salesforce Partner! We achieved the status of Salesforce Ridge Partner, completed numerous Salesforce certifications, and…

    Salesforce Partner, Salesforce Consulting Company, salesforce, CRM, payment gateway
    Kizzy Consulting Jun 20, 2024
    646  Views
    Salesforce Introduction | Salesforce Administrator Tutorial for Beginners

    Salesforce Introduction | Salesforce Administrator Tutorial for Beginners

    Video in Salesforce Training

    Salesforce Administrators work with stakeholders to define system requirements and customize the platform. To put it simply, they enable users to get the most out…

    Salesforce Training, Salesforce Tutorial, Salesforce Administrator, Salesforce Admin, Salesforce Learning
    Pooja Dec 9, 2021
    2,965  Views
    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.