trigger SCENARIO1 on Account (after insert) {
list<contact> c=new list<contact>();
for(account a:trigger.new)
{
contact b=new contact();
b.LastName=a.Name;
b.AccountId=a.Id;
c.add(b);
}
insert c;
}
===================================================================================
=====
2.when ever a record is inserted to the contact automatically inserted to the account
trigger scenario2 on Contact (after insert) {
if(Recursive.flag)
{
Recursive.flag=false;
list<account>a=new list<account>();
for(contact c:trigger.new)
{
account a1=new account();
a1.Phone=c.Phone;
a1.Name=c.LastName;
a.add(a1);
}
insert a;
}
Recursive trigger fire:
3.avoid recursive trigger
public class Recursive {
public static boolean flag=true;
}
===================================================================================
===========
3.when ever a create opportunity object record updated total opportunies and total amount in account object
trigger scenario24 on Opportunity (after insert) {
set<id>ids=new set<id>();
for(Opportunity op:trigger.new)
,{
ids.add(op.accountid);
}
list<account>ac=[select Total_opportunities__c,Total_Amount__c,(select id,Amount from Opportunities ) from a
ccount where id=:ids];
for(account a:ac)
{
a.Total_opportunities__c=a.opportunities.size();
decimal sum=0;
for(opportunity p:a.opportunities)
{
sum=sum+p.amount;
}
a.Total_Amount__c=sum;
}
update ac;
}
===================================================================================
===================
4.contact object when ever department equal to cse automatically before inserted email field
trigger scenario4 on Contact (before insert) {
for(contact c:trigger.new)
{
if(c.Department=='CSE')
{
c.Email='';
}
}
}
===================================================================================
========
5.when ever we modify inputout object doctorname automatically update droppoff object text field no relation ship
trigger SCENARIO32 on Inputout__c (after update) {
list<Dropoff1__c>d=[select id,name,Text__c from Dropoff1__c where Text__c='naveen'];
string name;
for(Inputout__c c:trigger.new)
{
name=c.Doctor_Name__c;
}
for(Dropoff1__c dp:d)
{
dp.Text__c=name;
}
update d;
}
, ===================================================================================
=======
6.limit reached the records
trigger SCENARIO6 on Account (before insert,before update) {
integer count=0;
list<account>a=[select id,name from account where createddate=today or lastmodifieddate=today];
for(account ac:trigger.new)
{
count=a.size();
ac.NumberofLocations__c=count;
if(count>2)
{
ac.adderror('reached limit today');
}
}
}
===================================================================================
===========
7.can not inser/update/delete that user account object records
trigger scenario30 on Account (before insert,before update,before delete) {
user u=[select id,name from user where username=''];
if(u.id==userinfo.getUserId())
{
if(trigger.isdelete)
{
for(account a:trigger.old)
{
a.adderror('cant delete record');
}
}
if(trigger.isupdate)
{
for(account b:trigger.new)
{
b.adderror('can not update');
}
}
if(trigger.isinsert)
{
for(account c:trigger.new)
{
c.adderror('can not insert');
}
}
}
}
===================================================================================