-
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

What is Pardot? What is Engagement History in Salesforce in 2023?
Pardot is a software as a service (SaaS) marketing automation platform by SalesForce offering email automation, targeted email campaigns, and lead management for B2B sales…

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:…

How to Hire Salesforce Developers During the 2023 Economic Downturn?
The pressure of economic recession is lurking around and spending on technology is expected to take a downturn. However, when it comes to the Customer…
Popular Salesforce Videos
About Queueable Apex | Asynchronous Apex in Salesforce | Learn Salesforce Development
Queueable apex is an asynchronous apex method. It's similar to the @future method. By using this queueable interface, we can process an Apex that runs…
What is Approval Process in Salesforce?
Automated process that approves records for the organization and it can be defined with a sequence of steps. Watch the video to learn and if…
1st Year as a Salesforce Business Analyst
If you are interested in a Salesforce Career, check out Talent Stacker. This is what I used when I first started and ended up joining…