-
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, 5 months ago by
Shaharyar.
-
This discussion was modified 8 years, 5 months ago by
Forcetalks.
-
This discussion was modified 8 years, 5 months ago by
Forcetalks.
-
This discussion was modified 8 years, 5 months ago by
Log In to reply.
Popular Salesforce Blogs
Strategies For Nonprofits To Navigate Their Way Through 2021 | Salesforce Guide
2020 will be engraved and etched in everyone's minds and hearts for the generations and centuries to come for the surprising, uncertain, and unprecedented turn…
SSO Implementation between two Salesforce Orgs in Lightning and Classic view
We can implement SSO for both Lighting and Classic view with the help of SAML. There is no difference between security protocol for both. Requirement:…
Top Reasons to Learn Salesforce
Salesforce is the number one CRM (customer relationship management) and cloud-based platform that enables companies to manage, organize and track their internal information effectively and…
Popular Salesforce Videos
Real-time Scenario: Custom Rollup Summaries Using Triggers | Salesforce Tutorial
Watch this video to learn all about Custom Rollup Summaries Using Triggers using the real-time scenarios. If you have any doubts or concerns do let…
A Year of Innovation and Growth for Kizzy Consulting - Top Salesforce Partner!
2023 was an amazing year for Kizzy Consulting - Top Salesforce Partner! We achieved the status of Salesforce Ridge Partner, completed numerous Salesforce certifications, and…
Salesforce Introduction | Salesforce Administrator Tutorial for Beginners
Salesforce Administrators work with stakeholders to define system requirements and customize the platform. To put it simply, they enable users to get the most out…