-
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
Visualforce Basics | Salesforce Learning Guide
Visualforce is a powerful tool and a stimulating framework allowing developers to explain the interface component and to create sophisticated custom interfaces which will be…
Unlock Your Salesforce Potential With Expert Customization Services
Every business runs differently. Your CRM should too. Standard Salesforce solutions often leave gaps. Custom configurations close those gaps and drive results. Why Salesforce Customization…
Discover How Salesforce Commerce Cloud Can Transform Your Business
Salesforce Commerce Cloud is revolutionizing the way businesses interact with their customers. It provides a powerful, cloud-based platform that enables companies to create engaging digital…
Popular Salesforce Videos
Handle Events in Lightning Web Components | Salesforce Tutorial
Watch this video and learn about the following: Create an app that includes multiple components. Describe the file structure of a complex component. Handle events.
Introduction to Salesforce Marketing Cloud
Hello Everyone, In this video, by Amit Singh, we will talk about the basic introduction of the Marketing Cloud in Salesforce. Watch the video and…
Salesforce Winter 21 Release - Sneak Peek
The Winter ’21 release delivers innovations across the Salesforce Customer 360 platform to help connect you to your customers, from anywhere. Fancy table updates, live…