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
Newbie10Newbie10 

Apex triggers messages

 am new to Apex and not very sure about how to display message. Here is my use case i need to check whether owner has been changed and a box is checked. and if condition is true should not allow the change. If condition is false should carry on processing.

It is NOT an 'ALL or NONE' case.

Its a before update trigger.

What i am doing below is changing the owner back to previous ,when my condition is true.and ideally i would like to show users list of records for which owner cannot be changed.Not sure whether that is even possible.Or is there a better way to do this?

I need to display this especially mainly when user tries to change ownership via  mass transfership  tool

 

public static void checkOwnerChange(map <ID,account>Triggeroldmap,list<account>Triggerlist){
List<account> toUpdate = new list<account> ();
for (Account accountrecord: Triggerlist){
Account oldaccount = TriggeroldMap.get(accountrecord.ID);
if ((accountrecord.checkbox == true) &&
(GEN_Utilities.recordType(accountrecord.recordTypeId) == 'check Account') &&
(oldaccount.OwnerID != accountrecord.OwnerID))
{
accountrecord.OwnerID = oldaccount.OwnerID ;
//some way to display a message saying this record cannot be changed
}
}
}

 
souvik9086souvik9086

Do it like this

 

public static void checkOwnerChange(map <ID,account>Triggeroldmap,list<account>Triggerlist){
List<account> toUpdate = new list<account> ();
for (Account accountrecord: Triggerlist){
Account oldaccount = TriggeroldMap.get(accountrecord.ID);
if ((accountrecord.checkbox == true) &&
(GEN_Utilities.recordType(accountrecord.recordTypeId) == 'check Account') &&
(oldaccount.OwnerID != accountrecord.OwnerID))
{
accountrecord.OwnerID = oldaccount.OwnerID ;

accountrecord.OwnerID.addError('This record cannot be changed');
}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks


}

Newbie10Newbie10

When i do this,and i try to update ownership by mass transfer tool

 

say i have two records ,one which is fine for owner change and other not(as per this condition)

 

but when do add.error statemnt,it simply dispays this error message on mass transfer tool page and  stops me from changin owner for both records,

 

Ideally i should still be able to transfer owner for one record and display error message for records that cannot be

souvik9086souvik9086

public static void checkOwnerChange(map <ID,account>Triggeroldmap,list<account>Triggerlist){
List<account> toUpdate = new list<account> ();
for (Account accountrecord: Triggerlist){
Account oldaccount = TriggeroldMap.get(accountrecord.ID);
if ((accountrecord.checkbox == true) &&
(GEN_Utilities.recordType(accountrecord.recordTypeId) == 'check Account') &&
(oldaccount.OwnerID != accountrecord.OwnerID))
{
accountrecord.OwnerID = oldaccount.OwnerID ;

if(//YOUR CONDITION FOR ERROR){

accountrecord.OwnerID.addError('This record cannot be changed');

}
}
}

}

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks



Newbie10Newbie10

In below,

if(//YOUR CONDITION FOR ERROR){

accountrecord.OwnerID.addError('This record cannot be changed');

}

 

 

actually my condition for error is same as  outerloop.

if ((accountrecord.checkbox == true) &&
(GEN_Utilities.recordType(accountrecord.recordTypeId) == 'check Account') &&
(oldaccount.OwnerID != accountrecord.OwnerID))

 

I do not want to change owner incase of checkbox checked, and recordtype is of a specific value

 

and issue is when i add 

add error as i have said in my above post.it simply displays the error on 'mass transfer tool ui' and if 2/5 records has error condition.wont save 5 of them

souvik9086souvik9086

Can you check like this and let me know whether it works

 

public static void checkOwnerChange(map <ID,account>Triggeroldmap,list<account>Triggerlist){
List<account> toUpdate = new list<account> ();
for (Account accountrecord: Triggerlist){
Account oldaccount = TriggeroldMap.get(accountrecord.ID);

accountrecord.OwnerID = oldaccount.OwnerID ;

if ((accountrecord.checkbox == true) &&
(GEN_Utilities.recordType(accountrecord.recordTypeId) == 'check Account') &&
(oldaccount.OwnerID != accountrecord.OwnerID))
{

accountrecord.OwnerID.addError('This record cannot be changed');

}
}

}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

Newbie10Newbie10

It was the orginal try and its the same issue of just giving an error,and stop processing for all the records

Newbie10Newbie10

aslo this is behaving like this on rmass transfer tool UI

I am  actually trying to prevent the mass transfer of account ownership by criteria