-
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 9 years, 2 months ago by
Shaharyar.
-
This discussion was modified 9 years, 2 months ago by
Forcetalks.
-
This discussion was modified 9 years, 2 months ago by
Forcetalks.
-
This discussion was modified 9 years, 2 months ago by
Log In to reply.
Popular Salesforce Blogs
Top 4 Considerations in Choosing your Salesforce CPQ Partner
Congratulations, you’ve made the decision to implement Salesforce CPQ! Now comes the challenging task of choosing the right Salesforce CPQ partner to help fulfill your…
Certified Salesforce Consulting Partner – A Game Changer for Businesses
In today’s digital world, businesses rely on Customer Relationship Management (CRM) platforms like Salesforce to streamline operations, enhance customer engagement, and drive sales growth. However,…
Salesforce Data Cloud: Features, Pricing, and How It Works
Businesses today collect customer information from CRMs, websites, marketing platforms, applications, databases, and other systems. The challenge is turning this fragmented information into a reliable,…
Popular Salesforce Videos
Winter '21 Release | Salesforce Tutorial Video
The Sandbox Preview window for Winter '21 begun on September 11, 2020. You must have an active sandbox on a preview instance before then to…
How to Focus on Report Results in Salesforce Console | Salesforce
When you work with reports in a fast-paced Salesforce Console environment, every extra click or focus change can slow you down. Now a streamlined console…
Salesforce and Slack | Meet the Slack-First Customer 360
Salesforce and Slack have joined forces to create the Slack-First Customer 360. Now it’s easier than ever to connect customers, employees, and partners with the…