• Bial
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 9
    Replies

Hi.

 
I am writing test code for trigger in case for sending email to contacts of case by updating or inserting any case field. But i am facing problem in it. Please help me to sort out this issue. Actually I wrote the following trigger and test code, also the error is given below.
 
Trigger Code
 
trigger sendemaildemo on Case (after insert, after update) {
 
final String template = 'Test';
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
 
Messaging.SingleEmailMessage message1 = new Messaging.SingleEmailMessage();
 
Messaging.SingleEmailMessage message2 = new Messaging.SingleEmailMessage();
 
Messaging.SingleEmailMessage message3 = new Messaging.SingleEmailMessage();
 
Messaging.SingleEmailMessage message4 = new Messaging.SingleEmailMessage();
 
 
 
message.setTemplateId([select id from EmailTemplate where Name =:template].id);
 
message1.setTemplateId([select id from EmailTemplate where Name =:template].id);
 
message2.setTemplateId([select id from EmailTemplate where Name =:template].id);
 
message3.setTemplateId([select id from EmailTemplate where Name =:template].id);
 
message4.setTemplateId([select id from EmailTemplate where Name =:template].id);
 
 
 
 
 
// Send single mail to contact of each updated case
for (Case uc: Trigger.new) {
 
Contact c = [select Email from Contact where Id =:uc.ContactId and Email!=null];
 
message.setTargetObjectId(c.Id);
message.setWhatId(uc.Id);
 
message.setToAddresses(new String[] {c.Email});
Messaging.sendEmail(new Messaging.Email[] {message});
}
 
for (Case vc: Trigger.new) {
IF (vc.Contactemail__c!=null){
Contact v = [select Email from Contact where Id =:vc.Contact__c and Email != null];
 
message1.setTargetObjectId(v.Id);
message1.setWhatId(vc.Id);
 
message1.setToAddresses(new String[] {v.Email});
Messaging.sendEmail(new Messaging.Email[] {message1});
}
 IF (vc.Contact2email__c!=null) {
Contact b = [select Email from Contact where Id =:vc.Contact2__c and Email != null];
 
message2.setTargetObjectId(b.Id);
message2.setWhatId(vc.Id);
 
message2.setToAddresses(new String[] {b.Email});
Messaging.sendEmail(new Messaging.Email[] {message2});
 
 
}
 IF (vc.Contact3email__c!=null) {
Contact d = [select Email from Contact where Id =:vc.Contact3__c and Email != null];
 
message3.setTargetObjectId(d.Id); 
message3.setWhatId(vc.Id);
 
message3.setToAddresses(new String[] {d.Email});
Messaging.sendEmail(new Messaging.Email[] {message3});
 
}
 
 
IF (vc.Contact4email__c!=null) {
Contact f = [select Email from Contact where Id =:vc.Contact4__c and Email != null];
 
message4.setTargetObjectId(f.Id);
message4.setWhatId(vc.Id);
 
message4.setToAddresses(new String[] {f.Email});
Messaging.sendEmail(new Messaging.Email[] {message4});
}
 
else {
}
 
}
}
 
 
Test Code.
 
@isTest
private class sendemaildemo {
 
    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        Case caset = new Case ();
        caset.Origin = '2F5008000000IEE2g';
        caset.Status = 'New';
        caset.Origin = 'CheseTek'; 
        caset.ContactId = '0038000001Doy5g';
        caset.Contact__c = '0038000001Doy5g';
        caset.Contact2__c = '0038000001Doy5g';
caset.Contact3__c = '0038000001Doy5g';
caset.Contact4__c = '0038000001Doy5g';
 
insert caset;
 
 
 
 
    }
}
 
 
Error

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, sendemaildemo: execution of AfterInsert

caused by: System.QueryException: List has no rows for assignment to SObject

Trigger.sendemaildemo: line 16, column 1: []

 
 
Thanks in Advance!
  • November 01, 2012
  • Like
  • 0

Hey all

I want to select multiple contacts in case via lookup field. I need your help in this problem 

Thanks in advance!

  • September 13, 2012
  • Like
  • 0

I want to use the Rest API for salesforce in my developer organization. for testing i use localhost (documentaion describes that localhost works just fine) but i face the following problems.

I can navigate to salesforce login page via Rest API , but  when i click login button the browser show this error

"connection attempt to localhost was rejected. The website may be down or your network may not be properly configured."

while this error is displayed the address  is "https://localhost/testingOauth/oauth_callback.php?code=aPrxaSyVmC8fBbdT3qna8.7vGJbVwZ3aFaydZJP6AbWO_bzpuZLjiozuzkcNYJbWCOJnyQMbeQ%3D%3D"

if i remove "https://" from this address then an SSL certificate problem is displayed.
 
in my php file redirect uri is "https://localhost/testingOauth/oauth_callback.php"
the same uri is Integrated in salesforce Remote Access.
 
i also tried "http://localhost/testingOauth/oauth_callback.php" but no luck.
 
Note that the login page apear only once. after first appearence the login page did not appear and directly navigates to the error page..
please help
thanks
  • August 11, 2012
  • Like
  • 0

How to customize the chatter object in any application, example

I want to hide recommendations sidebar at the right side of the page in Accounts application

 

OR i want to hide the coworkers names while they are posting new messages in Accounts application of salesforce.

 

 

Thank You

 

Regards

M.Bilal

  • July 05, 2012
  • Like
  • 0

Hey all

I want to select multiple contacts in case via lookup field. I need your help in this problem 

Thanks in advance!

  • September 13, 2012
  • Like
  • 0

I want to use the Rest API for salesforce in my developer organization. for testing i use localhost (documentaion describes that localhost works just fine) but i face the following problems.

I can navigate to salesforce login page via Rest API , but  when i click login button the browser show this error

"connection attempt to localhost was rejected. The website may be down or your network may not be properly configured."

while this error is displayed the address  is "https://localhost/testingOauth/oauth_callback.php?code=aPrxaSyVmC8fBbdT3qna8.7vGJbVwZ3aFaydZJP6AbWO_bzpuZLjiozuzkcNYJbWCOJnyQMbeQ%3D%3D"

if i remove "https://" from this address then an SSL certificate problem is displayed.
 
in my php file redirect uri is "https://localhost/testingOauth/oauth_callback.php"
the same uri is Integrated in salesforce Remote Access.
 
i also tried "http://localhost/testingOauth/oauth_callback.php" but no luck.
 
Note that the login page apear only once. after first appearence the login page did not appear and directly navigates to the error page..
please help
thanks
  • August 11, 2012
  • Like
  • 0

Hi there,
I'm using Salesforce.com for 2 months and I decided to learn Apex code to improve the efficiency of my programs. I've installed the Force.com IDE on Eclipse and everything was fine for my first program. In brief my second program is used to notify peoples about something. I've made a little test to see if sending emails works but it doesn't....

@isTest
private class MailTest {

    static testMethod void myUnitTest() {
       FNotifyContactChangedEmail.sendEmail();
    }
}


public class FNotifyContactChangedEmail{
    public static void sendEmail() {
        String[] toAddresses = new String[] {'exemple@exemple.ex','
exemple@exemple.ex'};
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        Messaging.reserveSingleEmailCapacity(1);
        mail.setToAddresses(toAddresses);
        mail.setReplyTo('
exemple@exemple.ex');
        mail.setSenderDisplayName('Salesforce Notifications');
        mail.setSubject('A contact has been modified ');
        mail.setPlainTextBody('Test');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}


That give me that error in the tab failures: Previous load of class failed: scout9|standardsheetlabelcontroller
And this in the log file :
|VARIABLE_SCOPE_BEGIN|[3]|toAddresses|LIST<String>|true|false
|VARIABLE_ASSIGNMENT|[3]|toAddresses|["exemple@exemple.ex","exemple@hotmail.ex"]|0x3095a4ae
|STATEMENT_EXECUTE|[4]
|FATAL_ERROR|Internal Salesforce.com Error
(1952023000)|CUMULATIVE_LIMIT_USAGE
[...]
Number of Email Invocations: 0 out of 10


Is there anything to configure in salesforce? Or something to implements in Eclipse ?

It would be great to have an answer to my question, I thank you in advance



When trying to deploy my custom controller and VF pages for our Sites site from Eclipse, when running the test cases I get the following error

StudentApplicationControllerTest.testController System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, entity type cannot be inserted: Person Account: []

According to the WSDL docs this is a permissions error as follows:
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY
You do not have permission to create, update, or activate the specified record.

This doesn't make sense as I am the System Admin and have permissions to all objects, fields, record types, etc.

 

Here is the getter that creates the Person Account record

 

 

    public Account getStudent() {
        if(student == null) {
            RecordType recType = getPersonAccountRecordTypeId();            
            student = new Account(recordtypeid=recType.id,web_submission__c=true); 
            //student = new Account(); 
        }
        return student;
    }

   RecordType getPersonAccountRecordTypeId() {
    	return [select id,name,sobjectType,ispersontype from recordType where ispersontype=true and sobjectType='account' limit 1];
    }

 

 

 

This is preventing our move to production and is critical.

 

Any help is much appreciated.

  • May 04, 2011
  • Like
  • 0

Hello every body,

I am trying to send email alert from case comment object. Please any one help me how to write the code for sending a mail using trigger.

I've got a lead detail page button which opens a VF page with most important lead details. Clicking on Send on VF page runs my class which sends out the email the given address.

The class works right, but I'm newbie and cannot create the test method.

VF code:

 

<apex:page standardController="Lead" extensions="sendLeadInEmail" title="Send Lead in Email">
 <apex:form >
     <apex:sectionHeader title="Send Lead in email to a specified person"/>
     <apex:pageBlock mode="edit" >
         <apex:messages />
         <apex:pageBlockSection title="Lead details">
                <apex:pageBlockTable value="{!lead}" var="lead">
                    <apex:column value="{!lead.Name}"></apex:column>
                    <apex:column value="{!lead.Company}"></apex:column>
                    <apex:column value="{!lead.Title}"></apex:column>
                    <apex:column value="{!lead.Phone}"></apex:column>
                    <apex:column value="{!lead.Email}"></apex:column>
                    <apex:column value="{!lead.Street}"></apex:column>
                    <apex:column value="{!lead.City}"></apex:column>
                    <apex:column value="{!lead.State}"></apex:column>
                    <apex:column value="{!lead.PostalCode}"></apex:column>
                    <apex:column value="{!lead.Country}"></apex:column>
                    <apex:column value="{!lead.Website}"></apex:column>
                    <apex:column value="{!lead.Product__c}"></apex:column>
                    <apex:column value="{!lead.CreatedDate}"></apex:column>
               </apex:pageBlockTable>
         </apex:pageblockSection>
         <apex:pageblockSection title="Name of Recipient">
                <apex:inputText value="{!recipient}" id="Recipient" maxlength="80" required="true" />
         </apex:pageBlockSection>
         <apex:pageblockSection title="Email address">
                <apex:inputText value="{!address}" id="Address" maxlength="80" required="true"/>
         </apex:pageBlockSection>
         <apex:pageBlockSection title="Personal message (optional)"  >
                <apex:inputTextarea value="{!comment}" id="Comment" required="false" cols="75" rows="6"/>
         </apex:pageBlockSection>
         <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Send Email" action="{!send}" />                        
                <apex:commandButton value="Cancel" action="{!cancel}"/>
         </apex:pageBlockButtons>
    </apex:pageBlock>    
  </apex:form>
</apex:page>

APEX Class code:

 

public class sendLeadInEmail {
 
	public String recipient{ get; set; }
	public String address { get; set; }
 	public String comment { get; set; }
 	public String plainPersonalMessage;
 	public String htmlPersonalMessage;
 	public String subject;
 	public String plainTextBody;
 	public String htmlBody;
 	public String Title, Phone, EmailAddress, Street, City, State, PostalCode, Country, Website, Product;
 
	private final Lead lead;
 
 	// Create a constructor that populates the Lead object
	public sendLeadInEmail(ApexPages.StandardController controller) {
  		lead = [select Name, Company, Title, Phone, Email, Street, City, State, PostalCode, Country, Website, Product__c, CreatedDate, Sent_in_Email_to__c
    			from Lead where id = :ApexPages.currentPage().getParameters().get('id')];
 	}
  
	 
 	public PageReference send() {
  		// Define the email
  		Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
 		   
  		subject = 'Lead details';
	
  		if (lead.Title == null) Title = ''; else Title = lead.Title;
  		if (lead.Phone == null) Phone = ''; else Phone = lead.Phone;
  		if (lead.Email == null) EmailAddress = ''; else EmailAddress = lead.Email;
  		if (lead.Street == null) Street = ''; else Street = lead.Street;
  		if (lead.City == null) City = ''; else City = lead.City+',';
  		if (lead.State == null) State = ''; else State = lead.State;
  		if (lead.PostalCode == null) PostalCode = ''; else PostalCode = lead.PostalCode;
  		if (lead.Country == null) Country = ''; else Country = lead.Country;
  		if (lead.Website == null) Website = ''; else Website = lead.Website;
  		if (lead.Product__c == null) Product = ''; else Product = lead.Product__c;
 
	  	plainPersonalMessage = comment;
  		htmlPersonalMessage = comment;
 
 	 	if (plainPersonalMessage != '') plainPersonalMessage = comment + '\n\n';
  		if (htmlPersonalMessage != '') htmlPersonalMessage = '<pre><font face="times,times new roman,liberation sans,liberation serif,helvetica,arial,sans,serif" size="4">' + comment + '</font></pre><br>';
  
  		plainTextBody = 'Dear '
  			+ recipient + ',\n\n' + plainPersonalMessage + /* ... */ + 'Best regards,';
  
    	htmlBody = '<font face="times,times new roman,liberation sans,liberation serif,helvetica,arial,sans,serif" size="4">Dear ' + recipient + ',<br><br>' +
  	 		htmlPersonalMessage + /* ... */ + 'Best regards,</font>';
  
		String[] toAddresses = address.split(',',0);          
  		String bccAddress = /* ... */;
  		String[] bccAddresses = bccAddress.split(',',0);  
		
  		// Sets the paramaters of the email
 		email.setSubject( subject );
  		email.setToAddresses( toAddresses );
  		email.setPlainTextBody( plainTextBody );
  		email.setHtmlBody( htmlBody );
	  
	  	// Sends the email
  		Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
		
		//set the 'Sent in Email to' field on related Lead:
  		if (lead.Sent_in_Email_to__c == null) lead.Sent_in_Email_to__c = recipient + ' (' + address + ')';
  		else lead.Sent_in_Email_to__c += ', ' + recipient + ' (' + address + ')';
  		update lead;

		// After email sent, navigate to the default view page:
  		return (new ApexPages.StandardController(lead)).view();

	}
	  			

 	static testMethod void myTest() {
       	Lead l = new Lead(
       		FirstName = 'testFirst',
       		LastName = 'testLast',
       		Company ='testcompany',
       		Title = 'testTitle',
       		Phone = '1234',
       		Email = 'test@email.com',
       		Street = 'testStreet',
       		City = 'testCity',
       		State = 'testState',
       		PostalCode = 'testPostal',
       		Country = 'testCountry',
       		Website = 'testWebsite',
       		Product__c = 'testProduct');
		insert l;
		
		Lead testL = [SELECT Id FROM Lead WHERE FirstName = 'testFirst'];
		PageReference leadPage = new PageReference('partialURL');
 		Test.setCurrentPage(leadPage);
		ApexPages.currentPage().getParameters().put('id',testL.Id);
		
		Id testID = ApexPages.currentPage().getParameters().get('id');
		System.assertEquals(testID,testL.Id);
		

		// Instantiate String_function controller
   		ApexPages.StandardController s = new ApexPages.standardController(testL);
   		sendLeadInEmail testSend = new sendLeadInEmail(s);
   		testSend.send();
        
 	}

}

 running the testSend.send(); line gives the next error:

System.NullPointerException: Attempt to de-reference a null object.

 

Where should I set to test the appropriate object?

 

thanks,

Viktor

 

 

  • September 02, 2010
  • Like
  • 0

Hi we have overridden the standard salesforce page layout with our custom VF page . The issue is that for ex in accounts / contacts page which  are custom VF page we are not getting the chatter bar  (hence not able to follow accounts etc ) is there a way this can be enabled 

  • June 23, 2010
  • Like
  • 0