You need to sign in to do that
Don't have an account?
Siddharth Lakhotia
Trigger to throw an error message , when updating value of a Cloned Record
Hi,
I want to write a trigger, which throws error message , when the accountname is changed from the cloned version of record.
I have tried writting code, somehow its not working
Can someone help
Please find the code below :
trigger PreventChangeOfAccountName on Order (before update , after insert) {
for (Order o : trigger.new){
If (o.isClone() && trigger.oldmap.get(o.Id).AccountId <> trigger.oldmap.get(o.Id).AccountId)
{
o.addError('Account Name for Cloned records cant be changed');
}
}
}
I want to write a trigger, which throws error message , when the accountname is changed from the cloned version of record.
I have tried writting code, somehow its not working
Can someone help
Please find the code below :
trigger PreventChangeOfAccountName on Order (before update , after insert) {
for (Order o : trigger.new){
If (o.isClone() && trigger.oldmap.get(o.Id).AccountId <> trigger.oldmap.get(o.Id).AccountId)
{
o.addError('Account Name for Cloned records cant be changed');
}
}
}
oldMAp is a map of IDs to the old versions of the sObject records.
This map is only available in update and delete triggers.
So we need to use getCloneSourceId() methods during clone.
Find the sample code bellow for your above trigger:
All Answers
trigger.oldmap is null in after insert, so null pointer exception will be throw from your code.
I suggest use the method getCloneSourceId() to get Source record ID then after soql query you can check if account is changed in clone record or not.
Cant gather you...
oldMAp is a map of IDs to the old versions of the sObject records.
This map is only available in update and delete triggers.
So we need to use getCloneSourceId() methods during clone.
Find the sample code bellow for your above trigger:
It worked. ! Thanks a lot.
Can you suggest the rational behind using this statement
mapSourceOrderIdToAccount.put(obj.getCloneSourceId(), null);
Hi OmPrakash,
I am unable to cover for error message while trying to write Test class for this trigger.. Seems like, I am unable to fit the logic in try statement,
Please find the code below for my test class , Can you suggest some changes regarding this/
@isTest(SeeAllData = false)
public class preventChangeOfAccountNameTestClass {
static testmethod void preventAccNameChange(){
Account acc = new Account();
acc.name='TestAccount1';
insert acc;
Order ord = new Order();
ord.AccountId = acc.Id;
ord.EffectiveDate = System.Today();
ord.EndDate = System.Today()+5;
ord.Status = 'NA';
ord.Offer_Status__c = 'Open';
insert ord;
try{
ord.isClone();
update ord;
}
catch(DMLexception e){
Boolean expectedExceptionThrown = e.getMessage().contains('Account Name for Cloned records cant be changed') ? true : false;
System.AssertEquals(expectedExceptionThrown, true);
}
}
}
It worked!!! You are a genious...
Can you still tell me , how does it cover for .adderror message
objNewOrder is cloned instance of order in your code (we used clone method) and we have mapped accountid = new account id.
If statement satisfied as per above statement and desire code covered.
Hope I explained well.
Let me know if any additional query.