function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Suraj DemgundeSuraj Demgunde 

Based on below class i want to update All Leads in the system how it is possible ?

trigger Lead_Update on Lead (before insert,before update) {
    String dam ='';
    String jub='';
    String west1 ='';
    String west2 = '';
    String central = '';
    String Gcc = '';
    /*Public String uid=UserInfo.getUserId(); 
  public user UserDetails =   [select  User.Location__c from User Where User.Id =:uid limit 1]; 
  public string UserLoc= UserDetails.Location__c;
   // System.debug(UserLoc);*/
    
    for(Lead lt : trigger.new){
        
    String uid=UserInfo.getUserId(); 
   user UserDetails =   [select  User.Location__c from User Where User.Id =:uid limit 1]; 
   string UserLoc= UserDetails.Location__c;
        
      //  system.debug('---------------------Location------------------'+location);
        
        
        
       lt.Region__c = UserLoc;
        
    }

}
 
Narender Singh(Nads)Narender Singh(Nads)
Hi Suraj,
Deactivate your trigger.

And make a new class as shown.
public class cls {
    
    public static void UpdateAllLeads(){
        lead[] LeadList=[Select id, name ,Region__c from lead]; //THIS WILL RETURN ALL LEADS FROM THE DATABASE
        lead[] LeadsToUpdate=new lead[]{};
        String uid=UserInfo.getUserId();
        user UserDetails =   [select  User.Location__c from User Where User.Id =:uid limit 1]; 
        for(lead l:LeadList){
            l.Region__c= UserDetails.Location__c;
            LeadsToUpdate.add(l);
        }
        update LeadsToUpdate;
    }
}
Then in anonymous window, write:

cls.UpdateAllLeads();

This will update all your leads according to your requirement

Let me know if it helps
Thanks!
Suraj DemgundeSuraj Demgunde
but when i deactivate trigger i want to fix old data so i write trigger it is afftected to update all data ?
 
Suraj DemgundeSuraj Demgunde
Any other way excluding data loader ?
Narender Singh(Nads)Narender Singh(Nads)
Hi,
Can you explain your requirement a bit more clearly please? I am not able to undestand your requirement.
Suraj DemgundeSuraj Demgunde
i want to fix some old lead data so i apply trigger on that but trigger work to fix the old date when lead update or certain operation happen on lead so it is not possible to deactive trigger and write class but in active trigger i want to update Leads
Narender Singh(Nads)Narender Singh(Nads)
Hi suraj,

You can apply filters to the SOQL query in the code, something like this.

[Select id, name ,Region__c from lead where createddate<today]; 

Note: The filters will depend upon your requirement.

I wounldn't suggest you doing what you want to do in your trigger. Because your trigger will fail incase the numbers of records are large.
Suraj DemgundeSuraj Demgunde
shall i go through data loader ?
 
Narender Singh(Nads)Narender Singh(Nads)
Hi,
Through data loader also if you try to insert more than 100 records, your trigger will hit the SOQL governor limit, because your code is not bulkified.
Suraj DemgundeSuraj Demgunde
so how i make it bulkfield plz help me its very urgent
 
Narender Singh(Nads)Narender Singh(Nads)
This is how your bulkified trigger will look like:
 
trigger Lead_Update on Lead (before insert,before update) {
	
    String dam ='';
    String jub='';
    String west1 ='';
    String west2 = '';
    String central = '';
    String Gcc = '';
    String uid=UserInfo.getUserId(); 
	user UserDetails =   [select  User.Location__c from User Where User.Id =:uid limit 1]; 
	
    lead[] LeadListToUpdate=new lead[]{};
    for(Lead lt : trigger.new){
		lt.Region__c = UserDetails.Location__c;
        LeadListToUpdate.add(lt);
    }
	update LeadListToUpdate;

}

 
Suraj DemgundeSuraj Demgunde
@ narendar thanks bhai one last qsn to you joined salesforce programmer as 5 month before but weak in coding what can i do for coding bcz warning came from manager s
Narender Singh(Nads)Narender Singh(Nads)
You just gotta keep practing. You can refer this link to understand the best practices to write apex code: https://developer.salesforce.com/page/Apex_Code_Best_Practices

You can practice for triggers from here: https://tekslate.com/15-sample-triggers-different-scenarios/