Salesforce Automation Tool - Trigger Solved Problems
Salesforce Automation Tool - Trigger Solved Problems
when ever a record is inserted to the account automatically inserted to the contact
}
insert c;
}
===================================================================================
=====
2.when ever a record is inserted to the contact automatically inserted to the account
}
insert a;
}
===================================================================================
===========
3.when ever a create opportunity object record updated total opportunies and total amount in account object
}
update ac;
}
===================================================================================
===================
4.contact object when ever department equal to cse automatically before inserted email field
===================================================================================
========
5.when ever we modify inputout object doctorname automatically update droppoff object text field no relation ship
}
===================================================================================
=======
===================================================================================
===========
}
}
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');
}
}
}
}
===================================================================================
====================
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');
}
}
===================================================================================
=====
}
update a;
trigger:
========
trigger scenario11 on Contact (after insert) {
rollupsummery.increment(trigger.new);
}
===================================================================================
====
10. when ever opportunity stagename =closedwon automatically update the account field rating=hot
for(account a:ac)
{
a.Rating='hot';
}
update ac;
}
}
===============================================================================
11.when ever account name is naveen automatically update the contact all lastnames
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
{
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;
}
}
}
===================================================================================
===================
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 opprtunity fields
trigger scenario17 on Contact (after insert) {
list<opportunity>op=[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;
}
}
}
==================================================================================
a.rating = 'Hot';
}
}
==================================================================================
17. Whenever a new contact is created, prefix the firstname with Mr.
trigger Contrig on Contact (before insert) {
c.salutation = 'Mr';
}
}
==================================================================================
18. Prevent the insertion of an account, if you already have an account with the same name
(Prevent the insertion of a duplicate account).
if(setName.size() > 0 )
{
List<Account> lstAccount = [select name ,id from account where name in :setName ];
19. Whenever a new contact is inserted in the system, update the accounts phone field with the contacts phone field.
TRIGGER :
trigger contactTrigger on Contact (before insert, before update, before delete, after insert, after update, after d
elete, after undelete) {
if(trigger.isBefore ){
system.debug('I am before trigger ');
}
else if(trigger.isAfter){
system.debug('I am after trigger ');
if(trigger.isUpdate){
contactTriggerHandler.afterUpdateHelper(trigger.new);
}
}
HANDLER CLASS :
List<Account> accItemList = [Select Id, Name, Phone, (Select Id, FirstName, LastName, Phone, Accoun
tId From Contacts) From Account Where Id IN:setId];
for(Account a1:accItemList){
for(Contact c1:a1.Contacts){
if(c1.Phone !=null){
a1.Phone = c1.Phone;
update accItemList;
}
}
}
}
}
==================================================================================
c.AccountId = a.id;
c.lastName = a.name;
c.phone = a.phone;
contList.add(c);
}
insert contList;
}
==================================================================================
22. if account is not exist during creating record in contact object then create new account record ::HAVE DOU
BT NEED TO ASK
==================================================================================
23. Trigger will prevent the user to delete records from account
A.addError(' You can not delete the record, Please contact your Administartator');
}
}
==================================================================================
24. Prevent user from creating duplicate accounts with same name
if(setName.size() > 0 )
{
List<Account> lstAccount = [select name ,id from account where name in :setName ];
}
}
==================================================================================
25. Add a prefix dr to all leads names whenever a record is updated or inserted
26. Prevent user to delete contact which is associated with any account.
==================================================================================
==================================================================================
29. Whenever new record is created in account object, before this record inserted in account object Account,
delete all contact records avilable with the same name
==================================================================================
30. when ever a record is inserted to the Account automatically inserted to the Contact
==================================================================================
31. when ever a create opportunity object record updated total opportunies and total amount in account object
}
update ac;
}
==================================================================================
32. can not inser/update/delete that user account object records
==================================================================================
33. whenever opportunity StageName = Closed Won automatically update the account field Rating = Hot
34. name field is updated on account then put name field as name + Phone
==================================================================================
35.Whenever account is created with annual revenue more than 50000, then add Pranay mehare as contact Name ::
DONE (Check 50 k and insert contact )
Cont.AccountId = Acc.Id;
Cont.Firstname = 'Pranay';
Cont.Lastname = 'Mehare';
ContList.add(Cont);
}
}
Insert ContList;
}
==================================================================================
36. Whenever Lead is created with lead Source as WEB then give rating as COLD otherwise HOT
if (l.leadSource == 'Web'){
l.Rating = 'Cold';
}
Else{
l.Rating = 'Hot';
}
}
}
==================================================================================
AccIds.add(A.Id);
}
for (Account A:[SELECT Id, Name, (SELECT Id, Firstname, LastName FROM Contacts) FROM ACCOU
NT WHERE Id IN :AccIds]){
AccMap.put(A.Id, A);
}
for (Account A1:Trigger.Old){
if (AccMap.get(A1.id).contacts.Size() >= 2){
A1.addError('You Can not delete Account');
}
}
}
==================================================================================
38. While creating or updating Lead check whether Email is already there in existing contact , if it is there then thro
w an error.
//Unrelated Objects ::
contMap.put(c.Email, c);
}
for (lead l:Trigger.new){
==================================================================================
39. Whenever phone is modified on account object then update contact record with phone field (other phone with ol
d value and home phone with new value)
associated with account record.
con.HomePhone=trigger.newmap.get(con.AccountId).Phone;
con.OtherPhone = trigger.oldmap.get(con.AccountId).Phone;
conlist.add(con);
}
update conlist;
==================================================================================
40. Counts number of contacts on Account using trigger (Create Lookup Summary field using Trigger)
TRIGGER :
trigger ContCount on Contact (After Delete, After Insert, After Update, After Undelete) {
HANDLER CLASS :
try{
if (NewContact != Null){
for (Contact con:newContact){
if(Con.AccountId != Null){
ContAccIds.add(con.AccountId);
}
}
}
if (oldContact != Null){
for (Contact con:oldContact){
if(Con.AccountId != Null){
ContAccIds.add(con.AccountId);
}
}
}
List<Account> AccList = [SELECT Id, ContCount__c, (SELECT Id FROM Contacts) FROM Accoun
t Where Id IN :ContAccIds];
if (AccList != Null){
for (Account AccValue:AccList){
accvalue.ContCount__c = accValue.Contacts.Size();
}
}
if(!AccList.isEmpty()){
update AccList;
}
}
==================================================================================
TRIGGER :
createCont.CreateConts(Trigger.New);
}
}
HANDLER :
cList.add(c);
if (!cList.isEmpty()){
insert cList
}
}
}
==========================================================******************=========
==============================================