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

    Salesforce for Nonprofits

    Salesforce for Nonprofits – Don’t miss out on these amazing benefits

    Blog in Others

    Giving back has been part of the DNA of Salesforce. Since inception, the Global tech giant has been on a mission to empower Nonprofits through…

    AppExchange Apps, CRM Solutions, CRM Technology, Customer Retention, Donor Retention
    DemandBlue Oct 24, 2018
    4,311  Views
    Salesforce Security using CRUD, FLS, and sharing rules

    Salesforce Security using CRUD , FLS & Sharing Rules | Salesforce Guide

    Blog in Salesforce

    Salesforce is a cloud-based Customer Relationship Management (CRM) platform that allows businesses to manage their sales, marketing, and customer service activities in a more efficient…

    Create, Custom Picklist Field, Delete, Field Level Security, Field Values
    Mohit Kumar Mar 21, 2023
    2,570  Views

    The Salesforce Implementation Process: What Is It?

    Blog in Salesforce Implementation

    Salesforce implementation is the process of integrating and customizing the Salesforce platform to meet the specific needs of a business or organization. Salesforce is a…

    Benefits, Business, Campaign Tracking, Case Management, Centralized Platform
    Chris Jul 4, 2023
    1,754  Views

    Popular Salesforce Videos

    Introduction to Salesforce Orchestrator

    Introduction to Salesforce Orchestrator

    Video in Salesforce Training, Salesforce Workflow

    Flow Orchestrator was created so admins like you can quickly create sophisticated multi-user, multi-step automated business processes with clicks, not code. Experienced Salesforce Admins will…

    Salesforce Training, Salesforce Tutorial, salesforce, User Experience, Salesforce Admins
    Prafull Oct 19, 2021
    3,916  Views
    Solving the Fizz Buzz (SSJS) in Salesforce Marketing Cloud

    Solving the Fizz Buzz (SSJS) in Salesforce Marketing Cloud

    Video in Marketing

    Cameron Robert from Datarati uses 2 different approaches to solve the Fizz Buzz programming challenge using SSJS Salesforce Marketing Cloud. What you'll learn: » How…

    Salesforce Training, Salesforce Tutorial, Salesforce Marketing Cloud, Salesforce Video, Javascript
    Cameron Sep 5, 2025
    1,159  Views
    Marketing for Nonprofits Customer 360 Demo | Salesforce Video

    Marketing for Nonprofits Customer 360 Demo | Salesforce Video

    Video in Salesforce Products

    Growing your nonprofit requires building relationships with lifelong supporters. Marketing Cloud for Nonprofits is a purpose-built marketing solution designed to help smaller nonprofits quickly and…

    Salesforce Training, Salesforce Video, Salesforce Customer 360, Salesforce Learning, Marketing Cloud
    Abhishek Jun 3, 2021
    2,013  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.