-
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 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.
-
This discussion was modified 8 years, 7 months ago by
Log In to reply.
Popular Salesforce Blogs
Case Study on Global Pharma Tendering Solution
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…
Salesforce Financial Services Cloud for Mortgage is Key to Simplify Lending Process
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…
Become a Marketing Expert with Salesforce Marketing Cloud Certification
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…
Popular Salesforce Videos
Creating a Rollup Using DLRS | Salesforce Tutorial
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…
How To Create Global Picklist In Salesforce
Learn how Global Picklist is created in Salesforce. The following points will be covered in this tutorial - 1. Introduction To Global Picklist Global picklist…
Survival Kit for Beginner Salesforce Developers - 1
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,…