-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
6Questions
-
2Replies
Rerender Issue
I am still relatively new to developing VisualForce pages and customer controllers. I would like to retrieve fields from a form and then pass the values to a webservice. When I click on the actionFunction, the method is executed. The form values are reset to blanks (at one time they stayed on the form). I can see the debugtxt flash up on the screen before it is reset to blanks as well. I would like for the form fieldss to not be blanked out, so I think this is a rerender issue.
VisualForce Page
Custom Controller
VisualForce Page
<apex:page controller="Product_UsersController" sidebar="false" > <apex:form > <apex:pagemessages id="errors" /> <apex:pageBlock title="Search" id="edit"> <table border="2" style="width:100%;"> <tr> <td style="font-weight:bold;width:200px;"> Find Product Users </td> <td> Filter Options: <input type="checkbox" name="chkShowRequesters" />Show Requesters <input type="checkbox" name="chkShowAdministrators"/>Show Only Administrators </td> </tr> </table> <table border="2" style="width:100%;"> <tr> <td style="width:200px;valign:top;border:1;"> <apex:pageBlock mode="edit" id="parameters"> <script type="text/javascript"> function getParameters() { try { passVariables( "SD", "MD", document.getElementById("txtFirstname").value, document.getElementById("txtLastname").value, document.getElementById("txtLoginname").value, document.getElementById("txtEmail").value, document.getElementById("txtCountry").value, document.getElementById("txtCity").value, document.getElementById("txtState").value, document.getElementById("txtZip").value); } catch(err) { alert("getParameters Error: " + err.message); } } </script> <apex:actionfunction name="passVariables" action="{!runSearch}" rerender="debug,errors"> <apex:param name="rbBusinessUnit" value="" /> <apex:param name="slProduct" value="" /> <apex:param name="txtFirstname" value="" /> <apex:param name="txtLastname" value="" /> <apex:param name="txtLoginname" value="" /> <apex:param name="txtEmail" value="" /> <apex:param name="txtCountry" value="" /> <apex:param name="txtCity" value="" /> <apex:param name="txtState" value="" /> <apex:param name="txtZip" value="" /> </apex:actionfunction> <table border="2" cellpadding="2" cellspacing="2" width="200px"> <tr> <td style="font-weight:bold;">Business<br /> <input type="radio" id="rbBusinessUnit" name="rbBusinessUnit" value="SD" checked="true" />SD <input type="radio" id="rbBusinessUnit" name="rbBusinessUnit" value="FD" />FD </td> </tr> <tr> <td style="font-weight:bold;">Product<br /> <apex:selectlist value="{!SDProducts}" id="slProduct" size="1"> <apex:selectOptions value="{!SDProducts}" /> </apex:selectlist> </td> </tr> <tr> <td style="Font-weight:bold;">First Name<br /> <input type="text" id="txtFirstname" name="txtFirstname" /> </td> </tr> <tr> <td style="Font-weight:bold;">Last Name<br /> <input type="text" id="txtLastname" name="txtLastname" /> </td> </tr> <tr> <td style="Font-weight:bold;">Login Name<br /> <input type="text" id="txtLoginname" name="txtLoginname" /> </td> </tr> <tr> <td style="Font-weight:bold;">Email<br /> <input type="text" id="txtEmail" name="txtEmail" /> </td> </tr> <tr> <td style="Font-weight:bold;">Country<br /> <input type="text" id="txtCountry" name="txtCountry" /> </td> </tr> <tr> <td style="Font-weight:bold;">City<br /> <input type="text" id="txtCity" name="txtCity" /> </td> </tr> <tr> <td style="Font-weight:bold;">State<br /> <input type="text" id="txtState" name="txtState" /> </td> </tr> <tr> <td style="Font-weight:bold;">Zip<br /> <input type="text" id="txtZip" name="txtZip" /> </td> </tr> <tr> <td> <button onclick="getParameters()">Search</button> <!-- <apex:commandButton value="Search" action="{!getParameters}" id="SearchButton" rerender="results,debug,errors" /> --> </td> </tr> </table> </apex:pageBlock> </td> </tr> </table> <apex:pageBlock title="Debug" id="debug"> <apex:outputText value="{!debugtxt}" /> </apex:pageBlock> </apex:pageBlock> </apex:form> </apex:page>
Custom Controller
public with sharing class Product_UsersController { public String debugtxt {get ; set; } public List<Product2> ProductTemp = new List<Product2>(); public string selectedProduct {get; set;} public List<SelectOption> SDProducts { get { ProductTemp = [Select p.SDProductID__c, p.Name From Product2 p where SDProductID__c > 'A' and Family like 'SD%' order by p.Name]; SDProducts = new List<SelectOption>(); SDProducts.add(new SelectOption('','<== Optional Product ==>')); for (Product2 temp : ProductTemp) { SDProducts.add(new SelectOption(temp.SDProductID__c, temp.Name)); } return SDProducts; } set; } // public List<SelectOption> FDProducts // { // get // { // ProductTemp = [Select p.SDProductID__c, p.Name From Product2 p // where SDProductID__c > 'A' and Family like 'FD%' // order by p.Name]; // FDProducts = new List<SelectOption>(); // for (Product2 temp : ProductTemp) // { // FDProducts.add(new SelectOption(temp.SDProductID__c, temp.Name)); // } // return FDProducts; // } // set; } public Product_UsersController() { // debugtxt = 'Complete search parameters and click run search to view selections'; system.debug('Entered Product_usersController'); } public PageReference runSearch() { system.debug('***Entered runSearch function'); string rbBusinessUnit = apexpages.currentPage().getParameters().get('rbBusinessUnit'); string slProduct = apexpages.currentPage().getParameters().get('slProduct'); string txtFirstname = apexpages.currentPage().getParameters().get('txtFirstname'); string txtLastname = apexpages.currentPage().getParameters().get('txtLastname'); string txtLoginname = apexpages.currentPage().getParameters().get('txtLoginname'); string txtEmail = apexpages.currentPage().getParameters().get('txtEmail'); string txtCountry = apexpages.currentPage().getParameters().get('txtCountry'); string txtCity = apexpages.currentPage().getParameters().get('txtCity'); string txtState = apexpages.currentPage().getParameters().get('txtState'); string txtZip = apexpages.currentPage().getParameters().get('txtZip'); debugtxt = 'Business Unit: ' + rbBusinessUnit; debugtxt = debugtxt + '; Product: ' + slProduct; debugtxt = debugtxt + '; First Name: ' + txtFirstname; debugtxt = debugtxt + '; Last Name: ' + txtLastname; debugtxt = debugtxt + '; Login Name: ' + txtLoginname; debugtxt = debugtxt + '; Email: ' + txtEmail; debugtxt = debugtxt + '; Country: ' + txtCountry; debugtxt = debugtxt + '; City: ' + txtCity; debugtxt = debugtxt + '; State: ' + txtState; debugtxt = debugtxt + '; Zip: ' + txtZip; system.debug('*** ' + debugtxt); return null; } }
- Jerryhnc
- November 05, 2014
- Like
- 0
Test Coverage
When I run the test below, I get a message that the List has no rows for assignment. When I manually run the query I get a single row returned.
@isTest private class AssetItemTriggerHandlerTest { static testMethod void AssetItemTriggerTest() { //getting any valid Asset Asset testAsset = [select Id, SD_Account_Number__c, Name, Status, PurchaseDate, Renewal_Date__c, AccountId from Asset where Status='Regular' limit 1]; test.startTest(); update testAsset; test.stopTest(); } }
- Jerryhnc
- July 21, 2014
- Like
- 0
Controller Test Coverage
I am new to writing test coverage and thought that I was starting with a simple code snippet. What would the test coverage look like for the following code?
public class webinarController { private List<Podcast_SD__c> podcasts; public List<Podcast_SD__c> getpodcasts() { podcasts = [Select Name, Link__c, Featured__c, Link_Name__c, Description__c, Episode_Number__c from Podcast_SD__c order by Episode_Number__c desc]; return podcasts; } private List<webinar__c> webinars; public List<webinar__c> getwebinars() { webinars = [Select Name, Link__c, Featured__c, Link_Type__c, Title__c, Type__c, Year_QTR__c from Webinar__c order by Name asc]; return webinars; }
- Jerryhnc
- July 10, 2014
- Like
- 0
- Jerryhnc
- July 08, 2014
- Like
- 0
Deleting Apex Class Problem
I am using the Force.IDE to modify the xml file for a class and change the status to Deleted in my production instance. I save the file locally and select Force.com> Save to Server. During the save process I can see the xml file change itself back to Active. I have been successful in doing this for other classes, but this one is not working. The class is listed below.
global class ChatterEmailHandler implements Messaging.InboundEmailHandler { global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) { //create result for email operation Messaging.InboundEmailResult result = new Messaging.InboundEmailresult(); try{ User user = [select id from User where email = :email.fromAddress]; Feedpost f = new Feedpost(); f.ParentId = user.id; String bodytext; if ( email.plainTextBody != null) bodytext = email.plainTextBody; String subjecttext = email.subject; String match; Integer s; Integer l; String query; String errormessage; if (subjecttext.lastindexof('#account') != -1) { s = subjecttext.lastindexof('#account'); s += 9; l = subjecttext.length(); match = subjecttext.substring(s,l); s = s - 9; subjecttext = subjecttext.substring (0,s); match = match.trim(); query = 'select Id from Account where name = \'' + match + '\' limit 1'; Account a = new Account(); try { a = Database.query(query); f.ParentId = a.Id; f.Type = 'TextPost'; } catch (Exception e) { errormessage ='Sorry, your chatter status has not been set. We could not find an account named: '+match; } } if (subjecttext.lastindexof('#contact') != -1) { s = subjecttext.lastindexof('#contact'); s += 9; l = subjecttext.length(); match = subjecttext.substring(s,l); s = s - 9; subjecttext = subjecttext.substring (0,s); match = match.trim(); Contact c = new Contact(); try { c = [Select Id from Contact where email = :match limit 1]; f.ParentId = c.Id; f.Type = 'TextPost'; } catch (Exception e) { errormessage ='Sorry, your chatter status has not been set. We could not find a contact with email address: '+match; } } if (subjecttext.lastindexof('#opportunity') != -1) { s = subjecttext.lastindexof('#opportunity'); s += 13; l = subjecttext.length(); match = subjecttext.substring(s,l); s = s - 13; subjecttext = subjecttext.substring (0,s); match = match.trim(); query = 'select Id from Opportunity where name = \'' + match + '\' limit 1'; Opportunity o = new Opportunity(); try { o = Database.query(query); f.ParentId = o.Id; f.Type = 'TextPost'; } catch (Exception e) { errormessage ='Sorry, your chatter status has not been set. We could not find an opportunity named: '+match; } } if (subjecttext.lastindexof('#case') != -1) { s = subjecttext.lastindexof('#case'); s += 6; l = subjecttext.length(); match = subjecttext.substring(s,l); s = s - 6; subjecttext = subjecttext.substring (0,s); Case ca = new Case(); try { ca = [Select Id from Case where CaseNumber = :match limit 1]; f.ParentId = ca.Id; f.Type = 'TextPost'; } catch (Exception e) { errormessage ='Sorry, your chatter status has not been set. We could not find case number: '+match; } } if (email.binaryAttachments != null) { f.body = subjecttext; f.Type = 'ContentPost'; f.ContentData = email.binaryAttachments[0].body; f.ContentFileName = email.binaryAttachments[0].filename; if (bodytext.length() > 1000) { f.ContentDescription = bodytext.substring (0,1000); } else { f.ContentDescription = email.plainTextBody; } } else { if (bodytext != null) bodytext= subjecttext +'\n\n'+bodytext; else bodytext= subjecttext; if (bodytext.length() > 1000) { f.body = bodytext.substring (0,1000); } else { f.body = bodytext; } } if (errormessage == null) { insert f; //I'm saying that it worked, and that a new status was created. result.message='Your Chatter status has been successfully set to: ' + f.body; result.success = true; return result; } else { result.message=errormessage; result.success = true; return result; } } catch (Exception e) { result.message='Sorry, your Chatter status could not be set. Please make sure you are sending from the email address associated with your salesforce login.'; result.success = true; return result; } } static testMethod void testChatterEmailHandler1(){ // Create a new email and envelope object Messaging.InboundEmail email = new Messaging.InboundEmail() ; Messaging.InboundEnvelope env = new Messaging.InboundEnvelope(); String userName = UserInfo.getUserName(); User activeUser = [Select Email From User where Username = :userName limit 1]; String userEmail = activeUser.Email; // Create a new test Account and insert it in the Test Method Account a = new account(Name='Test Account'); insert a; // Create a new test Contact and insert it in the Test Method Contact c = new Contact( account = a, lastName='Test Contact', Email='testc@mailop.com'); insert c; // test with subject that matches the unsubscribe statement email.subject = 'test #contact testc@mailop.com'; email.fromAddress = userEmail; // call the class and test it with the data in the testMethod ChatterEmailHandler ChatterEmailHandlerTest1 = new ChatterEmailHandler(); Test.starttest(); ChatterEmailHandlerTest1.handleInboundEmail(email, env ); Test.stoptest(); /*List<ContactFeed> posts = [select FeedPost.Id, FeedPost.Body, FeedPost.Title from ContactFeed where ParentId =:c.Id Order By CreatedDate DESC]; ContactFeed updatedPost = posts[0]; System.assertEquals(updatedPost.FeedPost.Body, 'test'); */ } static testMethod void testChatterEmailHandler2(){ // Create a new email and envelope object Messaging.InboundEmail email = new Messaging.InboundEmail() ; Messaging.InboundEnvelope env = new Messaging.InboundEnvelope(); String userName = UserInfo.getUserName(); User activeUser = [Select Email From User where Username = :userName limit 1]; String userEmail = activeUser.Email; // Create a new test Case and insert it in the Test Method Case ca = new Case( Status = 'new', Origin ='Phone' ); insert ca; Case testc = [Select case.CaseNumber from Case where case.Id = :ca.Id]; email.subject = 'test2 #case '+testc.CaseNumber; email.fromAddress = userEmail; // call the class and test it with the data in the testMethod ChatterEmailHandler ChatterEmailHandlerTest2 = new ChatterEmailHandler(); Test.starttest(); ChatterEmailHandlerTest2.handleInboundEmail(email, env ); Test.stoptest(); /* List<CaseFeed> posts = [select FeedPost.Id, FeedPost.Body, FeedPost.Title from CaseFeed where ParentId =:ca.Id Order By CreatedDate DESC]; CaseFeed updatedPost2 = posts[0]; System.assertEquals('test2', updatedPost2.FeedPost.Body); */ } static testMethod void testChatterEmailHandler3(){ // Create a new email and envelope object Messaging.InboundEmail email = new Messaging.InboundEmail() ; Messaging.InboundEnvelope env = new Messaging.InboundEnvelope(); String userName = UserInfo.getUserName(); User activeUser = [Select Email From User where Username = :userName limit 1]; String userEmail = activeUser.Email; // Create a new test Account and insert it in the Test Method Account a = new account(Name='Test Account'); insert a; // Create a new test Opportunity and insert it in the Test Method Opportunity o = new Opportunity( account = a, Name='Test Opportunity', CloseDate=Date.today(), StageName='Prospecting'); insert o; // test with subject that matches the unsubscribe statement email.subject = 'test3 #opportunity Test Opportunity'; email.fromAddress = userEmail; // call the class and test it with the data in the testMethod ChatterEmailHandler ChatterEmailHandlerTest3 = new ChatterEmailHandler(); Test.starttest(); ChatterEmailHandlerTest3.handleInboundEmail(email, env ); Test.stoptest(); /*List<OpportunityFeed> posts = [select FeedPost.Id, FeedPost.Body, FeedPost.Title from OpportunityFeed where ParentId =:o.Id Order By CreatedDate DESC]; OpportunityFeed updatedPost3 = posts[0]; System.assertEquals('test3', updatedPost3.FeedPost.Body); */ } static testMethod void testChatterEmailHandler4(){ // Create a new email and envelope object Messaging.InboundEmail email = new Messaging.InboundEmail() ; Messaging.InboundEnvelope env = new Messaging.InboundEnvelope(); String userName = UserInfo.getUserName(); User activeUser = [Select Email From User where Username = :userName limit 1]; String userEmail = activeUser.Email; // Create a new test Account and insert it in the Test Method Account a = new account(Name='Test Account'); insert a; // test with subject that matches the account email.subject = 'test4 #account Test Account'; email.fromAddress = userEmail; // call the class and test it with the data in the testMethod ChatterEmailHandler ChatterEmailHandlerTest4 = new ChatterEmailHandler(); Test.starttest(); ChatterEmailHandlerTest4.handleInboundEmail(email, env ); Test.stoptest(); /*List<AccountFeed> posts = [select FeedPost.Id, FeedPost.Body, FeedPost.Title from AccountFeed where ParentId =:a.Id Order By CreatedDate DESC]; AccountFeed updatedPost = posts[0]; System.assertEquals(updatedPost.FeedPost.Body, 'test4'); */ } }
- Jerryhnc
- July 08, 2014
- Like
- 0
Global Search Customization
We've got a local application that provides and stores user credentials. We use SalesForce as our CRM and support tool. We have created an API that will essentially provide a SOAP interface to get a list of users from our local application. I'm pretty confident we can build that table display and user list page.
However, what would be icing on the cake would be to make the list of users searchable from the SF global search. It is my understanding that you have to have custom objects (tables) created so that SF can search them. In our scenario we wouldn't actually have a true custom object as we load the data from an external API on the page.
Is it possible to include external API call result sets in the SF global search tool?
- Jerryhnc
- August 29, 2013
- Like
- 0
Rerender Issue
I am still relatively new to developing VisualForce pages and customer controllers. I would like to retrieve fields from a form and then pass the values to a webservice. When I click on the actionFunction, the method is executed. The form values are reset to blanks (at one time they stayed on the form). I can see the debugtxt flash up on the screen before it is reset to blanks as well. I would like for the form fieldss to not be blanked out, so I think this is a rerender issue.
VisualForce Page
Custom Controller
VisualForce Page
<apex:page controller="Product_UsersController" sidebar="false" > <apex:form > <apex:pagemessages id="errors" /> <apex:pageBlock title="Search" id="edit"> <table border="2" style="width:100%;"> <tr> <td style="font-weight:bold;width:200px;"> Find Product Users </td> <td> Filter Options: <input type="checkbox" name="chkShowRequesters" />Show Requesters <input type="checkbox" name="chkShowAdministrators"/>Show Only Administrators </td> </tr> </table> <table border="2" style="width:100%;"> <tr> <td style="width:200px;valign:top;border:1;"> <apex:pageBlock mode="edit" id="parameters"> <script type="text/javascript"> function getParameters() { try { passVariables( "SD", "MD", document.getElementById("txtFirstname").value, document.getElementById("txtLastname").value, document.getElementById("txtLoginname").value, document.getElementById("txtEmail").value, document.getElementById("txtCountry").value, document.getElementById("txtCity").value, document.getElementById("txtState").value, document.getElementById("txtZip").value); } catch(err) { alert("getParameters Error: " + err.message); } } </script> <apex:actionfunction name="passVariables" action="{!runSearch}" rerender="debug,errors"> <apex:param name="rbBusinessUnit" value="" /> <apex:param name="slProduct" value="" /> <apex:param name="txtFirstname" value="" /> <apex:param name="txtLastname" value="" /> <apex:param name="txtLoginname" value="" /> <apex:param name="txtEmail" value="" /> <apex:param name="txtCountry" value="" /> <apex:param name="txtCity" value="" /> <apex:param name="txtState" value="" /> <apex:param name="txtZip" value="" /> </apex:actionfunction> <table border="2" cellpadding="2" cellspacing="2" width="200px"> <tr> <td style="font-weight:bold;">Business<br /> <input type="radio" id="rbBusinessUnit" name="rbBusinessUnit" value="SD" checked="true" />SD <input type="radio" id="rbBusinessUnit" name="rbBusinessUnit" value="FD" />FD </td> </tr> <tr> <td style="font-weight:bold;">Product<br /> <apex:selectlist value="{!SDProducts}" id="slProduct" size="1"> <apex:selectOptions value="{!SDProducts}" /> </apex:selectlist> </td> </tr> <tr> <td style="Font-weight:bold;">First Name<br /> <input type="text" id="txtFirstname" name="txtFirstname" /> </td> </tr> <tr> <td style="Font-weight:bold;">Last Name<br /> <input type="text" id="txtLastname" name="txtLastname" /> </td> </tr> <tr> <td style="Font-weight:bold;">Login Name<br /> <input type="text" id="txtLoginname" name="txtLoginname" /> </td> </tr> <tr> <td style="Font-weight:bold;">Email<br /> <input type="text" id="txtEmail" name="txtEmail" /> </td> </tr> <tr> <td style="Font-weight:bold;">Country<br /> <input type="text" id="txtCountry" name="txtCountry" /> </td> </tr> <tr> <td style="Font-weight:bold;">City<br /> <input type="text" id="txtCity" name="txtCity" /> </td> </tr> <tr> <td style="Font-weight:bold;">State<br /> <input type="text" id="txtState" name="txtState" /> </td> </tr> <tr> <td style="Font-weight:bold;">Zip<br /> <input type="text" id="txtZip" name="txtZip" /> </td> </tr> <tr> <td> <button onclick="getParameters()">Search</button> <!-- <apex:commandButton value="Search" action="{!getParameters}" id="SearchButton" rerender="results,debug,errors" /> --> </td> </tr> </table> </apex:pageBlock> </td> </tr> </table> <apex:pageBlock title="Debug" id="debug"> <apex:outputText value="{!debugtxt}" /> </apex:pageBlock> </apex:pageBlock> </apex:form> </apex:page>
Custom Controller
public with sharing class Product_UsersController { public String debugtxt {get ; set; } public List<Product2> ProductTemp = new List<Product2>(); public string selectedProduct {get; set;} public List<SelectOption> SDProducts { get { ProductTemp = [Select p.SDProductID__c, p.Name From Product2 p where SDProductID__c > 'A' and Family like 'SD%' order by p.Name]; SDProducts = new List<SelectOption>(); SDProducts.add(new SelectOption('','<== Optional Product ==>')); for (Product2 temp : ProductTemp) { SDProducts.add(new SelectOption(temp.SDProductID__c, temp.Name)); } return SDProducts; } set; } // public List<SelectOption> FDProducts // { // get // { // ProductTemp = [Select p.SDProductID__c, p.Name From Product2 p // where SDProductID__c > 'A' and Family like 'FD%' // order by p.Name]; // FDProducts = new List<SelectOption>(); // for (Product2 temp : ProductTemp) // { // FDProducts.add(new SelectOption(temp.SDProductID__c, temp.Name)); // } // return FDProducts; // } // set; } public Product_UsersController() { // debugtxt = 'Complete search parameters and click run search to view selections'; system.debug('Entered Product_usersController'); } public PageReference runSearch() { system.debug('***Entered runSearch function'); string rbBusinessUnit = apexpages.currentPage().getParameters().get('rbBusinessUnit'); string slProduct = apexpages.currentPage().getParameters().get('slProduct'); string txtFirstname = apexpages.currentPage().getParameters().get('txtFirstname'); string txtLastname = apexpages.currentPage().getParameters().get('txtLastname'); string txtLoginname = apexpages.currentPage().getParameters().get('txtLoginname'); string txtEmail = apexpages.currentPage().getParameters().get('txtEmail'); string txtCountry = apexpages.currentPage().getParameters().get('txtCountry'); string txtCity = apexpages.currentPage().getParameters().get('txtCity'); string txtState = apexpages.currentPage().getParameters().get('txtState'); string txtZip = apexpages.currentPage().getParameters().get('txtZip'); debugtxt = 'Business Unit: ' + rbBusinessUnit; debugtxt = debugtxt + '; Product: ' + slProduct; debugtxt = debugtxt + '; First Name: ' + txtFirstname; debugtxt = debugtxt + '; Last Name: ' + txtLastname; debugtxt = debugtxt + '; Login Name: ' + txtLoginname; debugtxt = debugtxt + '; Email: ' + txtEmail; debugtxt = debugtxt + '; Country: ' + txtCountry; debugtxt = debugtxt + '; City: ' + txtCity; debugtxt = debugtxt + '; State: ' + txtState; debugtxt = debugtxt + '; Zip: ' + txtZip; system.debug('*** ' + debugtxt); return null; } }
- Jerryhnc
- November 05, 2014
- Like
- 0
Deleting Apex Class Problem
I am using the Force.IDE to modify the xml file for a class and change the status to Deleted in my production instance. I save the file locally and select Force.com> Save to Server. During the save process I can see the xml file change itself back to Active. I have been successful in doing this for other classes, but this one is not working. The class is listed below.
global class ChatterEmailHandler implements Messaging.InboundEmailHandler { global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) { //create result for email operation Messaging.InboundEmailResult result = new Messaging.InboundEmailresult(); try{ User user = [select id from User where email = :email.fromAddress]; Feedpost f = new Feedpost(); f.ParentId = user.id; String bodytext; if ( email.plainTextBody != null) bodytext = email.plainTextBody; String subjecttext = email.subject; String match; Integer s; Integer l; String query; String errormessage; if (subjecttext.lastindexof('#account') != -1) { s = subjecttext.lastindexof('#account'); s += 9; l = subjecttext.length(); match = subjecttext.substring(s,l); s = s - 9; subjecttext = subjecttext.substring (0,s); match = match.trim(); query = 'select Id from Account where name = \'' + match + '\' limit 1'; Account a = new Account(); try { a = Database.query(query); f.ParentId = a.Id; f.Type = 'TextPost'; } catch (Exception e) { errormessage ='Sorry, your chatter status has not been set. We could not find an account named: '+match; } } if (subjecttext.lastindexof('#contact') != -1) { s = subjecttext.lastindexof('#contact'); s += 9; l = subjecttext.length(); match = subjecttext.substring(s,l); s = s - 9; subjecttext = subjecttext.substring (0,s); match = match.trim(); Contact c = new Contact(); try { c = [Select Id from Contact where email = :match limit 1]; f.ParentId = c.Id; f.Type = 'TextPost'; } catch (Exception e) { errormessage ='Sorry, your chatter status has not been set. We could not find a contact with email address: '+match; } } if (subjecttext.lastindexof('#opportunity') != -1) { s = subjecttext.lastindexof('#opportunity'); s += 13; l = subjecttext.length(); match = subjecttext.substring(s,l); s = s - 13; subjecttext = subjecttext.substring (0,s); match = match.trim(); query = 'select Id from Opportunity where name = \'' + match + '\' limit 1'; Opportunity o = new Opportunity(); try { o = Database.query(query); f.ParentId = o.Id; f.Type = 'TextPost'; } catch (Exception e) { errormessage ='Sorry, your chatter status has not been set. We could not find an opportunity named: '+match; } } if (subjecttext.lastindexof('#case') != -1) { s = subjecttext.lastindexof('#case'); s += 6; l = subjecttext.length(); match = subjecttext.substring(s,l); s = s - 6; subjecttext = subjecttext.substring (0,s); Case ca = new Case(); try { ca = [Select Id from Case where CaseNumber = :match limit 1]; f.ParentId = ca.Id; f.Type = 'TextPost'; } catch (Exception e) { errormessage ='Sorry, your chatter status has not been set. We could not find case number: '+match; } } if (email.binaryAttachments != null) { f.body = subjecttext; f.Type = 'ContentPost'; f.ContentData = email.binaryAttachments[0].body; f.ContentFileName = email.binaryAttachments[0].filename; if (bodytext.length() > 1000) { f.ContentDescription = bodytext.substring (0,1000); } else { f.ContentDescription = email.plainTextBody; } } else { if (bodytext != null) bodytext= subjecttext +'\n\n'+bodytext; else bodytext= subjecttext; if (bodytext.length() > 1000) { f.body = bodytext.substring (0,1000); } else { f.body = bodytext; } } if (errormessage == null) { insert f; //I'm saying that it worked, and that a new status was created. result.message='Your Chatter status has been successfully set to: ' + f.body; result.success = true; return result; } else { result.message=errormessage; result.success = true; return result; } } catch (Exception e) { result.message='Sorry, your Chatter status could not be set. Please make sure you are sending from the email address associated with your salesforce login.'; result.success = true; return result; } } static testMethod void testChatterEmailHandler1(){ // Create a new email and envelope object Messaging.InboundEmail email = new Messaging.InboundEmail() ; Messaging.InboundEnvelope env = new Messaging.InboundEnvelope(); String userName = UserInfo.getUserName(); User activeUser = [Select Email From User where Username = :userName limit 1]; String userEmail = activeUser.Email; // Create a new test Account and insert it in the Test Method Account a = new account(Name='Test Account'); insert a; // Create a new test Contact and insert it in the Test Method Contact c = new Contact( account = a, lastName='Test Contact', Email='testc@mailop.com'); insert c; // test with subject that matches the unsubscribe statement email.subject = 'test #contact testc@mailop.com'; email.fromAddress = userEmail; // call the class and test it with the data in the testMethod ChatterEmailHandler ChatterEmailHandlerTest1 = new ChatterEmailHandler(); Test.starttest(); ChatterEmailHandlerTest1.handleInboundEmail(email, env ); Test.stoptest(); /*List<ContactFeed> posts = [select FeedPost.Id, FeedPost.Body, FeedPost.Title from ContactFeed where ParentId =:c.Id Order By CreatedDate DESC]; ContactFeed updatedPost = posts[0]; System.assertEquals(updatedPost.FeedPost.Body, 'test'); */ } static testMethod void testChatterEmailHandler2(){ // Create a new email and envelope object Messaging.InboundEmail email = new Messaging.InboundEmail() ; Messaging.InboundEnvelope env = new Messaging.InboundEnvelope(); String userName = UserInfo.getUserName(); User activeUser = [Select Email From User where Username = :userName limit 1]; String userEmail = activeUser.Email; // Create a new test Case and insert it in the Test Method Case ca = new Case( Status = 'new', Origin ='Phone' ); insert ca; Case testc = [Select case.CaseNumber from Case where case.Id = :ca.Id]; email.subject = 'test2 #case '+testc.CaseNumber; email.fromAddress = userEmail; // call the class and test it with the data in the testMethod ChatterEmailHandler ChatterEmailHandlerTest2 = new ChatterEmailHandler(); Test.starttest(); ChatterEmailHandlerTest2.handleInboundEmail(email, env ); Test.stoptest(); /* List<CaseFeed> posts = [select FeedPost.Id, FeedPost.Body, FeedPost.Title from CaseFeed where ParentId =:ca.Id Order By CreatedDate DESC]; CaseFeed updatedPost2 = posts[0]; System.assertEquals('test2', updatedPost2.FeedPost.Body); */ } static testMethod void testChatterEmailHandler3(){ // Create a new email and envelope object Messaging.InboundEmail email = new Messaging.InboundEmail() ; Messaging.InboundEnvelope env = new Messaging.InboundEnvelope(); String userName = UserInfo.getUserName(); User activeUser = [Select Email From User where Username = :userName limit 1]; String userEmail = activeUser.Email; // Create a new test Account and insert it in the Test Method Account a = new account(Name='Test Account'); insert a; // Create a new test Opportunity and insert it in the Test Method Opportunity o = new Opportunity( account = a, Name='Test Opportunity', CloseDate=Date.today(), StageName='Prospecting'); insert o; // test with subject that matches the unsubscribe statement email.subject = 'test3 #opportunity Test Opportunity'; email.fromAddress = userEmail; // call the class and test it with the data in the testMethod ChatterEmailHandler ChatterEmailHandlerTest3 = new ChatterEmailHandler(); Test.starttest(); ChatterEmailHandlerTest3.handleInboundEmail(email, env ); Test.stoptest(); /*List<OpportunityFeed> posts = [select FeedPost.Id, FeedPost.Body, FeedPost.Title from OpportunityFeed where ParentId =:o.Id Order By CreatedDate DESC]; OpportunityFeed updatedPost3 = posts[0]; System.assertEquals('test3', updatedPost3.FeedPost.Body); */ } static testMethod void testChatterEmailHandler4(){ // Create a new email and envelope object Messaging.InboundEmail email = new Messaging.InboundEmail() ; Messaging.InboundEnvelope env = new Messaging.InboundEnvelope(); String userName = UserInfo.getUserName(); User activeUser = [Select Email From User where Username = :userName limit 1]; String userEmail = activeUser.Email; // Create a new test Account and insert it in the Test Method Account a = new account(Name='Test Account'); insert a; // test with subject that matches the account email.subject = 'test4 #account Test Account'; email.fromAddress = userEmail; // call the class and test it with the data in the testMethod ChatterEmailHandler ChatterEmailHandlerTest4 = new ChatterEmailHandler(); Test.starttest(); ChatterEmailHandlerTest4.handleInboundEmail(email, env ); Test.stoptest(); /*List<AccountFeed> posts = [select FeedPost.Id, FeedPost.Body, FeedPost.Title from AccountFeed where ParentId =:a.Id Order By CreatedDate DESC]; AccountFeed updatedPost = posts[0]; System.assertEquals(updatedPost.FeedPost.Body, 'test4'); */ } }
- Jerryhnc
- July 08, 2014
- Like
- 0