function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
tulasiram chtulasiram ch 

Initial term of field expression must be a concrete SObject: Boolean

I wrote a trigger for updating contact Phone, Title based on Account Ownership == 'Private' (picklist) and Phone, AccountNumber
if(Account Ownership == 'Private')
contact Phone = account phone
contact   Title = account  AccountNumber ...But i am getting error Can anyone explain me the Solution. Just Learning and Practicing based on someones scenarios ...Thank you

Code: trigger updateFieldsOnContact on Account (after insert, after update)
{
    set<id> setAccid = new set<id>();
    for(Account acc:trigger.new){
        if(acc.Ownership =='Private' && acc.AccountNumber !=  Null && acc.Phone !=Null){
            setAccid.add(acc.Id);
        }
    }
    if(setAccid.size()>0)
            {
                map<id, account> mapAccCons = new map<id, account>([select id, (select id, Phone, Title from Contacts) from Account where id in :setAccid]);
               list<Contact> cons = new List<Contact>();
                for(account acc:trigger.new)
                        {
                            if(acc.Ownership == 'Private' && mapAccCons.containsKey(acc.Id).Contacts)
                                {
                                List<Contact> newUpdatingCons = mapAccCons.get(acc.Id).Contacts;
                                for(Contact cont : newUpdatingCons){
                                    cont.Phone = acc.Phone;
                                    cont.Title = acc.AccountNumber;
                                    cons.add(cont);
                                    
                                }
                                
                         }
                   
               }
                if(cons.size()>0){
                    update cons;
                }
        
    }
}
 
new sivanew siva
You can below code as a solution
trigger updateFieldsOnContact on Account (after update)
{
    set<id> setAccid = new set<id>();
    for(Account acc:trigger.new){
        if(acc.Ownership =='Private' && acc.AccountNumber !=  Null && acc.Phone !=Null ){
            setAccid.add(acc.Id);
        }
    }
    if(setAccid.size()>0)
    {
        map<id, account> mapAccCons = new map<id, account>([select id,phone,accountnumber from Account where id in :setAccid]);
        list<Contact> cons = new List<Contact>();
    
         for(contact cont:[select id,lastname, phone, title,accountid from contact where accountid IN: mapAccCons.keyset() ]){
           account acc =mapAccCons.get(cont.accountid);
           //Add Fields you want to update here, which have been in the query above.
           cont.Phone = acc.Phone;
            cons.add(cont);
        }
        {
        
        if (cons.size()>0)
        update cons;
        } 
      
        
    }
}
DeveloperSudDeveloperSud
Hi Tulasiram,

I have some confusion on the 'after insert' activity for the account object.Are we creating a new contact when a new account in created?The thing is not clear to me how will you associate a contact when a new account is created.In my trigger I have created a new contact when an account is created and updated the phone and title filed of contact accordingly.
You can refer the below code.Please post if you have any more questions otherwise mark this as solved .
 
trigger AccountTrigger1 on Account (after insert,after update) {
    
   
    List<contact> conToUpdate=new List<contact>();
    List<Contact> conToInsert=new List<Contact>();
    
    set<id> aid=new set<id>();
    List<contact> oldContacts= new List<contact>();
    for(Account a1: trigger.new){
        aid.add(a1.id);
        List<contact> newcon=[select name,id,phone,title from contact where accountid=:a1.id ];
        oldContacts.addAll(newcon);
    }
    
    if(trigger.isInsert){
        for(Account a2:trigger.new){
            if(a2.ownership__c=='Private'){
                //creating a new contact with same name as account 
                //with the phone na and title same as account's phone and no.
                contact c2=new contact();
                c2.LastName=a2.name;
                c2.Phone=a2.phone;
                c2.Title=a2.accountNumber;
                c2.AccountId=a2.Id;
                conToInsert.add(c2);
            }
        }
        insert conToInsert;
    }
    
    if(trigger.isUpdate){
        for(account a:trigger.new){
            if(a.ownership__c=='Private'){
                //List<contact> oldContacts=[select name,id,phone,title from contact where accountid=:a.id];
                for(contact c: oldContacts){
                    c.title=a.AccountNumber;
                    c.phone=a.Phone;
                    conToUpdate.add(c);
                }
            }
        }
        update conToUpdate;
    }
}
Amit Chaudhary 8Amit Chaudhary 8
Update your if condition like below
if(acc.Ownership == 'Private' && mapAccCons.containsKey(acc.Id) )

Please try below code
trigger updateFieldsOnContact on Account (after insert, after update)
{
    set<id> setAccid = new set<id>();
    for(Account acc:trigger.new)
	{
        if(acc.Ownership =='Private' && acc.AccountNumber !=  Null && acc.Phone !=Null)
		{
            setAccid.add(acc.Id);
        }
    }
	
    if(setAccid.size()>0)
	{
		map<id, account> mapAccCons = new map<id, account>([select id, (select id, Phone, Title from Contacts) from Account where id in :setAccid]);
		list<Contact> cons = new List<Contact>();
	   
		for(account acc:trigger.new)
		{
			if(acc.Ownership == 'Private' && mapAccCons.containsKey(acc.Id).Contacts)
			{
				List<Contact> newUpdatingCons = mapAccCons.get(acc.Id).Contacts;
				for(Contact cont : newUpdatingCons)
				{
					cont.Phone = acc.Phone;
					cont.Title = acc.AccountNumber;
					cons.add(cont);
					
				}
			}
		}
		if(cons.size()>0){
			update cons;
		}
        
    }
}

Let us know if this will help you