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
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com 

Need to modify this trigger on Accounts-Please help

Hi all,

      I have this triggger that is written on accounts,It creates a new record in summary, when the account is inserted for the first time and updates the same record  next time onwards (if 2 records are same with same account name).Delete  function is also performed accordingly.

Here is the trigger given below:

 

 

trigger insertinSummary on Account (before insert,after delete)
{
//If record is deleted
if(trigger.isdelete)
{
Account acc=trigger.old[0];
list<Summary__c> summlist =[select Total_No_Of_Accounts__c,Total_Money_from_Account__c from Summary__c where name = : acc.name];
if(summlist.size()>0)
{
for(integer i=0;i<summlist.size();i++)
{
summlist[i].Total_No_Of_Accounts__c=summlist[i].Total_No_Of_Accounts__c-1;
summlist[i].Total_Money_from_Account__c=summlist[i].Total_Money_from_Account__c-acc.Money_from_Account__c;
}

update summlist;

}

 

}



//If record is inserted for the first time
if(trigger.isinsert)
{
Account acc=trigger.new[0];
list<account>accclist=[select id from account where name=:acc.name];
//if the added record doesn't exist in account object then the list size will be 0 and new entry will be made
if (accclist.size()==0)
{
Summary__c ss=new Summary__c(name=acc.name,Total_No_Of_Accounts__c=1,Total_Money_from_Account__c=acc.Money_from_Account__c);
insert ss;
}
else
{
//if the added record is already there then the values will be updated in the existing summary record

list<Summary__c> summlist=[select name,Total_No_Of_Accounts__c, Total_Money_from_Account__c from Summary__c where name =: acc.name];

for(integer i=0;i<summlist.size();i++)
{
summlist[i].Total_No_Of_Accounts__c=summlist[i].Total_No_Of_Accounts__c+1;
summlist[i].Total_Money_from_Account__c=summlist[i].Total_Money_from_Account__c+acc.Money_from_Account__c;

}
update summlist;

 



}

}

 

 

}

 

 

Now I wanna change the trigger so that records of summary__c are clubbed on basis of ownerId of accounts rather than Account Name.

                   So that, account  record is created for the first time with new Owner Id a new summary recoerd is created and next time onwards that same summary record is updated (instead of creating a new one).Irrespective of account name I wanna calculate the no.of accounts and total money that one owner/user has.

salesforce expertsalesforce expert

create owner id in summary custom object. on update see if owner id already exists and update the count if it is there or create a new record with the ownerid ....

 

moreover i didnt see a code for after/before update in your trigger...

 

tell me if this clue helps you! i had the same need but it is similar to your issue.. let me know if you need the code to get an idea.!!

 

 

baskaran

shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com

Hi,

   Thanks a lot for bringing this to my notice.Please help me with the update part.I want only the amount to get updated and not the counter.When someone opens a previous record and updates its amount.

shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com

And as far as ownerid field is concerned, I am assigning it to name field of summary objetc.Its not working probably because its not written AFTER INSERT. Ownerid is generated only after insert