You need to sign in to do that
Don't have an account?
tulasiram 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;
}
}
}
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;
}
}
}
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;
}
}
}
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 .
Please try below code
Let us know if this will help you