• Youstina Guirguis
  • NEWBIE
  • 30 Points
  • Member since 2019

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

I have a batch process that prevents two different "concurrent" executions of the batch for a given user by maintaining some state (in the databse) that is set in start and cleared in finish (actually using the user for the created AsyncApexJob instances). During start processing, the state is queried from the database; if there is an entry already for the current user the batch is aborted, using System.abortJob, and an empty query locator is returned.

I am trying to test that two different users can successfully execute the batch via the use of the following:

Id profileId = UserInfo.getProfileId();

List<User> fakeUsers = new List<User> {
        new User(Alias = 'X', Email='X@testorg.com', EmailEncodingKey='UTF-8', FirstName = 'Jim', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = profileId, TimeZoneSidKey='Europe/London', UserName='X@testorg.com'),
        new User(Alias = 'Y', Email='Y@testorg.com', EmailEncodingKey='UTF-8', FirstName = 'Fred', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = profileId, TimeZoneSidKey='Europe/London', UserName='Y@testorg.com')
};

Test.startTest();

// Simulate running both together under different user accounts
Id id1;
Id id2;

System.runAs(fakeUsers[0]) {
    MyBatch b1 = new MyBatch();

    id1 = Database.executeBatch(b1);
}

System.runAs(fakeUsers[1]) {
    MyBatch b2 = new MyBatch();

    id2 = Database.executeBatch(b2);
}

Test.stopTest();

Unfortunately it seems that both of the batches still get run with the same user (the actual user running the test rather than either of the fake users). I suspect this is because of the way batches are actually executed during the Test.stopTest method invocation, rather than at some asynchronous time.

Have I come across a bug in the way batches are run in a given user context during testing? Is there a workaround I can use?
  • January 18, 2018
  • Like
  • 0
I have a requirement to set the From Name dynamically on all outgoing emails on the Case.

So for example if the From Email is support@abc.com, I would need the name to be the logged-in User's name, so if Tom sent an email it would say: Tom "support@abc.com" and if Jerry sent the email it would be Jerry: "support@abc.com"

I tried setting the FromName on the before trigger on the EmailMessage but it doesn't work.

Any ideas on how to achieve this?

Thanks

I have an Email Message Trigger written on the Sandbox which follows the best practices when I run the test class on the Sandbox, it passes, but when I deploy to Production I receive this error: Long-Running Tests
The following tests have long execution times. To improve each test's performance, be sure to follow Apex and SOQL best practices.

Does anybody have any idea what I should do to pass the test class on Production?

Salesforce Case emails being sent are now appearing in the Contact Activity feed.

This has only just started since Summer 19 was released a few weeks ago. Prior to this, the emails would only be in the Case. (no record against the contact)

I have been trying to use apex to prevent this from happening and found out that this is possible to prevent by doing the following

The following values had to be nullified through a custom trigger on the EmailMessage object 
- ToAddress (Standard field on the Email Message Object) [Before Trigger on Email Message] 
- RelationId (Standard field on the Email Message Relation Object) [After Trigger on Email Message to nullify the mentioned field on the child object] 

Please note that BOTH fields have to be nullified in order for the email record to not be related on the Account/Contact. 

Note: 
- Since the ToAddress field is nullified, I have to copy the email address in the BccAddress field in the trigger 
- According to the Email Message object documentation, 
"ToAddress: A string array of email addresses for recipients who were sent the email message. Include only email addresses that are not associated with Contact, Lead, or User records in Salesforce. If the recipient is a contact, lead, or user, add their ID to the ToIds field instead of adding their email address to the ToAddress field. Then the email message is automatically associated with the contact, lead, or user." 

so in theory, if I should nullify ToIds and not ToAddress but in reality, this is not how it works. 
Documentation Link: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_emailmessage.htm 


Question:
Is it possible to keep the ToAddress email field but remove the link to contact?

 

I have an Email Message Trigger written on the Sandbox which follows the best practices when I run the test class on the Sandbox, it passes, but when I deploy to Production I receive this error: Long-Running Tests
The following tests have long execution times. To improve each test's performance, be sure to follow Apex and SOQL best practices.

Does anybody have any idea what I should do to pass the test class on Production?

I have a batch process that prevents two different "concurrent" executions of the batch for a given user by maintaining some state (in the databse) that is set in start and cleared in finish (actually using the user for the created AsyncApexJob instances). During start processing, the state is queried from the database; if there is an entry already for the current user the batch is aborted, using System.abortJob, and an empty query locator is returned.

I am trying to test that two different users can successfully execute the batch via the use of the following:

Id profileId = UserInfo.getProfileId();

List<User> fakeUsers = new List<User> {
        new User(Alias = 'X', Email='X@testorg.com', EmailEncodingKey='UTF-8', FirstName = 'Jim', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = profileId, TimeZoneSidKey='Europe/London', UserName='X@testorg.com'),
        new User(Alias = 'Y', Email='Y@testorg.com', EmailEncodingKey='UTF-8', FirstName = 'Fred', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = profileId, TimeZoneSidKey='Europe/London', UserName='Y@testorg.com')
};

Test.startTest();

// Simulate running both together under different user accounts
Id id1;
Id id2;

System.runAs(fakeUsers[0]) {
    MyBatch b1 = new MyBatch();

    id1 = Database.executeBatch(b1);
}

System.runAs(fakeUsers[1]) {
    MyBatch b2 = new MyBatch();

    id2 = Database.executeBatch(b2);
}

Test.stopTest();

Unfortunately it seems that both of the batches still get run with the same user (the actual user running the test rather than either of the fake users). I suspect this is because of the way batches are actually executed during the Test.stopTest method invocation, rather than at some asynchronous time.

Have I come across a bug in the way batches are run in a given user context during testing? Is there a workaround I can use?
  • January 18, 2018
  • Like
  • 0