+ Start a Discussion
raghavi SRraghavi SR 

Throw error on changes during lead conversion

Hi i have a trigger to fire warning message when the first name and last name are interchnaged during conversion.

trigger LeadWarning on Lead (after update)
{
    for (Lead l:trigger.new)
    {
        if (l.IsConverted && trigger.oldMap.get(l.Id).First_Name_Kana__c==trigger.newMap.get(l.Id).Last_name_Kana__c && trigger.oldMap.get(l.Id).Last_name_Kana__c==trigger.newMap.get(l.Id).First_Name_Kana__c)
        {
           Trigger.new[0].addError('#####');
        }
    }
}


It should throw error only when the changed are made during conversion. but it is throwing error if the name is edited even before conversion. what is the missing part?
{!Piyush_soni__c}{!Piyush_soni__c}
HI raghavi,

 write your tigger on before update not on after update 

Thanks let us know if it helps you
Raj VakatiRaj Vakati
After update the record will be read only.. so do it with before update 
 
trigger LeadWarning on Lead (before update)
{
    for (Lead l:trigger.new)
    {
        if (l.IsConverted && trigger.oldMap.get(l.Id).First_Name_Kana__c==trigger.newMap.get(l.Id).Last_name_Kana__c && trigger.oldMap.get(l.Id).Last_name_Kana__c==trigger.newMap.get(l.Id).First_Name_Kana__c)
        {
           Trigger.new[0].addError('#####');
        }
    }
}

 
raghavi SRraghavi SR
Hi all,

Thanks for the responses.
Even after i changed to before update, i get the error when the name is edited before conversion itself. Is there any other possible changes?
Ajay K DubediAjay K Dubedi
Hi Raghavi,
Below code can fullfill your requirements. Hope this will work for you.
trigger LeadWarning on Lead (before update)
{
    for (Lead leadObj:trigger.new)
    {
        if (leadObj.IsConverted == true && trigger.oldMap.get(leadObj.Id).First_Name_Kana__c == trigger.newMap.get(leadObj.Id).Last_name_Kana__c && trigger.oldMap.get(leadObj.Id).Last_name_Kana__c == trigger.newMap.get(leadObj.Id).First_Name_Kana__c)
        {
           Trigger.new[0].Id.addError('test');
        }
    }
}

Please mark this as best answer if this solves your problem.

Thank you,
Ajay Dubedi