Custom setting Example:
trigger customsettingexample on Opportunity (before insert) {
// Grab your Custom Setting values
CSR_Settings__c settings = CSR_Settings__c.getInstance('csr');
String CSR_USER_ID = settings.CSR_User_ID__c;
Decimal OPP_MIN_VALUE = settings.Opp_Minimum_Value__c;
// Create a master list of accounts to bulk update
List<Account> accounts = new List<Account>();
for (Opportunity opp : Trigger.new) {
// Make sure we meet the minimum threshold
if (opp.Amount >= OPP_MIN_VALUE) {
// This is a trick to get the related account!
Account acc = new Account();
acc.Name=opp.AccountId;
acc.Id = opp.AccountId;
// Update the CSR and add to master list
// acc.CSR__c = CSR_USER_ID;
accounts.add(acc);
}
}
// Update the master list of accounts
// update accounts;
insert accounts;
}
_____________________________________________________________
================================================================
New NOTE create
----------------
trigger insertNote on Contact (after insert, after update){
List<Note> noteList = new List<Note>();
if(Trigger.isAfter && Trigger.isInsert){
for(Contact c : trigger.new){
//c.MobilePhone='123456789';
if(c.LastName != null){
Note newNote = new Note();
newNote.Title = 'Updated contact Record';
newNote.Body = c.LastName;
newNote.ParentId = c.Id;
noteList.add(newNote);
}
}
}
if(Trigger.isAfter && Trigger.isUpdate){
for(Contact c : trigger.new){
// c.MobilePhone='123456789';
if(trigger.oldMap.get(c.id).LastName != c.LastName){
Note newNote = new Note();
newNote.Title ='Updated contact Record';
newNote.Body = c.LastName;
newNote.ParentId = c.Id;
noteList.add(newNote);
}
}
}
if(noteList.size() > 0){
insert noteList;
}
}
======================================================================================
-----------------------------------------------------------------------------------------
Ne note on Account object
---------------------------
===============================
trigger insertnoteonacc on Account (Before insert,after insert, after update){
List<Note> noteList = new List<Note>();
if(Trigger.isBefore && Trigger.isInsert){
for(Account c : trigger.new){
c.Phone='123456789';
// Note newNote = new Note();
// newNote.Title = 'Updated ACC Record';
// newNote.Body = 'Name';
// newNote.ParentId = krishna138;
// noteList.add(newNote);
// insert noteList;
}
}
if(Trigger.isAfter && Trigger.isInsert ){
for(Account c : trigger.new){
if(c.Name != null){
Note newNote = new Note();
newNote.Title = 'Updated ACC Record';
newNote.Body = c.Name;
newNote.ParentId = c.Id;
noteList.add(newNote);
}
}
}
if(Trigger.isAfter && Trigger.isUpdate){
for(Account c : trigger.new){
if(trigger.oldMap.get(c.id).Name != c.Name){
Note newNote = new Note();
newNote.Title ='Updated Acc Record';
newNote.Body = c.Name;
newNote.ParentId = c.Id;
noteList.add(newNote);
}
}
}
if(noteList.size() > 0){
insert noteList;
}
}
=========================================================================================
------------------------------------------------------------------------------------------------------
No Phone Number simple
+++++++++++++++++++++++++++
+++++++++==================
trigger nophonenum on Account (before insert) {
for(Account a:Trigger.New){
if(a.phone=='' || a.phone==null){
a.addError('Phone no is a must ');
}
}
}
-----------------------------------------------------
sample
===========
trigger SampleTrigger8 on Opportunity (before insert,before Update) {
for(Opportunity opp:Trigger.New){
if(Opp.StageName == 'Closed Lost')
{
Opp.Description='Updated';
}
else
{
Opp.Description='Not Updated';
}
}
}
=============================================
Send Email Job Trigger
=====================
---------------------
+++++++++++++++++++++
trigger SendEmailJob on Opportunity (after insert,after update) {
Set<Id> setResource=new Set<Id>();
Set<Id> setjob=new Set<Id>();
List<NewJob__c> LatestJob=new List<NewJob__c>();
for(Opportunity opp:Trigger.new){
if(opp.Status__c == 'new'){
if(opp.Resource__c != null ){
setResource.add(opp.Resource__c);
}
}
}
List<String> lstEmails=new List<String>();
if(!setResource.isEmpty()){
List<Resource__c> lstResource= [Select Id,Email__c from Resource__c Where Id IN : setResource];
if(!lstResource.isEmpty())
{
for(Resource__c res:lstResource)
{
lstEmails.add(res.Email__c);
}
}
}
List<NewJob__c> newJoblist=[SELECT Id FROM NewJob__c order by CreatedDate desc Limit 1];
if(!newJoblist.isEmpty())
{
for(NewJob__c jobl:newJoblist){
setjob.add(jobl.id);
}
}
String Body='';
if(!setjob.isEmpty()){
List<Job_Application__c> Joblist=[SELECT Id FROM Job_Application__c WHERE Job__r.Id IN:setjob];
if(!Joblist.isEmpty())
{
for(Job_Application__c job:Joblist){
body=body+'https://ap2.salesforce.com/'+job.id+'\n';
}
}
}
if(!lstEmails.isEmpty()){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(lstEmails);
mail.setSubject('Job Alert');
mail.setPlainTextBody(Body);
mail.setHtmlBody(Body);
Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
}
}
============================================================================================================================
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
another Send Job mail
______________________________________________________________
trigger SendEmailUser on Opportunity (after insert,after update) {
Set<Id> setResource=new Set<Id>();
Set<Id> setjob=new Set<Id>();
List<NewJob__c> LatestJob=new List<NewJob__c>();
for(Opportunity opp:Trigger.new){
if(opp.Status__c == 'new'){
if(opp.Resource__c != null ){
setResource.add(opp.Resource__c);
// setjob.add(opp.Job__c);
}
}
}
List<String> lstEmails=new List<String>();
if(!setResource.isEmpty()){
List<Resource__c> lstResource= [Select Id,Email__c from Resource__c Where Id IN : setResource];
if(!lstResource.isEmpty())
{
for(Resource__c res:lstResource)
{
lstEmails.add(res.Email__c);
}
}
}
// Set<String> nid=new Set<String>();
// LatestJob=[SELECT Id FROM NewJob__c ORDER BY LastModifiedDate LIMIT 1];
String Body='';
if(!setResource.isEmpty()){
List<Job_Application__c> Joblist=[SELECT Id FROM Job_Application__c order by CreatedDate desc limit 1];
if(!Joblist.isEmpty())
{
for(Job_Application__c job:Joblist){
body=body+'https://ap2.salesforce.com/'+job.id+'\n';
}
}
}
if(!lstEmails.isEmpty()){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(lstEmails);
mail.setSubject('Job Alert');
mail.setPlainTextBody(Body);
mail.setHtmlBody(Body);
Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++
======================================================================================\\
Test Trigger
__________
+++++++++++++++
trigger testtrigger on Account (after insert) {
List<Note> noteList = new List<Note>();
for(Account a:Trigger.New)
if(a.Website != null){
a.Website='www.techyerru.com';
Note newNote = new Note();
newNote.Title = 'Custom Title';
newNote.Body = 'webnote';
newNote.ParentId = a.Id;
noteList.add(newNote);
}
if(noteList.size() > 0){
insert noteList;
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
=============================================================================================
once user register with details send a mail to that user associate email with data and CC BB details
=======================================================================================================
// Send a business proposal to each new Contact
trigger sendemail on Registrations__c (before insert) {
// Step 0: Create a master list to hold the emails we'll send
List<Messaging.SingleEmailMessage> mails =new List<Messaging.SingleEmailMessage>();
for (Registrations__c myContact : Trigger.new) {
if (myContact.Email__c != null && myContact.Account__c != null) {
// Step 1: Create a new Email
Messaging.SingleEmailMessage mail =
new Messaging.SingleEmailMessage();
// Step 2: Set list of people who should get the email
List<String> sendTo = new List<String>();
sendTo.add(myContact.Email__c);
mail.setToAddresses(sendTo);
// Step 3: Set who the email is sent from
mail.setReplyTo('krishna.yerru@gmail.com');
mail.setSenderDisplayName('Techyerru update');
// (Optional) Set list of people who should be CC'ed
List<String> ccTo = new List<String>();
ccTo.add('kyerru@gmail.com');
mail.setCcAddresses(ccTo);
// Step 4. Set email contents - you can use variables!
mail.setSubject('URGENT BUSINESS PROPOSAL');
String body = 'Dear ' + myContact.Name__c + ', ';
body += 'I confess this will come as a surprise to you.';
body += 'I am John Alliston CEO of the Bank of Nigeria.';
body += 'I write to request your cooperation in this ';
body += 'urgent matter as I need a foreign partner ';
body += 'in the assistance of transferring $47,110,000 ';
body += 'to a US bank account. Please respond with ';
body += 'your bank account # so I may deposit these funds.';
mail.setHtmlBody(body);
// Step 5. Add your email to the master list
mails.add(mail);
}
}
// Step 6: Send all emails in the master list
Messaging.sendEmail(mails);
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
=================================================================================================================
trigger customsettingexample on Opportunity (before insert) {
// Grab your Custom Setting values
CSR_Settings__c settings = CSR_Settings__c.getInstance('csr');
String CSR_USER_ID = settings.CSR_User_ID__c;
Decimal OPP_MIN_VALUE = settings.Opp_Minimum_Value__c;
// Create a master list of accounts to bulk update
List<Account> accounts = new List<Account>();
for (Opportunity opp : Trigger.new) {
// Make sure we meet the minimum threshold
if (opp.Amount >= OPP_MIN_VALUE) {
// This is a trick to get the related account!
Account acc = new Account();
acc.Name=opp.AccountId;
acc.Id = opp.AccountId;
// Update the CSR and add to master list
// acc.CSR__c = CSR_USER_ID;
accounts.add(acc);
}
}
// Update the master list of accounts
// update accounts;
insert accounts;
}
_____________________________________________________________
================================================================
New NOTE create
----------------
trigger insertNote on Contact (after insert, after update){
List<Note> noteList = new List<Note>();
if(Trigger.isAfter && Trigger.isInsert){
for(Contact c : trigger.new){
//c.MobilePhone='123456789';
if(c.LastName != null){
Note newNote = new Note();
newNote.Title = 'Updated contact Record';
newNote.Body = c.LastName;
newNote.ParentId = c.Id;
noteList.add(newNote);
}
}
}
if(Trigger.isAfter && Trigger.isUpdate){
for(Contact c : trigger.new){
// c.MobilePhone='123456789';
if(trigger.oldMap.get(c.id).LastName != c.LastName){
Note newNote = new Note();
newNote.Title ='Updated contact Record';
newNote.Body = c.LastName;
newNote.ParentId = c.Id;
noteList.add(newNote);
}
}
}
if(noteList.size() > 0){
insert noteList;
}
}
======================================================================================
-----------------------------------------------------------------------------------------
Ne note on Account object
---------------------------
===============================
trigger insertnoteonacc on Account (Before insert,after insert, after update){
List<Note> noteList = new List<Note>();
if(Trigger.isBefore && Trigger.isInsert){
for(Account c : trigger.new){
c.Phone='123456789';
// Note newNote = new Note();
// newNote.Title = 'Updated ACC Record';
// newNote.Body = 'Name';
// newNote.ParentId = krishna138;
// noteList.add(newNote);
// insert noteList;
}
}
if(Trigger.isAfter && Trigger.isInsert ){
for(Account c : trigger.new){
if(c.Name != null){
Note newNote = new Note();
newNote.Title = 'Updated ACC Record';
newNote.Body = c.Name;
newNote.ParentId = c.Id;
noteList.add(newNote);
}
}
}
if(Trigger.isAfter && Trigger.isUpdate){
for(Account c : trigger.new){
if(trigger.oldMap.get(c.id).Name != c.Name){
Note newNote = new Note();
newNote.Title ='Updated Acc Record';
newNote.Body = c.Name;
newNote.ParentId = c.Id;
noteList.add(newNote);
}
}
}
if(noteList.size() > 0){
insert noteList;
}
}
=========================================================================================
------------------------------------------------------------------------------------------------------
No Phone Number simple
+++++++++++++++++++++++++++
+++++++++==================
trigger nophonenum on Account (before insert) {
for(Account a:Trigger.New){
if(a.phone=='' || a.phone==null){
a.addError('Phone no is a must ');
}
}
}
-----------------------------------------------------
sample
===========
trigger SampleTrigger8 on Opportunity (before insert,before Update) {
for(Opportunity opp:Trigger.New){
if(Opp.StageName == 'Closed Lost')
{
Opp.Description='Updated';
}
else
{
Opp.Description='Not Updated';
}
}
}
=============================================
Send Email Job Trigger
=====================
---------------------
+++++++++++++++++++++
trigger SendEmailJob on Opportunity (after insert,after update) {
Set<Id> setResource=new Set<Id>();
Set<Id> setjob=new Set<Id>();
List<NewJob__c> LatestJob=new List<NewJob__c>();
for(Opportunity opp:Trigger.new){
if(opp.Status__c == 'new'){
if(opp.Resource__c != null ){
setResource.add(opp.Resource__c);
}
}
}
List<String> lstEmails=new List<String>();
if(!setResource.isEmpty()){
List<Resource__c> lstResource= [Select Id,Email__c from Resource__c Where Id IN : setResource];
if(!lstResource.isEmpty())
{
for(Resource__c res:lstResource)
{
lstEmails.add(res.Email__c);
}
}
}
List<NewJob__c> newJoblist=[SELECT Id FROM NewJob__c order by CreatedDate desc Limit 1];
if(!newJoblist.isEmpty())
{
for(NewJob__c jobl:newJoblist){
setjob.add(jobl.id);
}
}
String Body='';
if(!setjob.isEmpty()){
List<Job_Application__c> Joblist=[SELECT Id FROM Job_Application__c WHERE Job__r.Id IN:setjob];
if(!Joblist.isEmpty())
{
for(Job_Application__c job:Joblist){
body=body+'https://ap2.salesforce.com/'+job.id+'\n';
}
}
}
if(!lstEmails.isEmpty()){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(lstEmails);
mail.setSubject('Job Alert');
mail.setPlainTextBody(Body);
mail.setHtmlBody(Body);
Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
}
}
============================================================================================================================
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
another Send Job mail
______________________________________________________________
trigger SendEmailUser on Opportunity (after insert,after update) {
Set<Id> setResource=new Set<Id>();
Set<Id> setjob=new Set<Id>();
List<NewJob__c> LatestJob=new List<NewJob__c>();
for(Opportunity opp:Trigger.new){
if(opp.Status__c == 'new'){
if(opp.Resource__c != null ){
setResource.add(opp.Resource__c);
// setjob.add(opp.Job__c);
}
}
}
List<String> lstEmails=new List<String>();
if(!setResource.isEmpty()){
List<Resource__c> lstResource= [Select Id,Email__c from Resource__c Where Id IN : setResource];
if(!lstResource.isEmpty())
{
for(Resource__c res:lstResource)
{
lstEmails.add(res.Email__c);
}
}
}
// Set<String> nid=new Set<String>();
// LatestJob=[SELECT Id FROM NewJob__c ORDER BY LastModifiedDate LIMIT 1];
String Body='';
if(!setResource.isEmpty()){
List<Job_Application__c> Joblist=[SELECT Id FROM Job_Application__c order by CreatedDate desc limit 1];
if(!Joblist.isEmpty())
{
for(Job_Application__c job:Joblist){
body=body+'https://ap2.salesforce.com/'+job.id+'\n';
}
}
}
if(!lstEmails.isEmpty()){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(lstEmails);
mail.setSubject('Job Alert');
mail.setPlainTextBody(Body);
mail.setHtmlBody(Body);
Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++
======================================================================================\\
Test Trigger
__________
+++++++++++++++
trigger testtrigger on Account (after insert) {
List<Note> noteList = new List<Note>();
for(Account a:Trigger.New)
if(a.Website != null){
a.Website='www.techyerru.com';
Note newNote = new Note();
newNote.Title = 'Custom Title';
newNote.Body = 'webnote';
newNote.ParentId = a.Id;
noteList.add(newNote);
}
if(noteList.size() > 0){
insert noteList;
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
=============================================================================================
once user register with details send a mail to that user associate email with data and CC BB details
=======================================================================================================
// Send a business proposal to each new Contact
trigger sendemail on Registrations__c (before insert) {
// Step 0: Create a master list to hold the emails we'll send
List<Messaging.SingleEmailMessage> mails =new List<Messaging.SingleEmailMessage>();
for (Registrations__c myContact : Trigger.new) {
if (myContact.Email__c != null && myContact.Account__c != null) {
// Step 1: Create a new Email
Messaging.SingleEmailMessage mail =
new Messaging.SingleEmailMessage();
// Step 2: Set list of people who should get the email
List<String> sendTo = new List<String>();
sendTo.add(myContact.Email__c);
mail.setToAddresses(sendTo);
// Step 3: Set who the email is sent from
mail.setReplyTo('krishna.yerru@gmail.com');
mail.setSenderDisplayName('Techyerru update');
// (Optional) Set list of people who should be CC'ed
List<String> ccTo = new List<String>();
ccTo.add('kyerru@gmail.com');
mail.setCcAddresses(ccTo);
// Step 4. Set email contents - you can use variables!
mail.setSubject('URGENT BUSINESS PROPOSAL');
String body = 'Dear ' + myContact.Name__c + ', ';
body += 'I confess this will come as a surprise to you.';
body += 'I am John Alliston CEO of the Bank of Nigeria.';
body += 'I write to request your cooperation in this ';
body += 'urgent matter as I need a foreign partner ';
body += 'in the assistance of transferring $47,110,000 ';
body += 'to a US bank account. Please respond with ';
body += 'your bank account # so I may deposit these funds.';
mail.setHtmlBody(body);
// Step 5. Add your email to the master list
mails.add(mail);
}
}
// Step 6: Send all emails in the master list
Messaging.sendEmail(mails);
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
=================================================================================================================
No comments:
Post a Comment