-
Why do I'm getting an error on inserting and updating the address in contact mailing address?
trigger ContactMailAddr on Contact (after insert, after update) { // first you create a list to store the Account ids List<Id> accountToSearch = new List<Id>(); // get them from the contacts for (Contact contact : Trigger.new) { accountToSearch.add(contact.AccountId); } // query in the db List<Account> accounts = [SELECT Id, BillingStreet, BillingCity, BillingState, BillingCountry, BillingPostalCode FROM Account WHERE Id IN :accountToSearch]; list<contact> contacts =new list<contact>([select id,AccountId,MailingCity,MailingState,MailingStreet,MailingCountry from contact where accountid in :accountToSearch]); // iterate over the accounts and contacts for (Account account : accounts) { for (Contact contact : Trigger.new) { // if a contact is set to the account then if (contact.AccountId == account.Id && contact.AccountId != null) { // copy the contact's address to the account's fields contact.MailingStreet = account.BillingStreet; contact.MailingCountry = account.BillingCountry; contact.MailingState = account.BillingState; contact.MailingCity = account.BillingCity; contact.MailingPostalCode = account.BillingPostalCode; } } } // finally update the contacts records update contacts; }
I'm getting an error on inserting and updating the address?
Log In to reply.
Popular Salesforce Blogs
How Can Customizing Salesforce Help Non-Profit Organizations?
Salesforce can support every industry sector, helping companies earn more profits and achieve business growth. But what about the non-profit sector? The good thing is…
Power of Asynchronous Apex: Benefits and Insights
Introduction Salesforce is renowned for its ability to provide businesses with comprehensive customer relationship management (CRM) solutions. To supercharge its functionality, Salesforce introduced Asynchronous Apex,…
Top 10 Salesforce Admin Tips in 2018
No doubt, you might be the best Admin, but still, you will always look for tips to maintain the spark of being the best. When…
Popular Salesforce Videos
Salesforce Development Course for Intermediate Developers
If you want to become a Salesforce developer in 2021 then watch this video, here you'll learn about some of the best Salesforce Development Course…
Salesforce Service Cloud Telephony
Natterbox CTI and Telephony inside Salesforce ServiceCloud. Transform your telephony experience and empower your agents with fast and personalised support.
COVID 19 Tracker in Salesforce Lightning Aura Component
In this video, we are developing a lightning Component to track the current pandemic COVID19 Stats Tracker which shows the current situation of all countries…
Popular Salesforce Infographics
Benefits of Salesforce Pardot Implementation
Cyntexa offers the best Salesforce Pardot Implementation services so that you can offer the best customer services. Our certified experts will provide Salesforce Pardot Implementation…
In marketing, Can We Achieve What We Can’t Measure? | Salesforce
Metrics in marketing are always evolving and we can all learn more about them from our peers. Let’s look at where marketers are spending their…
Future of Salesforce CPQ
Salesforce CPQ is a highly dependable tool that provides an accurate pricing strategy by configuring, pricing, and quoting the products of each company based on…