Friday, 3 January 2014

Sending emails to the selected roles email id if there are checkbox selected.

Problem:- Recently one of my friend asked me to post a code for the below requirement:-

If there are checkbox selected on a VF Page then he wanted to send an email to the CEO role and Admin role users.

Please let me know if any issue.
Solution: I gave a try and created a VF page with Standard controller and extension.
//VF Page

<apex:page standardController="Candidate__c" extensions="sendEmail">
 <apex:form >
  <apex:pageBlock >
   <apex:pageBlockButtons >
    <apex:commandButton value="SendEmail" action="{!sendEmailAlert}"/>
   </apex:pageBlockButtons>
  
   <apex:pageBlockSection >
  <apex:inputField value="{!Candidate__c.US_Citizen__c}"/>
    <apex:inputField value="{!Candidate__c.Visa_Required__c}"/>
 </apex:pageBlockSection>
 </apex:pageBlock>
 
 </apex:form>
</apex:page>

//COntroller

public class sendEmail
{
public Candidate__c candidate;

    public sendEmail(ApexPages.StandardController controller)
   {
     this.candidate = (Candidate__C)controller.getRecord();
     candidate.Visa_Required__c = false;
     candidate.US_Citizen__c = false;
    
        }

public Id userId;
List<User> emailAddr;

public void sendEmail()
{

if(candidate.US_Citizen__c == true && candidate.Visa_Required__c == true)
 {
sendEmailAlert();
 }
 }

public PageReference sendEmailAlert()
{
 String addresses;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

       emailAddr =  [select Email from User u where userRole.name in('Admin','SCEO')];
       addresses = emailAddr.get(0).Email;
     for(Integer i=1;i<emailAddr.size();i++)
      {
          System.Debug('Emai numbers' +emailAddr.size());
          addresses += ':' + emailAddr.get(i).Email;
      }
             
        System.Debug('Admin EMail Address' + addresses);      
        String[] toAddresses = addresses.split(':', 0);       
        mail.setToAddresses(toAddresses);      

        mail.setHtmlBody('Test mail');

        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        return null;
       
    }
}

Wednesday, 27 November 2013

Generate XML using button click and save to the Desktop

Problem :- I had a requirement where If user click on a custom button on Account Page or any other Custom Object Detail page, XML should generate and user should be able to save that XML to their Desktop.

Solution:
1.Create a Custom button and add that page to the page layout.
2.Create a VF Page and controller as below:-

VF Page:
<apex:page standardController="Account" extensions="TestController" contentType="application/octet-stream"><?xml version="1.0" encoding="UTF-8"?>
    <MyAccount:stuff xmlns:MyAccount="http://www.bogussite.com/namespace/myNamespace">
        <apex:repeat value="{!accountName}" var="account">
            <MyAccount:Account Name="{!account.Name}"/>  
<!--            <MyAccount:Account AccountNumber="Act Name" value="{!account.AccountNumber}"/>-->
            <MyAccount:Account Phone="{!account.Phone}"/>
            <MyAccount:Account NoOfContact="{!account.noOfContacts__c}"/>    
        </apex:repeat>
    </MyAccount:stuff>
</apex:page>

Apex Controller :

public with sharing class TestController
{

public Id id{get;set;}
public Account account{get;set;}
public List<Account> accountName;
public TestController(ApexPages.StandardController controller)
{
//id = ApexPages.CurrentPage().getParameters().get('id');

}

public  List<Account> getAccountName()
{
accountName = [select Name,Phone,noOfContacts__c from Account where id = :ApexPages.currentPage().getParameters().get('id')];
System.Debug('Account Name ' +accountName + 'ID = ' +id);
return accountName;
}

}



When user will click on the button it will prompt a window asking user to save the xml.

My next requirement is to add the related List record to the root of the Parent Record, Will update the same on .....

Try it and let me know in case of any issue or any improvement

Saturday, 16 November 2013

Salesforce Dev-401 Materials

Force.com Developer Certification Handbook DEV-401 by Siddhesh Kabe.
Force.com Developer Certification study guide
Salesforce Validation Rule:-

Problem:
There are One check box and two pickList fields  on the Custom Object Page. User want to make sure if checkBox fields is selected then both the Picklist value should not be null.

Solution:-

AND((US_Citizen__c==true),
((ISPICKVAL(City__c ,'')) || (ISPICKVAL(State__c ,''))))

Here in this Solution:
 US_Citizen__c = Custom Check box
and
City__c and State__c = Custom PickList Fields.