+ Start a Discussion
Gavin Britto 1Gavin Britto 1 

Send email using Apex

Hello to all the Developers out there! Our University is slowly but surely moving all our staff on to salesforce. However, utill then we are looking to get rid of the paper based system access request process.

1. Currently a new staff member will fill out a Access request paper form and checks off various directories and systems that he requires access to do his/her job.

2. That request is approved by their supervisor and then the paper form is sent to the System Administrators to provision the access.

Now the System Administrators are on salesforce but the supervisors are Not on Salesforce yet.

What I am temporarily trying to do:
 
  •  I tried building an approval process and found out that since the Supervisors are not in Salesforce, they are unable to Approve the Request (They require API enabled User Profiles). Even though, the System Administrators can receive the request, they can't provsion the access until the Supervisor approves the request.
So I developed a System Access Request form on a Public Salesforce site page to submit systems access requests. I want to email The data from the system request form to a supervisor who is outside of salesforce. ( I feel this is way better than walking over to his desk with a sheet of paper). The System Administrators can go ahead and provision the access once they get a call or email from the supervisor approving the electronic access request form. I can mention in the body of the email to contact the system admin at the following email address.

The Visualforce page works fine. However, I need help with the Apex trigger. Maybe a trigger is not the right choice. Any help on having the details from the visualforce form sent to the Supervisor_Email__c would be helpful. 

I started on the Apex Trigger. Feel free to demolish it if neccesary. Visualforce page below.
Trigger SupervisorNotification on SCS_Systems_Access_request ( After Insert){
For( SCS_System_Access_Request SysRequest:Trigger.new){

//initialize outbound email message service
	//Outbound Email Messaging service - single email messaging	
	
	Messaging.SingleEmailMessage = new messaging.SingleEmailMessage();
	
	
	String[] = sendingTo=new String[]{'Supervisor@georgetown.edu'};
		semail.setToAddress(sendingTo);
	
	//The Email Subject would say
		semail.setSubject('Action Required: Access Request Approval');
	
	//Body of the email would say
	semail.setPlainTextBody('Access to the following systems is requested')
	
	//merge fields from SCS System Access Request go here
	
	//send email to supervisor
	Messaging.sendEmail(new Messaging.SingleEmailMessage[] {semail});


}

}

VF Page (works fine)
ex:page standardController="SCS_System_Access_Request__c"
           standardStylesheets="true"
    showHeader="false" sidebar="false" cache="false"
    >
    
  
 <head>
    <title>
      GU SCS System Access Request Form
    </title>
    
    <title>
    Confidentiality Agreement
    </title>
    
    <apex:stylesheet value="{!$Resource.POMCSSMAIN}"/> 
  </head>
  <body>
  
  <p><strong class="logo"><a href="http://www.georgetown.edu/">Georgetown University</a></strong></p>
  
  
 
  
  <apex:form >
      <apex:pageBlock title="System Access Request Form">
          Fields indicated by red bar are required.
          <apex:pageMessages />
              <apex:pageBlockButtons >
                  <apex:commandButton value="Submit" action="{!save}"/>                  
                  <apex:commandButton value="Cancel" action="{!cancel}"/>
              </apex:pageBlockButtons>

              <apex:pageBlockSection title="Personal Information" >
                  <apex:pageBlockSection columns="1">
                      <apex:pageBlockSectionItem > 
                          <apex:outputLabel >Data Security Quiz Score</apex:outputLabel>
                          <apex:inputField value="{!SCS_System_Access_Request__c.Data_Security_Quiz_score__c}" required="true" />                                     
                      </apex:pageblockSectionItem>
                      
                      
                      <apex:inputField value="{!SCS_System_Access_Request__c.NetID__c}" required="true" />
                      <apex:inputField value="{!SCS_System_Access_Request__c.Email__c}" required="true" />
                      
                      <apex:inputField value="{!SCS_System_Access_Request__c.VPN_Access_Required__c}" required="true" />
                      
                      
                      <apex:inputField value="{!SCS_System_Access_Request__c.Department__c}" required="true" />
                      
                      
                      <apex:inputField value="{!SCS_System_Access_Request__c.Supervisor__c}" required="true" />
                      
                      
                      <apex:inputField value="{!SCS_System_Access_Request__c.Phone_Number__c}" required="true" />
                      
                      
                      <apex:inputField value="{!SCS_System_Access_Request__c.Systems_you_need_access_to__c}" required="true" />
                      
                 <p>
                 test
                 </p>
                 <apex:inputField value="{!SCS_System_Access_Request__c.Confidentiality_Agreement__c}" required="true" />
                                               
                                                           
                  </apex:pageBlockSection>  
                  
                 
                            
             
                      
                </apex:pageBlockSection>  
              
              
      </apex:pageBlock>     
  </apex:form>
  </body>
  
</apex:page>

Thank you in advance!

Sincerely,

Gavin Britto
Best Answer chosen by Gavin Britto 1
Roshni RahulRoshni Rahul
Hi Gavin,

Please check this out,

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        List<String> emails =new List<String>();
        emails.add(a.Email__c);
        List<String> ccemails =new List<String>();
        ccemails.add(a.owner.Email);
        mail.setToAddresses(emails );
       mail.setCcAddresses(ccemails);
       
        mail.setReplyTo(a.owner.Email);
        OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address =: Label.Fifteen_Day_Email_OWA];
        if ( owea.size() > 0 ) 
        {
        mail.setOrgWideEmailAddressId(owea.get(0).Id);
        }
       else
        {
        mail.setSenderDisplayName(a.owner.Name);
        }
        mail.setSubject('Harmar :: Lead Follow-Up');
        String messageBodyfortask;
        messageBody ='<html><body>';
        messageBodyfortask='';
        messageBody =messageBody +'<div><img src="'+Label.Logourl+'" alt="Mountain View" style="width:304px;height:228px;"></div><br/><br/>';
        messageBody=messageBody+ 'Hi,<br/>';  
        messageBodyfortask=messageBodyfortask+'To: '+a.Email__c+ '\n\n';
        messageBodyfortask=messageBodyfortask+'Hello\n';
messageBody=messageBody+tablebdy+'</table>';
       messageBodyfortask= messageBodyfortask+'\n'+tablebdyfortask+'\n\n Thank you so much! \n\n Continued success, \n\n ';
       messageBody= messageBody+'<br/>Thank you!<br/><br/>Continued success,<br/><br/>Happy<br/>';
        messageBody=messageBody+' </body></html>';
        mail.setHtmlBody(messageBody);
        mail.setUseSignature(false);
 Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
 Messaging.SendEmailResult[] results = Messaging.sendEmail( theEmails);

Hope it works..
Regards
Roshni

All Answers

Roshni RahulRoshni Rahul
Hi Gavin,

Please check this out,

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        List<String> emails =new List<String>();
        emails.add(a.Email__c);
        List<String> ccemails =new List<String>();
        ccemails.add(a.owner.Email);
        mail.setToAddresses(emails );
       mail.setCcAddresses(ccemails);
       
        mail.setReplyTo(a.owner.Email);
        OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address =: Label.Fifteen_Day_Email_OWA];
        if ( owea.size() > 0 ) 
        {
        mail.setOrgWideEmailAddressId(owea.get(0).Id);
        }
       else
        {
        mail.setSenderDisplayName(a.owner.Name);
        }
        mail.setSubject('Harmar :: Lead Follow-Up');
        String messageBodyfortask;
        messageBody ='<html><body>';
        messageBodyfortask='';
        messageBody =messageBody +'<div><img src="'+Label.Logourl+'" alt="Mountain View" style="width:304px;height:228px;"></div><br/><br/>';
        messageBody=messageBody+ 'Hi,<br/>';  
        messageBodyfortask=messageBodyfortask+'To: '+a.Email__c+ '\n\n';
        messageBodyfortask=messageBodyfortask+'Hello\n';
messageBody=messageBody+tablebdy+'</table>';
       messageBodyfortask= messageBodyfortask+'\n'+tablebdyfortask+'\n\n Thank you so much! \n\n Continued success, \n\n ';
       messageBody= messageBody+'<br/>Thank you!<br/><br/>Continued success,<br/><br/>Happy<br/>';
        messageBody=messageBody+' </body></html>';
        mail.setHtmlBody(messageBody);
        mail.setUseSignature(false);
 Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
 Messaging.SendEmailResult[] results = Messaging.sendEmail( theEmails);

Hope it works..
Regards
Roshni
This was selected as the best answer
Gavin Britto 1Gavin Britto 1
Hi Roshni,

You are a genious! Thank you so much! I was able to customize this to get my notification to work! 
Roshni RahulRoshni Rahul
Hi,
If working fine, can you please mark my answer as best answer.
Gavin Britto 1Gavin Britto 1
I must've mistakenly un-selected it. Thank you Roshni!
Gavin Britto 1Gavin Britto 1
I must've un-selected it by mistake or salesforce must be doing some maintenance. I got a 503 error last time I wrote a post. I am now responding via my inbox. Its selected as the best answer now. If it gets un-selected, let me know. I will select it as the best answer again. Thank you Roshni!
Roshni RahulRoshni Rahul
Thank you Gavin. :)