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
nagarjuna gurajanagarjuna guraja 

Need Trigger Development and trigger related question reply

Hello, 
 I need a trigger and simple question on trigger

1.Write a trigger on lead object, whenever leadSource is 'Direct',update LeadStatus should be equal to 'Hot'.
2. Can we perform dml operation on same object in trigger'
Best Regards,
 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Nagarjuna,

try with below code
trigger  leadUpdate on Lead (before update) {
if(trigger.isBefore && trigger.isUpdate){
         for(Lead l :trigger.new){
        if(trigger.oldMap.get(l.id).LeadSource != l.LeadSource && l.LeadSource == 'Direct'){
           l.LeadStatus = 'Hot';
        }
    
    }
   
    
    }
}

If this helps, Please mark it as best answer.

Thanks!!
 
mukesh guptamukesh gupta
Hi Nagarajun,

Before triggers are used to perform the logic on the same object and it triggers fired before the data saved into the database. For DML operation it required to commit with data base. So, we cannot use the DML operation on these triggers.
 
trigger  leadUpdateTrigger on Lead (before update) {
if(trigger.isBefore && trigger.isUpdate){
         for(Lead led :trigger.new){
        if(trigger.oldMap.get(led.id).LeadSource != led.LeadSource && led.LeadSource == 'Direct'){
           led.LeadStatus = 'Hot';
        }
    
    } 
    
    }
}


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 
nagarjuna gurajanagarjuna guraja
Hi, Write a trigger on lead such that whenever leadSource is 'Direct' then leadStatus is equal to 'Hot'. Indeed a trigger for that. Thanks in Advance
mukesh guptamukesh gupta
Hi Nagarjuna,

Instead of triiger you can use WorkFlow or Process buider, for your solution

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh


 
Prateek Jain 167Prateek Jain 167
Hi  Nagarjuna,
please find answers to your query below.
1. 
trigger  leadUpdate on Lead (before update) {
if(trigger.isBefore && trigger.isUpdate){
         for(Lead l :trigger.new){
        if(trigger.oldMap.get(l.id).LeadSource != l.LeadSource && l.LeadSource == 'Direct'){
           l.LeadStatus = 'Hot';
        }
    }
   }
}
2. You cannot  perform DML operation In trigger, the reason it is not possible is 
  • In Before trigger If you update any  value in the record  salesforce automatically update the value for you, DML Statements are not required  in code 
  • In after trigger, the record in which the update is happing is locked so you are not allowed to perform any DML here. 
Kindly mark my solution as the best answer if it helps you.
Thanks
Prateek