I have 2 Object, No relationship between obj - one field is common - My requirement is once i change fiedl value in 1 objject it should be refelct in other obj in 1 of field. how to write trigger for that
Here I'm tring to count Child record and update total count in parent. [lookup relationship],
This trigger is working fine with insert but throw error while delete . when I add opty it reflect count , but when i delete opty it wont update total on Account page - below is code snippet. --------------------------------------------------------------------------------------------------------------------------------------- trigger Opty_ChildCount on Opportunity (after insert, after delete) { set<id> setid = new set<id>(); for(Opportunity op : Trigger.new) { setid.add(op.AccountId); } List<Account> accList = new List<Account>(); List<Account> acc = [select id, Opty_ChildCount__c,(select id from opportunities) from Account where id IN : setid]; if(Trigger.isInsert && Trigger.isAfter) { for(Opportunity opty : Trigger.new) { for(Account a:acc) { Account a1 = new Account(); a1.id = a.id a1.Opty_ChildCount__c = a.Opportunities.size(); accList.add(a1); } } } if(Trigger.isDelete && Trigger.isAfter) { for(Account a:acc) { Account a1 = new Account(); a1.id = a.id; a1.Opty_ChildCount__c = a.Opportunities.size(); accList.add(a1); } } update accList; }
@Ajay K Dubedi For above trigger can you please help me with update trigger code. when user edit opportunity and change (related to) account name, at that time count get updated on AC page.
You can use the below code:
<------ Apex class--->>>>>
<<<---- Trigger------>>>>
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
All Answers
You can use the below code:
<------ Apex class--->>>>>
<<<---- Trigger------>>>>
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
This is working absolutely fine.
Can you Help me with one more trigger.
Here I'm tring to count Child record and update total count in parent. [lookup relationship],
This trigger is working fine with insert but throw error while delete .
when I add opty it reflect count , but when i delete opty it wont update total on Account page - below is code snippet.
---------------------------------------------------------------------------------------------------------------------------------------
trigger Opty_ChildCount on Opportunity (after insert, after delete)
{
set<id> setid = new set<id>();
for(Opportunity op : Trigger.new)
{
setid.add(op.AccountId);
}
List<Account> accList = new List<Account>();
List<Account> acc = [select id, Opty_ChildCount__c,(select id from opportunities) from Account where id IN : setid];
if(Trigger.isInsert && Trigger.isAfter)
{
for(Opportunity opty : Trigger.new)
{
for(Account a:acc)
{
Account a1 = new Account();
a1.id = a.id
a1.Opty_ChildCount__c = a.Opportunities.size();
accList.add(a1);
}
}
}
if(Trigger.isDelete && Trigger.isAfter)
{
for(Account a:acc)
{
Account a1 = new Account();
a1.id = a.id;
a1.Opty_ChildCount__c = a.Opportunities.size();
accList.add(a1);
}
}
update accList;
}
You can use the below code:
<------ Apex class--->>>>>
<<<---- Trigger------>>>>
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
and way more optimized code.
Thank you.
For above trigger can you please help me with update trigger code.
when user edit opportunity and change (related to) account name, at that time count get updated on AC page.