+ Start a Discussion
DIVAKAR BABU 15DIVAKAR 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;
}
Steven NsubugaSteven Nsubuga
Use this
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, Contact_Area__c, contact_country__c  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;
}

 
Abdul KhatriAbdul Khatri
Hi DIVAKAR

I see some issues in your code apart from
variable doesnt exist .variable doesnt exist Contact_Area__c, contact_country__c

The following code will fail after Insert Trigger because oldMap is not available on Insert 
User-added image


Here is the functional code. It also takes care of null pointers. The code is simple, compact with less variables and no SOQL
trigger ConnectA on Contact (after insert, after update) {

    List<Account> accountListToUpdate = new List<Account>();
    
    for(Contact con : Trigger.new ) {
       
        if(contact.AccountId == null) continue;        
        
        if(Trigger.isInsert ||
           (Trigger.isUpdate && (con.contact_country__c != contact_country__c || 
                                con.Contact_Area__c != Trigger.oldMap.get(con.Id).Contact_Area__c)))                                
        {       
            Account accountToUpdate = new Account (Id = con.AccountId);
            accountToUpdate.contact_country__c = con.contact_country__c;
            accountToUpdate.Contact_Area__c = con.Contact_Area__c;
            
            accountListToUpdate.add(accountToUpdate);
        }
        
    }
    
    if(!accountListToUpdate.isEmpty()) update accountListToUpdate;
}


 
Abdul KhatriAbdul Khatri
Please confim if the solution was helpful and mark the answer best accordingly. Thanks