Saturday 7 May 2016

PAGEREFERENCE AND EXTENSION AND SOQL Using Custom Controller

PAGEREFERENCE AND EXTENSION AND SOQL Using Custom Controller


<apex:page controller="CallVfDemoController">
<apex:form>
<apex:pageBlock>
<apex:commandButton value="Call Contact Page" action="{!Callcontactpage}"/>
<apex:commandButton value="Call Acount Page" action="{!Call_VF_Page}"/>
</apex:pageBlock>
</apex:form>
</apex:page>



Public with sharing class CallVfDemoController {
 
  Public CallVfDemoController(){
  }
   
  Public pagereference Call_VF_Page(){
    PageReference openvfpage = New Pagereference('/apex'+'/DemovfPage');
    openvfpage.setRedirect(false); 
    return openvfpage ;
    return openvfpage ;
  }
  Public pagereference Callcontactpage(){
   PageReference openvfpage2 = New Pagereference('/apex'+'/DemovfPage2');
   openvfpage2.setRedirect(false); 
 return openvfpage2 ;
  }
  
  
}



<apex:pagestandardcontroller="Account" sidebar="false" showHeader="false">
<apex:form>
   Welcome TO PAge2
<apex:pageBlock Title="Account">
<apex:pageBlockButtons>
<apex:commandButton Value="Save" Action="{!save}"/>
<apex:commandButton Value="Edit" Action="{!edit}"/>
 
</apex:pageBlockButtons>
<apex:pageBlockSection Title="Records">
<apex:InputField value="{!Account.Name}"/>
<apex:InputField value="{!Account.Industry}"/>
 
 
 
 
 
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>


<apex:pagestandardcontroller="Registrations__c" extensions="Regsearch2">
<apex:form>
 
 
<apex:OutputLabel Value="Enter Name For Register User"></apex:OutputLabel>
<apex:Inputtext value="{!searchname}"/>
<apex:commandButton Value="Search" action="{!searchrecords}"/>
<apex:pageBlock>
<apex:pageBlockTable value="{!searchrecords}" var="y" >
<apex:column value="{!y.Id}"/>
<apex:column value="{!y.Account__c}"/>
<apex:column value="{!y.Email__c}"/>
<apex:column value="{!y.Name__c}"/>
 
 
 
</apex:pageBlockTable>
 
 
</apex:pageBlock>
 
 
 
</apex:form>
 
 
</apex:page>


Extension Controller with Soql _________________


Public with sharing class Regsearch2{

    public PageReference searchrecords() {
        return null;
    }
    
    Public Regsearch2(ApexPages.StandardController stdController){
    
    
    }

Public String searchname{set;get;}
Public List<Registrations__c> regs;

Public List<Registrations__c>  getsearchrecords(){
regs=[SELECT Id,Account__c,Email__c,Name__c,Sur_Name__c FROM Registrations__c Where Name__c=:searchname];
return regs;
}


}

RECORDSETVAR

<apex:pagestandardController="contact" recordSetVar="contacts" showHeader="false" sidebar="false">
<apex:form>
<apex:pageBlockTabStyle="Account" >
<apex:pageBlockTable value="{!contacts}" var="y">
<apex:column Value="{!y.Account.Name}"/>
<apex:column Value="{!y.Name}"/>
<apex:column Value="{!y.Phone}"/>
<apex:column Value="{!y.Email}"/>
 
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
 


APEX TRIGGERS

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);
}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
=================================================================================================================