-
How to create unique contacts in Salesforce depending on the value of number of location field in account object?
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 9 years ago by
Shaharyar.
-
This discussion was modified 9 years ago by
Forcetalks.
-
This discussion was modified 9 years ago by
Forcetalks.
-
This discussion was modified 9 years ago by
Log In to reply.
Popular Salesforce Blogs
All You Need to Know About Apex Programming
Apex Programming Apex is the object-oriented programming language used by the Salesforce platform. It enables developers to execute transactions and control statements on servers associated…
A Beginner’s Guide to Salesforce CRM Managed Services: What You Need to Know
Businesses across the world are using Salesforce CRM as a strategic tool for their growth. The operations of the CRM are maintained by an IT…
5 Ways to Improve the Patient Journey with Salesforce
Healthcare has faced unexpected days, years, and months during the recent decade. Patients have more choices about their care, and they’re empowered with information on…
Popular Salesforce Videos
Creating Datasets - Tableau CRM | Salesforce Tutorial
Recipes provide an easy way to update, combine and transform your sources into the datasets you'll use to drive your dashboards. You'll see how to…
Salesforce Einstein Keynote: Meet Your Smart CRM Assistant
Salesforce Einstein is your smart CRM assistant, making your employees smarter and your customers happier. With Einstein, everyone has an AI-powered assistant to help them…
Getting to Know Einstein Analytics
Einstein Analytics is an app used to visualize the activity occurring in your Salesforce environment. Whether you use Salesforce for Sales, Marketing, or Service, this…