1.When ever a record is inserted to the account automatically inserted to the contact.
trigger SCENARIO1 on Account (after insert) {
list c=new list();
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;
lista=new list();
for(contact c:trigger.new)
{
account a1=new account();
a1.Phone=c.Phone;
a1.Name=c.LastName;
a.add(a1);
}
insert a;
}
3.How to avoid recursive trigger in salesforce ?
public class Recursive {
public static boolean flag=true;
}
3.When ever a created opportunity object record, update total opportunities and total amount in account object.
trigger scenario24 on Opportunity (after insert) {
setids=new set();
for(Opportunity op:trigger.new)
{
ids.add(op.accountid);
}
listac=[select Total_opportunities__c,Total_Amount__c,(select id,Amount from Opportunities ) from account 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.On contact object ,when ever department equal to cse automatically insert email on email field.
trigger scenario4 on Contact (before insert) {
for(contact c:trigger.new)
{
if(c.Department==’CSE’)
{
c.Email=’sfdcforum@gmail.com’;
}
}
}
5.When ever we modifying inputout__c object doctor name automatically update on droppoff__c object text field on relationship.
trigger SCENARIO32 on Inputout__c (after update) {
listd=[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;
lista=[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 insert/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=’naveensfdc98@gmail.com’];
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’);
}
}
}
}
8.Already existing records display an error message.
trigger scenario8 on Contact (before insert) {
listst=new list();
for(contact c:trigger.new)
{
list<contact>a=[select id,name,Email,lastname from contact where Email=:c.Email];
if(a.size()>0)
{
c.Email.adderror(‘already existing’);
}
}
}
9.for loop without query.
trigger duplicatetrigger on Inputout__c (before insert) {
sets=new set();
for(Inputout__c op:trigger.new)
{
s.add(op.Doctor_Name__c);
}
listd=[select id,Doctor_Name__c from Inputout__c where Doctor_Name__c=:s];
setdupids=new set();
for(Inputout__c don:d)
{
dupids.add(don.Doctor_Name__c);
}
for(Inputout__c c:trigger.new)
{
if(c.Doctor_Name__c!=null)
{
if(dupids.contains(c.Doctor_Name__c))
{
c.Doctor_Name__c.adderror(‘already existing record’);
}
}
}
9.count of related contacts and accounts field display size
public class rollupsummery {
public static void increment(list<contact>con)
{
set<id>ids=new set<id>();
for(contact c:con)
{
ids.add(c.accountid);
}
list<account>a=[select id,name,NumberOfEmployees,(select id,lastname from contacts) from account where id=:ids];
for(account ac:a)
{
ac.NumberOfEmployees=ac.contacts.size();
}
update a;
}
trigger:
trigger scenario11 on Contact (after insert) {
rollupsummery.increment(trigger.new); }
- when ever opportunity stagename =closedwon automatically update the account field rating=hot.
trigger scenario12 on Opportunity (after insert,after update) {
setids=new set();
listac=new list();
for(opportunity op:trigger.new)
{
ids.add(op.AccountId);
ac=[select id,name,rating from account where id=:ids];
if(op.StageName==’Closed won’)
{
for(account a:ac)
{
a.Rating=’hot’;
}
update ac;
}
}
}
11.when ever account name is “SFDCForum” automatically update the contact all lastnames.
trigger scenario13 on Account (after update) {
string names;
list<contact>c=[select id,lastname,firstname from contact where lastname=:names ];
for(account a:trigger.new)
{
names=a.name;
}
for(contact con:c)
{
con.lastname=names;
}
update c;
}
12.When ever a opportunity created record amount field is calculated by account total field
trigger scenario21 on Opportunity (after insert,after update,after delete) {
setids=new set();
mapopp=new map();
Decimal oldVal;
Decimal newVal;
if(trigger.isinsert)
{
for(opportunity op:trigger.new)
{
ids.add(op.AccountId);
opp.put(op.AccountId, op);
}
list<account> acc=[select id,Total_Amount__c from account where id=:ids];
for(account a:acc)
{
if(a.Total_Amount__c==null )
{
a.Total_Amount__c=opp.get(a.Id).amount;
}
else
{
a.Total_Amount__c= a.Total_Amount__c+opp.get(a.Id).amount;
}
}
update acc;
}
if(trigger.isUpdate)
{
for(opportunity op:trigger.new)
{
ids.add(op.AccountId);
opp.put(op.AccountId, op);
newVal=op.Amount;
}
for(Opportunity ops:trigger.old){
oldVal=ops.Amount;
}
list<account> acc=[select id,Total_Amount__c from account where id=:ids];
for(account a:acc)
{
if(a.Total_Amount__c==null )
{
a.Total_Amount__c=opp.get(a.Id).amount;
}
else
{
a.Total_Amount__c= a.Total_Amount__c+opp.get(a.Id).amount-oldVal;
}
}
update acc;
}
}
}
13.when ever a create a lead object automatically converted account ,contact,opportunity.
trigger scenario19 on Lead (after insert) {
listacc=new list();
listcon=new list();
listop=new list();
for(lead l:trigger.new)
{
account a=new account();
a.Name=l.lastname;
a.Phone=l.Phone;
acc.add(a);
contact c=new contact();
c.LastName=l.Name;
con.add(c);
opportunity o=new opportunity();
o.Amount=l.AnnualRevenue;
o.CloseDate=system.today();
o.StageName=’closed won’;
op.add(o);
}
insert acc;
insert con;
insert op;
}
14.when ever create a contact automatically update opportunity fields.
trigger scenario17 on Contact (after insert) {
listop=[select id,name,stagename,Description,amount from opportunity limit 50];
for(contact c:trigger.new){
for(opportunity o:op)
{
if(o.amount<5000||o.Amount==null)
{
o.amount=5000;
o.Name=o.Name+’Mr’;
o.StageName=’prospecting’;
}
else{
o.Amount=o.Amount+1000;
o.Name=o.Name+’Dr’;
}
update o;
}
}
}
- What is Action poller in salesforce.
public class actionpoller1 {
public datetime dateandtime{get;set;}
public void datetimemethod()
{
dateandtime=system.now();
Visualforce page :
<apex:page controller=”actionpoller1″>
<apex:form>
<apex:pageBlock id=”pb”>
<apex:actionPoller action=”{!datetimemethod}” reRender=”pb” interval=”5″ />
time:{!dateandtime}
refresh 5 mins
</apex:pageBlock>
</apex:form>
</apex:page>
16. What is Action : status
public class actionstatus {
Integer count = 0;
public PageReference incrementCounter() {
count++;
return null;
}
public Integer getCount() {
return count;
}
}
visualforce page:
<apex:page controller=”actionstatus”>
<apex:form >
<apex:outputpanel id=”counter”>
<apex:outputText value=”Click Me!: {!count}”/>
<apex:actionSupport event=”onclick” action=”{!incrementCounter}” rerender=”counter” status=”counterStatus”/>
</apex:outputpanel>
<apex:actionStatus id=”counterStatus” startText=” (processing…)” stopText=”(completed)”/>
</apex:form>
</apex:page>
17.What is Aggregate functions in salesforce?
public class aggregatefunctions {
public listac{get;set;}
public integer count{get;set;}
public decimal sum{get;set;}
public decimal min{get;set;}
public decimal max{get;set;}
public aggregatefunctions()
{
ac= [select id,name,AnnualRevenue from account where name=:’naveen’];
count=[select count() from account];
aggregateresult res=[select sum(AnnualRevenue)sumt,min(AnnualRevenue)mint,max(AnnualRevenue)maxt from account ];
sum=(decimal)res.get(‘sumt’);
min=(decimal)res.get(‘mint’);
max=(decimal)res.get(‘maxt’);
}
Visualforce page:
<apex:page controller=”aggregatefunctions” >
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockTable value=”{!ac}” var=”n”>
<apex:column value=”{!n.name}”/>
<apex:column value=”{!n.AnnualRevenue}”/>
</apex:pageBlockTable>
</apex:pageBlockSection>
{!sum}
{!min}
{!max}
</apex:pageBlock>
</apex:form>
</apex:page>
18.Sending email outbound email message
public class emailprogramme1 {
public void myemails()
{
messaging.SingleEmailMessage m1=new messaging.SingleEmailMessage();
string[] toadd=new string[]{‘naveengorentla1@gmail.com’};
string[] tocc=new string[]{‘naveen123sfdc@gmail.com’};
m1.setToAddresses(toadd);
m1.setCcAddresses(tocc);
m1.setSubject(‘accenture’);
m1.setPlainTextBody(‘this is interview call letter’);
messaging.email[] m2=new messaging.Email[]{m1};
messaging.sendEmail(m2);
}
}
Executions:
emailprogramme1 v=new emailprogramme1();
v.myemails();
18.Outbound message pdf file.
public class emailprogramme2 {
public void emailbody()
{
messaging.SingleEmailMessage m1=new messaging.SingleEmailMessage();
string[] toadd=new string[]{‘naveengorentla1@gmail.com’};
m1.setToAddresses(toadd);
m1.setSubject(‘Pdf file’);
m1.setPlainTextBody(‘THIS IS BILL OF TELEPHONE’);
messaging.EmailFileAttachment m2=new messaging.EmailFileAttachment();
pagereference p=page.page1;
blob body=p.getContentAsPDF();
m2.setBody(body);
m2.setFileName(‘jan-feb-march’);
messaging.EmailFileAttachment[] eft1=new messaging.EmailFileAttachment[]{m2};
m1.setFileAttachments(eft1);
messaging.Email[] m3=new messaging.Email[]{m1};
messaging.sendEmail(m3);
}
Output :
emailprogramme2 v=new emailprogramme2();
v.emailbody();
19.sending email template to outbound message
public class emailprogramme3 {
public void emailmethod()
{
messaging.SingleEmailMessage m1=new messaging.SingleEmailMessage();
emailtemplate et=[select id from emailtemplate where name=’doctor’ ];
m1.setTemplateId(et.Id);
contact c=[select id,lastname,phone from contact where phone=’999′];
m1.setTargetObjectId(c.id);
Inputout__c D=[select id,Doctor_Name__c from Inputout__c limit 1];
m1.setWhatId(D.Id);
messaging.Email[] m2=new messaging.Email[]{m1};
messaging.sendEmail(m2);
}
}
output:
emailprogramme3 v=new emailprogramme3();
v.emailmethod();
20.Sending email trigger.
trigger sendingmailtrigger on Inputout__c (before insert) {
for(Inputout__c i:trigger.new)
{
if(i.Check_box__c==true)
{
messaging.SingleEmailMessage m1=new messaging.SingleEmailMessage();
string[] toadd =new string[]{‘naveengorentla1@gmail.com’};
m1.setToAddresses(toadd);
m1.setSubject(‘accenture’);
m1.setPlainTextBody(‘this is interview call letter’);
messaging.Email[] mail1=new messaging.Email[]{m1};
messaging.sendEmail(mail1);
}
}
}