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
Siddharth LakhotiaSiddharth Lakhotia 

Apex Trigger to restrict the change in value of a lookup field , once the record is cloned

HI,

I need to write an apex trigger, that restricts the account name field on the orders object to be changed, after the record has been cloned.

Can somebody help me with this
Gaurish Gopal GoelGaurish Gopal Goel
Hi, Please post the code which you have written and I will help you in resolving the issue. This community is only for those who have tried something themselves, please don't post any query unless you have tried.
Siddharth LakhotiaSiddharth Lakhotia
Actually, I am new to development..So haven't tried writting one.. Should I try writting once.. and you will help me with that
Gaurish Gopal GoelGaurish Gopal Goel
I will advice you to complete Developer Beginner Trail on Trailhead first.
Siddharth LakhotiaSiddharth Lakhotia

Ok.. 

Meanwhile, can you help me know, if I am going right way

trigger PreventChangeOfAccountName on Order (before insert) {

list <order> ord = new list <Order>();
    
    for (Order o : trigger.new){
        
       If ( o.IsCloned__c = TRUE && ISCHANGED(o.AccountId))
       {
           
       }
    }
    
}

Gaurish Gopal GoelGaurish Gopal Goel
I think you should try creating a VF page and override the clone button.
Siddharth LakhotiaSiddharth Lakhotia
Hi Gaurish,

I have written this code,  however , It is not allowing me to save the record , even before cloning. Can you help me explain why

trigger PreventChangeOfAccountName on Order (before update) {

list <order> ord = new list <Order>();
    
    for (Order o : trigger.new){
        
       If ( o.IsCloned__c = TRUE && o.AccountId != null && Trigger.oldMap.get(o.id).AccountId != null)
       {
           o.addError('Account name for Cloned Records cant be changed');
       }
    }
    
}
Gaurish Gopal GoelGaurish Gopal Goel
Hi Siddharth, The apex trigger won't work in this case as the cloned record is a new record and not yet inserted. So you cannot use UPDATE event. And in INSERT event you cannot check what was the previous value. I will suggest you to create a VF page and override the clone button. Thanks.