You need to sign in to do that
Don't have an account?
DIVAKAR BABU 15
i am getting error variable doesnt exist .variable doesnt exist Contact_Area__c, contact_country__c
parent object: account
fields : contact country - Picklist values: India, Africa, America
child object : contact
field : Contact Area - Picklist values: Karataka, Andhra Pradesh, Kerala, South Africa, Nigeria, Kenya, California, San Fransisco, Texas
when contact is inserted / updated, based the contact's Contact Area field the parents contact Country should change with respected country. wirte a trigger for insert, Update. create required fields.
trigger ConnectA on Contact (after insert,after update) {
Map<Id,Contact> AccID = New Map<Id,Contact>();
Map<Id,Contact> oldCOn = trigger.oldMap ;
for(Contact con : Trigger.new){
if( (con.contact_country__c!=oldCOn.get(con.Id).contact_country__c) ||
(con.Contact_Area__c!=oldCOn.get(con.Id).Contact_Area__c) ){
AccID.add(con.AccountId);
}
}
List<Account> accList = [SELECT Name, BillingStreet FROM Account WHERE id in :AccID.keySet()];
for(Account a :accList){
Contact c = AccID.get(a.Id) ;
a.Contact_Area__c = c.Contact_Area__c ;
a.contact_country__c = c.contact_country__c ;
}
update accList;
}
fields : contact country - Picklist values: India, Africa, America
child object : contact
field : Contact Area - Picklist values: Karataka, Andhra Pradesh, Kerala, South Africa, Nigeria, Kenya, California, San Fransisco, Texas
when contact is inserted / updated, based the contact's Contact Area field the parents contact Country should change with respected country. wirte a trigger for insert, Update. create required fields.
trigger ConnectA on Contact (after insert,after update) {
Map<Id,Contact> AccID = New Map<Id,Contact>();
Map<Id,Contact> oldCOn = trigger.oldMap ;
for(Contact con : Trigger.new){
if( (con.contact_country__c!=oldCOn.get(con.Id).contact_country__c) ||
(con.Contact_Area__c!=oldCOn.get(con.Id).Contact_Area__c) ){
AccID.add(con.AccountId);
}
}
List<Account> accList = [SELECT Name, BillingStreet FROM Account WHERE id in :AccID.keySet()];
for(Account a :accList){
Contact c = AccID.get(a.Id) ;
a.Contact_Area__c = c.Contact_Area__c ;
a.contact_country__c = c.contact_country__c ;
}
update accList;
}
I see some issues in your code apart from
The following code will fail after Insert Trigger because oldMap is not available on Insert
Here is the functional code. It also takes care of null pointers. The code is simple, compact with less variables and no SOQL