Skip to main content
Format
Source Link
Keith C
  • 137.3k
  • 29
  • 218
  • 458

I have a scenario where i want to update the lead record's rating field when the status changes to status == 'Closed - Not Converted'.And And i wrote after update trigger     for this:. Below is the code:

trigger leadUpdate on Lead (after update) {
list<lead> leadUpdate = new List<Lead>();
    for(Lead l :trigger.new){
        if(trigger.oldMap.get(l.id).status != l.status && l.status == 'Closed - Not Converted'){
           lead ld = new lead(id = l.id,rating = 'cold');
            leadUpdate.add(ld); 
        }
    
    }
    update leadUpdate; 
}


And samething can be achieved through  before trigger as well. Below code:

And same thing can be achieved through before trigger as well. Below code:

trigger  leadUpdate on Lead (before update) {
List<lead> leadUpdate = new List<Lead>();
if(trigger.isBefore && trigger.isUpdate){
         for(Lead l :trigger.new){
        if(trigger.oldMap.get(l.id).status != l.status && l.status == 'Closed - Not Converted'){
           l.rating = 'cold';
            leadUpdate.add(l); 
        }
    
    }
   
    }
}

I want to ask which one is the correct approach and what is the difference?

I have a scenario where i want to update the lead record's rating field when the status changes to status == 'Closed - Not Converted'.And i wrote after update trigger   for this: Below is the code

trigger leadUpdate on Lead (after update) {
list<lead> leadUpdate = new List<Lead>();
    for(Lead l :trigger.new){
        if(trigger.oldMap.get(l.id).status != l.status && l.status == 'Closed - Not Converted'){
           lead ld = new lead(id = l.id,rating = 'cold');
            leadUpdate.add(ld); 
        }
    
    }
    update leadUpdate; 
}


And samething can be achieved through  before trigger as well. Below code:
trigger  leadUpdate on Lead (before update) {
List<lead> leadUpdate = new List<Lead>();
if(trigger.isBefore && trigger.isUpdate){
         for(Lead l :trigger.new){
        if(trigger.oldMap.get(l.id).status != l.status && l.status == 'Closed - Not Converted'){
           l.rating = 'cold';
            leadUpdate.add(l); 
        }
    
    }
   
    }
}

I want to ask which one is the correct approach and what is the difference?

I have a scenario where i want to update the lead record's rating field when the status changes to status == 'Closed - Not Converted'. And i wrote after update trigger  for this. Below is the code:

trigger leadUpdate on Lead (after update) {
list<lead> leadUpdate = new List<Lead>();
    for(Lead l :trigger.new){
        if(trigger.oldMap.get(l.id).status != l.status && l.status == 'Closed - Not Converted'){
           lead ld = new lead(id = l.id,rating = 'cold');
            leadUpdate.add(ld); 
        }
    
    }
    update leadUpdate; 
}

And same thing can be achieved through before trigger as well. Below code:

trigger  leadUpdate on Lead (before update) {
List<lead> leadUpdate = new List<Lead>();
if(trigger.isBefore && trigger.isUpdate){
         for(Lead l :trigger.new){
        if(trigger.oldMap.get(l.id).status != l.status && l.status == 'Closed - Not Converted'){
           l.rating = 'cold';
            leadUpdate.add(l); 
        }
    
    }
   
    }
}

I want to ask which one is the correct approach and what is the difference?

I have a scenario where i want to update the lead record's rating field when the status changes to status == 'Closed - Not Converted'.And i wrote after update trigger  for this: Below is the code trigger leadUpdate on Lead (after update) { list leadUpdate = new List(); for(Lead l :trigger.new){ if(trigger.oldMap.get(l.id).status != l.status && l.status == 'Closed - Not Converted'){ lead ld = new lead(id = l.id,rating = 'cold'); leadUpdate.add(ld);  } } update leadUpdate;  }

And samething can be achieved through  before trigger as well. Below code: trigger  leadUpdate on Lead (before update) { List leadUpdate = new List(); if(trigger.isBefore && trigger.isUpdate){ for(Lead l :trigger.new){ if(trigger.oldMap.get(l.id).status != l.status && l.status == 'Closed - Not Converted'){ l.rating = 'cold'; leadUpdate.add(l);  } } } }

trigger leadUpdate on Lead (after update) {
list<lead> leadUpdate = new List<Lead>();
    for(Lead l :trigger.new){
        if(trigger.oldMap.get(l.id).status != l.status && l.status == 'Closed - Not Converted'){
           lead ld = new lead(id = l.id,rating = 'cold');
            leadUpdate.add(ld); 
        }
    
    }
    update leadUpdate; 
}


And samething can be achieved through  before trigger as well. Below code:
trigger  leadUpdate on Lead (before update) {
List<lead> leadUpdate = new List<Lead>();
if(trigger.isBefore && trigger.isUpdate){
         for(Lead l :trigger.new){
        if(trigger.oldMap.get(l.id).status != l.status && l.status == 'Closed - Not Converted'){
           l.rating = 'cold';
            leadUpdate.add(l); 
        }
    
    }
   
    }
}

I want to ask which one is the correct approach and what is the difference?

I have a scenario where i want to update the lead record's rating field when the status changes to status == 'Closed - Not Converted'.And i wrote after update trigger  for this: Below is the code trigger leadUpdate on Lead (after update) { list leadUpdate = new List(); for(Lead l :trigger.new){ if(trigger.oldMap.get(l.id).status != l.status && l.status == 'Closed - Not Converted'){ lead ld = new lead(id = l.id,rating = 'cold'); leadUpdate.add(ld);  } } update leadUpdate;  }

And samething can be achieved through  before trigger as well. Below code: trigger  leadUpdate on Lead (before update) { List leadUpdate = new List(); if(trigger.isBefore && trigger.isUpdate){ for(Lead l :trigger.new){ if(trigger.oldMap.get(l.id).status != l.status && l.status == 'Closed - Not Converted'){ l.rating = 'cold'; leadUpdate.add(l);  } } } }

I want to ask which one is the correct approach and what is the difference?

I have a scenario where i want to update the lead record's rating field when the status changes to status == 'Closed - Not Converted'.And i wrote after update trigger  for this: Below is the code

trigger leadUpdate on Lead (after update) {
list<lead> leadUpdate = new List<Lead>();
    for(Lead l :trigger.new){
        if(trigger.oldMap.get(l.id).status != l.status && l.status == 'Closed - Not Converted'){
           lead ld = new lead(id = l.id,rating = 'cold');
            leadUpdate.add(ld); 
        }
    
    }
    update leadUpdate; 
}


And samething can be achieved through  before trigger as well. Below code:
trigger  leadUpdate on Lead (before update) {
List<lead> leadUpdate = new List<Lead>();
if(trigger.isBefore && trigger.isUpdate){
         for(Lead l :trigger.new){
        if(trigger.oldMap.get(l.id).status != l.status && l.status == 'Closed - Not Converted'){
           l.rating = 'cold';
            leadUpdate.add(l); 
        }
    
    }
   
    }
}

I want to ask which one is the correct approach and what is the difference?

Source Link
Shalini
  • 21
  • 1
  • 2

Before Update Vs after Update Trigger

I have a scenario where i want to update the lead record's rating field when the status changes to status == 'Closed - Not Converted'.And i wrote after update trigger  for this: Below is the code trigger leadUpdate on Lead (after update) { list leadUpdate = new List(); for(Lead l :trigger.new){ if(trigger.oldMap.get(l.id).status != l.status && l.status == 'Closed - Not Converted'){ lead ld = new lead(id = l.id,rating = 'cold'); leadUpdate.add(ld);  } } update leadUpdate;  }

And samething can be achieved through  before trigger as well. Below code: trigger  leadUpdate on Lead (before update) { List leadUpdate = new List(); if(trigger.isBefore && trigger.isUpdate){ for(Lead l :trigger.new){ if(trigger.oldMap.get(l.id).status != l.status && l.status == 'Closed - Not Converted'){ l.rating = 'cold'; leadUpdate.add(l);  } } } }

I want to ask which one is the correct approach and what is the difference?