• devforce_111
  • NEWBIE
  • 0 Points
  • Member since 2010

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

How to bulkify this trigger

 

trigger UpdateUserFields on User (before insert ,before update) {


Set<String> userEmails = new Set<String>();

if(Trigger.isInsert){
for(User u :trigger.new){
userEmails.add(u.Email);

}
}

if(Trigger.isUpdate){
for(User u : trigger.new)
                       {
                        User oldUser =trigger.OldMap.get(u.id);
                        userEmails.add(oldUser.Email);
                        }
                        }
                        
 List<Contact> conList =new List<Contact>([Select Email ,Code__c , BTF__c  from Contact where  Account.Name  = 'Test' and id IN :userEmails]);
                                  
                                                          
                for(User u : trigger.new){
               if(u.Email == conList[0].Email){
                              
              u.Testfield1__c = conList[0]. Code__c ;

              u.TestField2__c  = conList[0].BTF__c; 
                                  
             } 
                                  
               }
         
               }

 

Thanks

 

HI ,

 

 Map<Id,Contact[]> entries = new Map<Id,Contact[]> ([Select Email ,Code__c Advisor__c   from Contact where  Account.Name  = Test' and id IN :userEmails]);

 

for(User u :trigger.new)

{

How can i update my user fields test_1__c , test_2__c with the values of the contact fields Code__c and Advisor__c

(test_1__c = code__c and test_2__c = Advisor__c)

}

 

any help is appreciated!!

Thanks

HI,

 

Below is the class and test class. It fine in UAT and gives  no errors but iam not able to deploy it into production as it gives error as  System.LimitException:Too many Query locator rows:10001

 

global class Visibility_Batch_BankAccount implements Database.Batchable<sObject> , Database.Stateful {
String query;
private Visibility_Utility vu;

global Database.QueryLocator start(Database.BatchableContext BC){
vu = new Visibility_Utility();

Visibility_Batch_Base vbb = new Visibility_Batch_Base('Bank_Account__c');
List<Integration_Info__c> intInfoList = [Select Last_Run_Time__c, Previous_Last_Run_Time__c from Integration_Info__c limit 1];
DateTime lastRunTime = intInfoList[0].Last_Run_Time__c;
DateTime PreviousLastRunTime = intInfoList[0].Previous_Last_Run_Time__c;

query = vbb.getQueryString();
return database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List<sObject> scope){
vu.createShare('Bank_Account__c',scope);
}

global void finish(Database.BatchableContext BC){

}
}

 

and the test class is

 

@isTest
private class Visibility_Batch_BankAccount_Test{

static testMethod void Visibility_Batch_BankAccountTest(){
Visibility_Batch_BankAccount visBctBnkAcc = new Visibility_Batch_BankAccount();
Integration_Info__c ii = new Integration_Info__c();
ii.Previous_Last_Run_Time__c = System.Now().addMinutes(-1);
ii.Last_Run_Time__c = system.Now();
insert ii;


Bank_Account__c testBnkAcc = new Bank_Account__c(Name='testName');
List<Bank_Account__c> lstBnkAcc = new List<Bank_Account__c>();
lstBnkAcc.add(testBnkAcc);
insert lstBnkAcc;
Test.startTest();
visBctBnkAcc.start(null);
visBctBnkAcc.execute(null,lstBnkAcc);
visBctBnkAcc.finish(null);
Test.stopTest();
}
}

 

Thanks

How to bulkify this trigger

 

trigger UpdateUserFields on User (before insert ,before update) {


Set<String> userEmails = new Set<String>();

if(Trigger.isInsert){
for(User u :trigger.new){
userEmails.add(u.Email);

}
}

if(Trigger.isUpdate){
for(User u : trigger.new)
                       {
                        User oldUser =trigger.OldMap.get(u.id);
                        userEmails.add(oldUser.Email);
                        }
                        }
                        
 List<Contact> conList =new List<Contact>([Select Email ,Code__c , BTF__c  from Contact where  Account.Name  = 'Test' and id IN :userEmails]);
                                  
                                                          
                for(User u : trigger.new){
               if(u.Email == conList[0].Email){
                              
              u.Testfield1__c = conList[0]. Code__c ;

              u.TestField2__c  = conList[0].BTF__c; 
                                  
             } 
                                  
               }
         
               }

 

Thanks

 

HI ,

 

 Map<Id,Contact[]> entries = new Map<Id,Contact[]> ([Select Email ,Code__c Advisor__c   from Contact where  Account.Name  = Test' and id IN :userEmails]);

 

for(User u :trigger.new)

{

How can i update my user fields test_1__c , test_2__c with the values of the contact fields Code__c and Advisor__c

(test_1__c = code__c and test_2__c = Advisor__c)

}

 

any help is appreciated!!

Thanks

HI,

 

Below is the class and test class. It fine in UAT and gives  no errors but iam not able to deploy it into production as it gives error as  System.LimitException:Too many Query locator rows:10001

 

global class Visibility_Batch_BankAccount implements Database.Batchable<sObject> , Database.Stateful {
String query;
private Visibility_Utility vu;

global Database.QueryLocator start(Database.BatchableContext BC){
vu = new Visibility_Utility();

Visibility_Batch_Base vbb = new Visibility_Batch_Base('Bank_Account__c');
List<Integration_Info__c> intInfoList = [Select Last_Run_Time__c, Previous_Last_Run_Time__c from Integration_Info__c limit 1];
DateTime lastRunTime = intInfoList[0].Last_Run_Time__c;
DateTime PreviousLastRunTime = intInfoList[0].Previous_Last_Run_Time__c;

query = vbb.getQueryString();
return database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List<sObject> scope){
vu.createShare('Bank_Account__c',scope);
}

global void finish(Database.BatchableContext BC){

}
}

 

and the test class is

 

@isTest
private class Visibility_Batch_BankAccount_Test{

static testMethod void Visibility_Batch_BankAccountTest(){
Visibility_Batch_BankAccount visBctBnkAcc = new Visibility_Batch_BankAccount();
Integration_Info__c ii = new Integration_Info__c();
ii.Previous_Last_Run_Time__c = System.Now().addMinutes(-1);
ii.Last_Run_Time__c = system.Now();
insert ii;


Bank_Account__c testBnkAcc = new Bank_Account__c(Name='testName');
List<Bank_Account__c> lstBnkAcc = new List<Bank_Account__c>();
lstBnkAcc.add(testBnkAcc);
insert lstBnkAcc;
Test.startTest();
visBctBnkAcc.start(null);
visBctBnkAcc.execute(null,lstBnkAcc);
visBctBnkAcc.finish(null);
Test.stopTest();
}
}

 

Thanks