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

    • 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

    Case Study on Global Pharma Tendering Solution

    Blog in Salesforce

    Enhancing tendering operations across multiple markets in the pharmaceutical industry through a unified solution within Salesforce. In the dynamic and competitive pharmaceutical industry, the efficient…

    Business Operations, Business Process, Centralization, Challenges, Client Background
    Triple Innovations Oct 20, 2023
    1,516  Views
    Salesforce Financial Services Cloud

    Salesforce Financial Services Cloud for Mortgage is Key to Simplify Lending Process

    Blog in Salesforce Cloud Platform

    Most borrowers find applying to the mortgage application process a time-consuming and tedious task with conventional financial institutions & lenders (who still use old manual…

    AppExchange Partners, Application Development, Application Process, Approval Process, Automated Approval Management
    SP Dec 20, 2021
    5,291  Views

    Become a Marketing Expert with Salesforce Marketing Cloud Certification

    Blog in Salesforce Products

    There is no wonder that Salesforce Marketing Cloud is a powerful marketing automation platform that enables businesses to engage with their customers on a more…

    Market Segmentations, Marketers, Marketers and Organizations, marketing, Marketing Campaigns
    Eden Feb 14, 2023
    1,687  Views

    Popular Salesforce Videos

    Creating a Rollup Using DLRS | Salesforce Tutorial

    Creating a Rollup Using DLRS | Salesforce Tutorial

    Video in Salesforce Training, Salesforce Admin

    In certain situations, an object does not support a roll-up, or the relationship is not permitted. Learn how to make it possible by using the…

    Salesforce Training, Salesforce Tutorial, Salesforce Video, Salesforce Learning, Salesforce Architect
    SkyPlanner Jul 16, 2021
    2,179  Views
    How To Create Global Picklist In Salesforce

    How To Create Global Picklist In Salesforce

    Video in Others, Salesforce Training

    Learn how Global Picklist is created in Salesforce. The following points will be covered in this tutorial - 1. Introduction To Global Picklist Global picklist…

    Salesforce Training, salesforce, Salesforce Objects, Salesforce Video, Salesforce Learning
    Algoworks Dec 10, 2020
    1,981  Views
    Survival Kit for Beginner Salesforce Developers - 1

    Survival Kit for Beginner Salesforce Developers - 1

    Video in Salesforce Training

    Are you a developer just starting out with Salesforce? Are you an admin looking to get into development? Join us to learn a few easy,…

    salesforce training videos for beginners, Salesforce Developer, Salesforce Beginner Tutorial, Salesforce Tutorial for Beginners, Salesforce Lightning
    Shiv Kumar Sharma Feb 8, 2018
    2,139  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.