• Prodizee
  • NEWBIE
  • 25 Points
  • Member since 2012

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

Hello,

 

Our team has created an email service that later invokes batches based on the email message. So the flow is:

 

Data Load Job complete -> Email sent to Salesforce Email service handler -> Handler Class invlokes batches

 

While this had been working great, lately following things are happening:

 

1) If email sent automatically (when Data Load Job complete), email never reaches Email Service handler, result: No batch is invoked

2) If email sent manually from outlook, sender receives error email, result: expected batches are invoked

 

Email service accetps email from any source. Automatic emails are reaching to other valid email addresses without fail. Below is the message when we try sending out email manually. I am wondering, where this is going wrong or what changed suddenly? I have tried deactivating and activating Email service in Sandbox, creating another email address for Email service, but still same result.

 

 

I would appreciate your inputs on this situation.

 

Thanks.

 

 

Hello All,

 

I have coded a batch which shares a set of Accounts with Public group in my organization based on certain logic. I have also codeda test method for the same. After 20 plus attempts of updating test method, it is still returning me 9% from Apex Test Runner.

 

Any ideas, what I am missing in latest code below? These test methods are really killing me. Not sure, who else on this forum feels the same. Your inputs would be appreciated.

 

global class Batch_SetAccountSharing implements Database.Batchable<sObject>{
String query = 'SELECT Id, Account_Number__c FROM Account';

global database.querylocator start(Database.BatchableContext BC){
            return Database.getQueryLocator(query);}

global void execute(Database.BatchableContext BC, List<sObject> scope){
    List<Account> accounts = new List<Account>();
    List<AccountShare> accountShares = new List<AccountShare>();
    Map<String,Id> groupMap = new Map<String,Id>();
    
    //  Constants   
    final String GROUP1     = 'Group1';
    final String Group2     = 'Group2';
    final String Group3     = 'Group3';
    final String Group4    =  'Group4';
    final String ROW_CAUSE          = 'Manual';
    final String GROUP_TYPE         = 'Regular';
    final String ACCT_ACCESS_LEVEL  = 'Read';
    final String OPPT_ACCESS_LEVEL  = 'Read';
    final String CASE_ACCESS_LEVEL  = 'Read';
    final String OEM2_IDENTIFIER    = 'CD'; 		//To identify Accounts that begin with CD
    final String ACCT_ACCESS_LEVEL2 = 'Edit';
    final String OPPT_ACCESS_LEVEL2 = 'Edit';
    final String CASE_ACCESS_LEVEL2 = 'Edit';

for(sObject s : scope){Account a = (Account)s;{
            accounts.add(a);
            }
        }
        System.Debug('The number of accounts which need Account sharing is: ' + accounts.size());
        
//Get group Ids from group names
 for (Group g : [SELECT Id, Name
                    FROM Group 
                    WHERE (Name = :GROUP1 or Name = :Group2 or Name = :Group3 or Name = : Group4)]) {
        groupMap.put(g.Name, g.Id);             
    }
       
    System.debug('See what group map prints: ' + groupMap);
    
// Remove existing Opportunity sharing rules    
    AccountShare[] aShares = [SELECT Id 
                              FROM AccountShare 
                              WHERE (UserOrGroupId = :groupMap.get(GROUP1) or UserOrGroupId = :groupMap.get(Group2) or UserOrGroupId = :groupMap.get(Group3))
                              AND AccountId in :accounts AND RowCause = :ROW_CAUSE];
    System.debug('aShares has got: ' + aShares);                                        
    if (!aShares.isEmpty()) delete aShares;
    System.debug('Account Shares has been emptied for relevant accounts, size of aShares is' + aShares.size());
    
//  Create sharing rules

     for(Account a : accounts){
        System.debug('This is account: ' + a);
        if(a.Account_Number__c!=NULL){
        if(a.Account_Number__c.contains(CD_IDENTIFIER)) {
            accountShares.add(new AccountShare(AccountId = a.Id, 
                                               UserOrGroupid = groupMap.get(GROUP1),
                                               AccountAccessLevel = ACCT_ACCESS_LEVEL,
                                               OpportunityAccesslevel = OPPT_ACCESS_LEVEL,
                                               CaseAccessLevel = CASE_ACCESS_LEVEL
                                               ));
        } else {
            accountShares.add(new AccountShare(AccountId = a.Id, 
                                               UserOrGroupid = groupMap.get(Group2), 
                                               AccountAccessLevel = ACCT_ACCESS_LEVEL,
                                               OpportunityAccesslevel = OPPT_ACCESS_LEVEL,
                                               CaseAccessLevel = CASE_ACCESS_LEVEL
                                               ));
        }
            accountShares.add(new AccountShare(AccountId = a.Id, 
                                               UserOrGroupid = groupMap.get(Group3),
                                               AccountAccessLevel = ACCT_ACCESS_LEVEL,
                                               OpportunityAccesslevel = OPPT_ACCESS_LEVEL,
                                               CaseAccessLevel = CASE_ACCESS_LEVEL
                                               ));
        }
            accountShares.add(new AccountShare(AccountId = a.Id, 
                                               UserOrGroupid = groupMap.get(Group4),
                                               AccountAccessLevel = ACCT_ACCESS_LEVEL2,
                                               OpportunityAccesslevel = OPPT_ACCESS_LEVEL2,
                                               CaseAccessLevel = CASE_ACCESS_LEVEL2
                                               ));
    }
     
    if (!accountShares.isEmpty()) insert accountShares;
    System.Debug('AccountShares holds following data: ' + accountShares);
    System.Debug('Size of Accounts: '+ accounts.size());
} 
global void finish(Database.BatchableContext BC){
}

static testMethod void Batch_SetAccountSharing_Test(){  

user u1 = [SELECT ID, UserName FROM User
             WHERE username='xyz@mycompany.com'];
String u1id = u1.id;

Group g1 = [SELECT ID FROM GROUP
			WHERE Name = 'Group1'];

Group g2 = [SELECT ID FROM GROUP
			WHERE Name = 'Group2'];
			
Group g3 = [SELECT ID FROM GROUP
			WHERE Name = 'Group3'];
			
Group g4 = [SELECT ID FROM GROUP
			WHERE Name = 'Group4'];

List<Account> OEM1acct =new list<Account>();
List<Account> OEM2acct =new list<Account>();
List<Account> blankAccts =new list<Account>();
Account a1;

for(integer i=0;i<200;i++){
	//Create OEM1 Accounts
	a1 = new account (Name= 'Test Acc' + i, Account_Number__c = 'AB0' + i);
	OEM1acct.add(a1);
}
for(integer i=200;i<380;i++){
	//Create OEM2 Accounts
	a1 = new account (Name= 'Test Acc' + i, Account_Number__c = 'CD0' + i);
	OEM2acct.add(a1);
}
for(integer i=380;i<400;i++){
	//Create blank Account number accounts
	a1 = new account (Name= 'Test Acc' + i);
	blankAccts.add(a1);
}
insert OEM1acct;
insert OEM2acct;
insert blankAccts;

System.runAs(u1){

Test.StartTest();
Batch_SetAccountSharing TestMADPS = new Batch_SetAccountSharing();
TestMADPS.query = 'SELECT Id, Account_Number__c, Ownerid FROM Account' 
					+ 'WHERE OwnerId=\'' + u1.Id 
					+ '\'' 
					+ 'LIMIT 200';
ID batchprocessid = Database.executeBatch(TestMADPS, 200); 
Test.StopTest();
}
}
}

 

Hello,

 

Our team has created an email service that later invokes batches based on the email message. So the flow is:

 

Data Load Job complete -> Email sent to Salesforce Email service handler -> Handler Class invlokes batches

 

While this had been working great, lately following things are happening:

 

1) If email sent automatically (when Data Load Job complete), email never reaches Email Service handler, result: No batch is invoked

2) If email sent manually from outlook, sender receives error email, result: expected batches are invoked

 

Email service accetps email from any source. Automatic emails are reaching to other valid email addresses without fail. Below is the message when we try sending out email manually. I am wondering, where this is going wrong or what changed suddenly? I have tried deactivating and activating Email service in Sandbox, creating another email address for Email service, but still same result.

 

 

I would appreciate your inputs on this situation.

 

Thanks.

 

 

Hello All,

 

I have coded a batch which shares a set of Accounts with Public group in my organization based on certain logic. I have also codeda test method for the same. After 20 plus attempts of updating test method, it is still returning me 9% from Apex Test Runner.

 

Any ideas, what I am missing in latest code below? These test methods are really killing me. Not sure, who else on this forum feels the same. Your inputs would be appreciated.

 

global class Batch_SetAccountSharing implements Database.Batchable<sObject>{
String query = 'SELECT Id, Account_Number__c FROM Account';

global database.querylocator start(Database.BatchableContext BC){
            return Database.getQueryLocator(query);}

global void execute(Database.BatchableContext BC, List<sObject> scope){
    List<Account> accounts = new List<Account>();
    List<AccountShare> accountShares = new List<AccountShare>();
    Map<String,Id> groupMap = new Map<String,Id>();
    
    //  Constants   
    final String GROUP1     = 'Group1';
    final String Group2     = 'Group2';
    final String Group3     = 'Group3';
    final String Group4    =  'Group4';
    final String ROW_CAUSE          = 'Manual';
    final String GROUP_TYPE         = 'Regular';
    final String ACCT_ACCESS_LEVEL  = 'Read';
    final String OPPT_ACCESS_LEVEL  = 'Read';
    final String CASE_ACCESS_LEVEL  = 'Read';
    final String OEM2_IDENTIFIER    = 'CD'; 		//To identify Accounts that begin with CD
    final String ACCT_ACCESS_LEVEL2 = 'Edit';
    final String OPPT_ACCESS_LEVEL2 = 'Edit';
    final String CASE_ACCESS_LEVEL2 = 'Edit';

for(sObject s : scope){Account a = (Account)s;{
            accounts.add(a);
            }
        }
        System.Debug('The number of accounts which need Account sharing is: ' + accounts.size());
        
//Get group Ids from group names
 for (Group g : [SELECT Id, Name
                    FROM Group 
                    WHERE (Name = :GROUP1 or Name = :Group2 or Name = :Group3 or Name = : Group4)]) {
        groupMap.put(g.Name, g.Id);             
    }
       
    System.debug('See what group map prints: ' + groupMap);
    
// Remove existing Opportunity sharing rules    
    AccountShare[] aShares = [SELECT Id 
                              FROM AccountShare 
                              WHERE (UserOrGroupId = :groupMap.get(GROUP1) or UserOrGroupId = :groupMap.get(Group2) or UserOrGroupId = :groupMap.get(Group3))
                              AND AccountId in :accounts AND RowCause = :ROW_CAUSE];
    System.debug('aShares has got: ' + aShares);                                        
    if (!aShares.isEmpty()) delete aShares;
    System.debug('Account Shares has been emptied for relevant accounts, size of aShares is' + aShares.size());
    
//  Create sharing rules

     for(Account a : accounts){
        System.debug('This is account: ' + a);
        if(a.Account_Number__c!=NULL){
        if(a.Account_Number__c.contains(CD_IDENTIFIER)) {
            accountShares.add(new AccountShare(AccountId = a.Id, 
                                               UserOrGroupid = groupMap.get(GROUP1),
                                               AccountAccessLevel = ACCT_ACCESS_LEVEL,
                                               OpportunityAccesslevel = OPPT_ACCESS_LEVEL,
                                               CaseAccessLevel = CASE_ACCESS_LEVEL
                                               ));
        } else {
            accountShares.add(new AccountShare(AccountId = a.Id, 
                                               UserOrGroupid = groupMap.get(Group2), 
                                               AccountAccessLevel = ACCT_ACCESS_LEVEL,
                                               OpportunityAccesslevel = OPPT_ACCESS_LEVEL,
                                               CaseAccessLevel = CASE_ACCESS_LEVEL
                                               ));
        }
            accountShares.add(new AccountShare(AccountId = a.Id, 
                                               UserOrGroupid = groupMap.get(Group3),
                                               AccountAccessLevel = ACCT_ACCESS_LEVEL,
                                               OpportunityAccesslevel = OPPT_ACCESS_LEVEL,
                                               CaseAccessLevel = CASE_ACCESS_LEVEL
                                               ));
        }
            accountShares.add(new AccountShare(AccountId = a.Id, 
                                               UserOrGroupid = groupMap.get(Group4),
                                               AccountAccessLevel = ACCT_ACCESS_LEVEL2,
                                               OpportunityAccesslevel = OPPT_ACCESS_LEVEL2,
                                               CaseAccessLevel = CASE_ACCESS_LEVEL2
                                               ));
    }
     
    if (!accountShares.isEmpty()) insert accountShares;
    System.Debug('AccountShares holds following data: ' + accountShares);
    System.Debug('Size of Accounts: '+ accounts.size());
} 
global void finish(Database.BatchableContext BC){
}

static testMethod void Batch_SetAccountSharing_Test(){  

user u1 = [SELECT ID, UserName FROM User
             WHERE username='xyz@mycompany.com'];
String u1id = u1.id;

Group g1 = [SELECT ID FROM GROUP
			WHERE Name = 'Group1'];

Group g2 = [SELECT ID FROM GROUP
			WHERE Name = 'Group2'];
			
Group g3 = [SELECT ID FROM GROUP
			WHERE Name = 'Group3'];
			
Group g4 = [SELECT ID FROM GROUP
			WHERE Name = 'Group4'];

List<Account> OEM1acct =new list<Account>();
List<Account> OEM2acct =new list<Account>();
List<Account> blankAccts =new list<Account>();
Account a1;

for(integer i=0;i<200;i++){
	//Create OEM1 Accounts
	a1 = new account (Name= 'Test Acc' + i, Account_Number__c = 'AB0' + i);
	OEM1acct.add(a1);
}
for(integer i=200;i<380;i++){
	//Create OEM2 Accounts
	a1 = new account (Name= 'Test Acc' + i, Account_Number__c = 'CD0' + i);
	OEM2acct.add(a1);
}
for(integer i=380;i<400;i++){
	//Create blank Account number accounts
	a1 = new account (Name= 'Test Acc' + i);
	blankAccts.add(a1);
}
insert OEM1acct;
insert OEM2acct;
insert blankAccts;

System.runAs(u1){

Test.StartTest();
Batch_SetAccountSharing TestMADPS = new Batch_SetAccountSharing();
TestMADPS.query = 'SELECT Id, Account_Number__c, Ownerid FROM Account' 
					+ 'WHERE OwnerId=\'' + u1.Id 
					+ '\'' 
					+ 'LIMIT 200';
ID batchprocessid = Database.executeBatch(TestMADPS, 200); 
Test.StopTest();
}
}
}