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, 1 month 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

    Visualforce Basics

    Visualforce Basics | Salesforce Learning Guide

    Blog in Visualforce

    Visualforce is a powerful tool and a stimulating framework allowing developers to explain the interface component and to create sophisticated custom interfaces which will be…

    Components, Controller, Controllers and Extensions, Custom Controller, Custom Interfaces
    Narendra Sa Jan 14, 2022
    4,438  Views

    Unlock Your Salesforce Potential With Expert Customization Services

    Blog in Salesforce customization

    Every business runs differently. Your CRM should too. Standard Salesforce solutions often leave gaps. Custom configurations close those gaps and drive results. Why Salesforce Customization…

    Salesforce Customization
    TechForce Nov 24, 2025
    185  Views

    Discover How Salesforce Commerce Cloud Can Transform Your Business

    Blog in Salesforce Cloud Platform

    Salesforce Commerce Cloud is revolutionizing the way businesses interact with their customers. It provides a powerful, cloud-based platform that enables companies to create engaging digital…

    Automated Marketing Campaigns, Automated Order Management, B2B, B2C, Behavior
    Daisy Jun 7, 2023
    1,987  Views

    Popular Salesforce Videos

    Handle Events in Lightning Web Components | Salesforce Tutorial

    Handle Events in Lightning Web Components | Salesforce Tutorial

    Video in Lightning, Salesforce Training

    Watch this video and learn about the following: Create an app that includes multiple components. Describe the file structure of a complex component. Handle events.

    Salesforce Training, Salesforce Tutorial, Salesforce Video, Lightning Web Components, Salesforce Learning
    Rupal Kakkar Jun 24, 2021
    2,121  Views
    Introduction to Salesforce Marketing Cloud

    Introduction to Salesforce Marketing Cloud

    Video in Marketing, Salesforce Cloud Platform

    Hello Everyone, In this video, by Amit Singh, we will talk about the basic introduction of the Marketing Cloud in Salesforce. Watch the video and…

    Salesforce Training, Salesforce Marketing Cloud, Salesforce Cloud Platform, Salesforce Video, Salesforce Learning
    Jogender Jun 25, 2021
    1,299  Views
    Salesforce Winter 21 Release - Sneak Peek

    Salesforce Winter 21 Release - Sneak Peek

    Video in Salesforce Stories

    The Winter ’21 release delivers innovations across the Salesforce Customer 360 platform to help connect you to your customers, from anywhere. Fancy table updates, live…

    Salesforce Video, Salesforce Customers, Salesforce Release Notes, Salesforce Winter 21 Release, Winter 21
    Rupal Kakkar Aug 26, 2020
    1,746  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.