You need to sign in to do that
Don't have an account?

account city field update trigger
Hi friends
i want a trigger code for this
When a contact is made primary(1) the city of the contact should be updated in the Account’s city(3) field.
i want a trigger code for this
When a contact is made primary(1) the city of the contact should be updated in the Account’s city(3) field.
for( Contact con : trigger.new){
if(con.city != null){
Account acc = [SELECT account_city from account where id =: con.AccountId];
acc.Account_city = con.city;
insert acc;
}
}
All Answers
for( Contact con : trigger.new){
if(con.city != null){
Account acc = [SELECT account_city from account where id =: con.AccountId];
acc.Account_city = con.city;
insert acc;
}
}
actully this trigger is in account ....contact is related list of account...
and it should copy the city name whose primary value is true..
pls look into this ... the code u provide is also good..but not matching my requirements
string conCity= SELECT Contact.city FROM contacts WHERE IsPrimary = TRUE and Id = : con.Id].city;
add this line before if condition
trigger CityonContact on Contact (After insert) {
List<Contact> con = new List<Contact>();
List<Account> acct = new List<Account>();
for (Contact c : Trigger.new)
{
// Contact cont = new Contact(id = c.Id,Lastname = c.LastName,MailingCity = c.MailingCity, Primary_Contact__C = c.Primary_Contact__c);
con.add(c);
}
for (contact c : con)
{
if ((c.Primary_Contact__c==true) && (c.MailingCity!=null))
{
Account acc = [Select Id, BillingCity from Account where Id =:c.AccountId];
acc.BillingCity = c.MailingCity;
acct.add(acc);
}
}
update acct;
}