You are on page 1of 4

trigger CreateNewAccountOpportunity on Account (after insert) {

List<Opportunity> listOfOpp = new List<Opportunity>();

For(Account ac : Trigger.New){
Opportunity opp = new Opportunity();
opp.Name = ac.Name;
//opportunity.FieldAPi = Account.FieldName;
//opp.AccountId = ac.Id;
opp.StageName = 'Proposal';
opp.Closedate = System.today() + 30;

listOfOpp.add(opp);
}

if(listOfOpp.isEmpty() == false){
Database.insert(listOfOpp);
}
}
=========================================================================
======
trigger ClosedOpportunityTrigger on Opportunity (after insert,after
update)
{
List<Task> tas = new List<Task>();
try
{
for(Opportunity opp : Trigger.New)
{
if(opp.StageName == 'Closed Won')
{
Task tt = new Task();
//tt.OwnerId = opp.Id;
tt.WhatId = opp.Id;
tt.Subject = 'Follow Up Test Task';
tt.status = 'Completed';
tt.Priority = 'Normal';
tas.add(tt);
}
}
Database.insert(tas,false);
}catch(Exception e)
{
System.debug('Check : '+e);
}

}
=========================================================================
==
trigger CountContactsOfAccount on Contact (after insert, after update,
after delete, after undelete)
{
Set<Id> accountsId = new Set<Id>();
/*for(Contact c: Trigger.IsDelete ? Trigger.Old : Trigger.New)
{
accountsId.add(c.AccountId);
System.debug('Account Ids : ' + accountsId);
} this one is also works*/
if(Trigger.IsInsert || Trigger.IsUpdate)
{
for(Contact c : Trigger.New)
{
accountsId.add(c.AccountId);
}

}else
{
for(Contact c : Trigger.Old)
{
accountsId.add(c.AccountId);
}
}

List<Account> lstToUpdate = new List<Account>();


for(Account acc : [Select ID, Number_of_contacts__c,(Select Id From
Contacts) From Account Where Id In: accountsId])
{
acc.Number_of_contacts__c = acc.contacts.size();
lstToUpdate.add(acc);
}
try
{
if(lstToUpdate.size() > 0 && !lstToUpdate.isEmpty())
{
Database.upsert(lstToUpdate, false);
}
}catch(Exception e)
{
System.debug('Thrown Exception : '+e.getMessage());
}

}
=========================================================================
=================================================
trigger RollUpAmount on Opportunity (after insert,after update, after
delete)
{
Set<Id> accids = new Set<Id>();
List<Account> updateAcc = new List<Account>();
if(Trigger.isInsert || Trigger.isUpdate)
{
for(Opportunity opp : Trigger.new)
{
accids.add(opp.AccountId);
}
System.debug('accids : '+accids);
}
System.debug('accids : '+accids);
List<AggregateResult> oop = [Select AccountId,Sum(Amount)amts from
Opportunity Where AccountId IN: accids group by AccountId];
System.debug('Check : '+oop);
for(AggregateResult opp1 : oop)
{
Account acc1 = new Account();
acc1.Id = (id)opp1.get('AccountId');
acc1.Total_Web_Contacts__c = (decimal)opp1.get('amts');
updateAcc.add(acc1);
}
update updateAcc;
}
=========================================================================
========================================================
trigger AccountCreateTask on Account (before Update)
{
Set<Id> accId = new Set<Id>();
for(Account acc : Trigger.New)
{
if(acc.Active_Task__c == True)
{
accId.add(acc.id);
}
}
list<Task> newTasks = new list<Task>();
for(Account acct : [Select Id,Active_Task__c,OwnerId From Account
Where Id IN: accId])
{
Task t = new Task();
t.priority = 'Normal';
t.Status = 'Not Started';
t.Subject = 'Call';
t.OwnerId = acct.OwnerId;
newTasks.add(t);
}
insert newTasks;

}
=========================================================================
=========================================================
trigger AddUserToAT on Customer__c (after insert,after update, before
Delete) {

Set<Id> customerIdToAccountId = new Set<Id>(); //Saving Set of


Account ID
Set<Id> customerOwnerId= new Set<Id>(); ////Saving Set of Owner
ID
List<AccountTeamMember> insertedAcctList = new
List<AccountTeamMember>(); // list to be inserted in Account Team Member
Id currentOppOwner; // Saving Customer owner ID

if(Trigger.isInsert){

for(Customer__C cut : Trigger.new)


{
if(cut.Account__c != null)
{
customerIdToAccountId.add(cut.Account__c );
customerOwnerId.add(cut.Account_Manager__c);
}
}

List<AccountTeamMember> lstTeam = [SELECT Id, AccountId, UserId,


User.Name, TeamMemberRole
FROM AccountTeamMember
WHERE AccountId IN
:customerIdToAccountId AND UserId IN : customerOwnerId];

for(Customer__C cust : Trigger.new)


{
if(lstTeam.Isempty())
{
currentOppOwner = cust.Account_Manager__c;
AccountTeamMember newInsertATM = new AccountTeamMember();
newInsertATM.AccountId = cust.Account__c;
newInsertATM.UserId = currentOppOwner;
newInsertATM.TeamMemberRole = cust.Name;
insertedAcctList.add(newInsertATM);
System.debug('Hello : '+insertedAcctList);
}

}
if(insertedAcctList.size() >0 && insertedAcctList != null)
{
Database.Insert(insertedAcctList);
}

if(Trigger.isDelete)
{
Set<id> custIds=new Set<id>();
for(Customer__c oldATM: trigger.old)
{
custIds.add(oldATM.Account_Manager__c);
System.debug('Hell : '+custIds);
}

List<AccountTeamMember> ATMdelete = [Select Id,UserId From


AccountTeamMember Where UserId In: custIds];
if(ATMdelete.size()>0 && ATMdelete !=null)
{
Database.delete(ATMdelete);
}
}

}
=========================================================================
============================================

You might also like