-
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 6 years, 7 months ago by Shaharyar.
- This discussion was modified 6 years, 7 months ago by Forcetalks.
- This discussion was modified 6 years, 7 months ago by Forcetalks.
Log In to reply.
Popular Salesforce Blogs
How to use fieldset in Salesforce Visualforce pages?
Fieldset: A fieldset is a grouping of fields. When a fieldset is added to a Visualforce page, developers can loop over its fields and render…
Enhance Your Fundraising Efforts by Integrating Your Organization's System With Salesforce
As per Salesforce.org's Nonprofit Trends Report (3rd Edition), the pandemic forced 71 percent of organizations polled to speed up their transition to digital programs or…
Useful Salesforce Steps and Instructions - Part 2
Hello All, Welcome to another part of Useful Salesforce Steps and Instructions. The provided below steps will be very useful for you while working with…
Popular Salesforce Videos
PASS Salesforce Certified Administrator Exam on the FIRST Attempt
Want to pass the Salesforce Certified Administrator Exam on the FIRST Attempt? Then this video is for you. Here are the steps: STEP 1 :…
Revenue Sharing with Salesforce | Unlocking Profit Potential | Live Stream Highlights
This extract from our live webinar uncovered how this powerful platform can revolutionize your revenue generation and maximize your profits. Discover the untapped potential of…
Salesforce Data Management - Data Loader - Insert, Update, Upsert, Export, Delete
Use the Data Loader wizards to add, modify, or delete records. The upsert wizard combines inserting and updating a record. If a record in your…