-
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 7 years, 6 months ago by
Shaharyar.
-
This discussion was modified 7 years, 6 months ago by
Forcetalks.
-
This discussion was modified 7 years, 6 months ago by
Forcetalks.
-
This discussion was modified 7 years, 6 months ago by
Log In to reply.
Popular Salesforce Blogs

What Is Salesforce On-Demand Administrator and How Your Organization Can Benefit From It?
There are plenty of things to love about Salesforce. First of all, it has an easy-to-use interface with plenty of features and extensions. You can…

Salesforce Application Development Client On Boarding Features For The Banking Industry
Banking industry has been an integrated part of everyone's life and the process involved to execute Banking operations has been quite slow, until the technological integration for its…

Salesforce CPQ: Unleashing the Power of Configure, Price, Quote
In today’s fast-paced business landscape, optimizing the sales process is crucial for staying competitive and driving revenue growth. Salesforce CPQ (Configure, Price, Quote) emerges as…
Popular Salesforce Videos
Security Best Practices for Building Lightning Components
Lightning Component is a framework to build Salesforce applications faster. If you are using Salesforce1, you already experiencing the Lightning. It is based on an…
How To Become A Salesforce Administrator | Salesforce Administrator Training
This "How to become a Salesforce Administrator" video will help you understand who is a Salesforce Administrator and what are their roles and responsibilities. It…
How to Learn Salesforce in easy step by step and get certified
From Salesforce instructor, Become a Salesforce Expert. Learn Salesforce step by step and get certified easily.