-
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, 3 months ago by Shaharyar.
- This discussion was modified 7 years, 3 months ago by Forcetalks.
- This discussion was modified 7 years, 3 months ago by Forcetalks.
Log In to reply.
Popular Salesforce Blogs
Files, Attachments And Notes Salesforce Lightning Components | Force.com
Download Notes & Attachments from Force.com Site of Lightning Component Create an email alert that will be sent to the Opportunity Owner. This email will…
Leverage the Power of Salesforce Marketing Cloud Account Engagement for Inbound Marketing
Inbound marketing is an effective strategy for producing excellent content that meets the demands of your target markets and fosters enduring relationships with customers. Because…
Salesforce Lightning Experience: Tools for Transitioning from Salesforce Classic to Lightning
Introduction January 7, 2020: The date when good news finally arrived from Salesforce to its users that the company is auto activating Salesforce Lighting Experience…
Popular Salesforce Videos
An Introduction to Salesforce org Nonprofit Cloud
Did you know that over 30,000 nonprofit organizations use Salesforce? Salesforce.org’s premier product, NPSP, helps nonprofits drive greater impact by connecting everything they do. Learn…
What is Lead Assignment rule in Salesforce and How to create it?
This video explains Lead Assignment rules in depth in an interesting way and fun way. This video is divided into 4 parts: - What is…