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

4 Best Salesforce Sales Cloud Features to Boost Revenue and Business
The newest enhancements to Salesforce's Sales Cloud and Revenue Cloud, which concentrate on revenue intelligence, sales enablement, and subscription organization, have been made public. At…

How Does Salesforce Genie Impact Your Business?
Salesforce Genie, a real-time platform, powers customer magic. Your teams are able to meet your customers where they are unlike ever before by harmonizing data…

Salesforce Marketing Cloud Personalization Overview
Suppose you've attracted leads to your website or app. Some of them converted into prospects. You carry on gathering data, but they keep unsubscribing or…
Popular Salesforce Videos
Salesforce Marketing Cloud Automation Studio
Automation Studio is a Marketing Cloud application used to execute multi-step marketing and data management activities on an immediate, triggered, or scheduled basis. Use Automation…
A Must-Have for Admins: Salesforce Profiles, Permission Sets, and Roles Explained
Keeping your data safe and organized is a must-have. Luckily, Salesforce provides all the tools to help administrators control who can see and do what.…
Salesforce Health Cloud – Creating Patient Relationships Not Records
Unfortunately, the gruesome truth facing the world today is the ever increasing pressure of diseases due to chronic disorders, pollution, growing age, and definitely increasing…