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
Richa_LearningRicha_Learning 

Trigger query field

Hi,

our developer has created this trigger to check the owner of an account. Initialowner__C is a cusotm field and a text field. I searched everywhere but i am not sure from where we can get the initialowner__C field value.

Any idea?
trigger ownerchange on Account (before insert) {
Account [] acc = Trigger.new.clone();
Profile pfs = [Select Id, name from Profile where id = :UserInfo.getProfileId()];
UserRole rls = null;
if(UserInfo.getUserRoleId() != null) {
rls = [Select Id, name from UserRole where id =:UserInfo.getUserRoleId() ];
} 
if(acc[0].InitialOwner__c!=null && ((rls != null && rls.name == 'Team') || pfs.name =='Manager')) {
Trigger.new[0].Ownerid = Trigger.new[0].InitialOwner__c;

} 


}

 
srlawr uksrlawr uk
I take it you are getting an error then telling you the field cannot be found, or isn't being loaded?

Fields in triggers are lazy loaded, so simply referencing them in code using the dot notation like on line 08 should just have the value in it.

The only complication here is, (for some reason?) you are cloningTrigger.new into a new array, so - off the top of my head - I don't actually know exactly if you will still get all the benefits of the lazy loading in the trigger context variables.

Does the clone of the account list get created by copy, or reference? Why do you need to clone trigger.new as you are just copying two fields over within that record I can't quite work out what you are hoping this trigger does at such a complicated level!

What s your error message.. then perhaps we can work on it..!
Adrian  GawryszewskiAdrian Gawryszewski
Hi

How do you create those accounts? Do you import them or create manually via Salesforce interface? I'll be honest with you this code is a liitle bit messy. I would assume that that field would be added to new account form layout and this is from where you can take that value. However without more info I'm not 100% sure.
Richa_LearningRicha_Learning
I found a blog:http://mysalesforcecode.blogspot.com/2012_09_01_archive.html

I checked in my org and yes the initialowner was getting copied from custom clone buton.....................

But issue is, it is not working..it overrides the owner
srlawr uksrlawr uk
OK. So I have looked a little closer, and I see a couple of problems. Setting the owner is in a before trigger... I have a feeling that is going to get overridden in the transaction.. Otherwise, your trigger is designed to override the owner right? So when you say "its not working, it overrides the owner" do you mean the original owner overrides the innitalowner, or visa versa?!
Richa_LearningRicha_Learning
My requirement is : if user who is cloning the account has role as Team or profile as manager  then the original account owner should be assigned to new account. If any other role or profile is cloning account then it should take the person who is cloning.

My button code: 
/{!Account.Id}/e?clone=1&00NG000000Dgx7D={!Account.OwnerId}

This is my full trigger. I did the Debug logs and found that the trigger runs but it skips the owner association. but it keep the status=active. so i  am nto sure why it skips the owner association.
trigger ownerchange on Account (before insert) {
Account [] acc = Trigger.new.clone();
Profile pfs = [Select Id, name from Profile where id = :UserInfo.getProfileId()];
UserRole rls = null;
if(UserInfo.getUserRoleId() != null) {
rls = [Select Id, name from UserRole where id =:UserInfo.getUserRoleId() ];
} 
if(acc[0].InitialOwner__c!=null && ((rls != null && rls.name == 'Team') || pfs.name =='Manager')) {
Trigger.new[0].Ownerid = Trigger.new[0].InitialOwner__c;

} 
if((rls != null && rl.name == 'Team') || pfs.name =='Manager'){
Trigger.new[0].status__c = 'Active';}

}

 
Richa_LearningRicha_Learning
I figured this out..thanks everyone.....