You need to sign in to do that
Don't have an account?
srilakshmi1.387861669756762E12
Trigger to Update account field when opportunity is inserted?
Hai friends, if any one want this criteria go thuough it .
trigger UpdateAccount on Opportunity (after insert,after delete, after update ) {
if(Trigger.isInsert) {
List<Account> acc=new List<Account>();
List<Id> listIds = new List<Id>();
for (opportunity childObj : Trigger.new) {
listIds.add(childObj.AccountId);
}
Map<id,Account> mapAccounts=new Map<id,Account>([SELECT Id,TotalCount__c FROM Account where Id IN :listIds]);
for (Opportunity opp :trigger.new){
account a=mapAccounts.get(opp.AccountId);
if(a!=null) {
if(a.TotalCount__c == null) a.TotalCount__c =100;
if(opp.Count__c != null) {
a.TotalCount__c -= opp.Count__c;
acc.add(a);
}
}
for(integer i=0;i<acc.size();i++)
{
update acc;
}
}
}
if(trigger.isDelete) {
List<Account> acc=new List<Account>();
List<Id> listIds = new List<Id>();
for (opportunity childObj : Trigger.old) {
listIds.add(childObj.AccountId);
}
Map<id,Account> mapAccounts=new Map<id,Account>([select id, TotalCount__C from Account where id=:listIds]);
for (opportunity childObj : Trigger.old) {
if(childObj.AccountId != null) {
account ac=mapAccounts.get(childObj.AccountId);
ac.TotalCount__c -= childObj.Count__c;
acc.add(ac);
}
}
for(integer i=0;i<acc.size();i++)
{
update acc;
}
}
if(trigger.isUpdate)
{
List<Account> acc=new List<Account>();
List<Id> listIds = new List<Id>();
List<opportunity> newopp=new List<opportunity>();
for (opportunity childObj : Trigger.new) {
listIds.add(childObj.AccountId);
newopp.add(childObj);
}
Map<id,Account> mapAccounts=new Map<id,Account>([select id, TotalCount__C from Account where id IN :listIds]);
for (opportunity childObj : Trigger.New) {
account ac=mapAccounts.get(childObj.AccountId);
Opportunity oldOp = Trigger.oldMap.get(childObj.ID);
if(ac.TotalCount__C == null) ac.TotalCount__C = 100;
if(childObj.Count__c !=oldOp.count__c)
{
if(oldOp.count__c>childObj.count__c)
{
Decimal i=oldOp.count__c-childObj.count__c;
ac.TotalCount__C =ac.TotalCount__C+i;
acc.add(ac);
}
else if(oldOp.count__c<childObj.count__c)
{
Decimal i=childObj.count__c-oldOp.count__c;
ac.TotalCount__C =ac.TotalCount__C-i;
acc.add(ac);
}
update acc;
}
}
}
}
trigger UpdateAccount on Opportunity (after insert,after delete, after update ) {
if(Trigger.isInsert) {
List<Account> acc=new List<Account>();
List<Id> listIds = new List<Id>();
for (opportunity childObj : Trigger.new) {
listIds.add(childObj.AccountId);
}
Map<id,Account> mapAccounts=new Map<id,Account>([SELECT Id,TotalCount__c FROM Account where Id IN :listIds]);
for (Opportunity opp :trigger.new){
account a=mapAccounts.get(opp.AccountId);
if(a!=null) {
if(a.TotalCount__c == null) a.TotalCount__c =100;
if(opp.Count__c != null) {
a.TotalCount__c -= opp.Count__c;
acc.add(a);
}
}
for(integer i=0;i<acc.size();i++)
{
update acc;
}
}
}
if(trigger.isDelete) {
List<Account> acc=new List<Account>();
List<Id> listIds = new List<Id>();
for (opportunity childObj : Trigger.old) {
listIds.add(childObj.AccountId);
}
Map<id,Account> mapAccounts=new Map<id,Account>([select id, TotalCount__C from Account where id=:listIds]);
for (opportunity childObj : Trigger.old) {
if(childObj.AccountId != null) {
account ac=mapAccounts.get(childObj.AccountId);
ac.TotalCount__c -= childObj.Count__c;
acc.add(ac);
}
}
for(integer i=0;i<acc.size();i++)
{
update acc;
}
}
if(trigger.isUpdate)
{
List<Account> acc=new List<Account>();
List<Id> listIds = new List<Id>();
List<opportunity> newopp=new List<opportunity>();
for (opportunity childObj : Trigger.new) {
listIds.add(childObj.AccountId);
newopp.add(childObj);
}
Map<id,Account> mapAccounts=new Map<id,Account>([select id, TotalCount__C from Account where id IN :listIds]);
for (opportunity childObj : Trigger.New) {
account ac=mapAccounts.get(childObj.AccountId);
Opportunity oldOp = Trigger.oldMap.get(childObj.ID);
if(ac.TotalCount__C == null) ac.TotalCount__C = 100;
if(childObj.Count__c !=oldOp.count__c)
{
if(oldOp.count__c>childObj.count__c)
{
Decimal i=oldOp.count__c-childObj.count__c;
ac.TotalCount__C =ac.TotalCount__C+i;
acc.add(ac);
}
else if(oldOp.count__c<childObj.count__c)
{
Decimal i=childObj.count__c-oldOp.count__c;
ac.TotalCount__C =ac.TotalCount__C-i;
acc.add(ac);
}
update acc;
}
}
}
}
logontokartik
Is there an issue that you are trying to ask? Can you please elaborate, your trigger doesnt seem to be right
srilakshmi1.387861669756762E12
No issue working fine.