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
fredkafredka 

Add user Fields to Custom Object

We use a custom object named "quote" that rolls up to opportunity - the owner of the quote is what we report on.  I am trying to have manager type reporting on this custom object.  I added a field named "direct manager" in the user object that holds the manager for this purpose.  I am trying to automatically populate this field based on the owner of the quote.... Here is what I have so far (which does not work)..... any help would be greatly appreciated!!!
 
trigger UpdateManagerDirector on Quote__c (after insert, after update) {
    for (Quote__c q : Trigger.new)
        {
                String QuoteId = q.Id;
                String SalesRep = q.Rep_Name__c;
                String SalesRepID = q.OwnerID;              
            try
            {
               Quote__c[] qt = [Select Quote__c.Id, Quote__c.Rep_Name__c, Quote__c.OwnerID,Quote__c.Manager__c from Quote__c where Quote__c.Id=:q.Id];
                User[] Usr = [Select User.Direct_Manager__c from User where User.ID=:q.OwnerId];
                if(trigger.isInsert)   
                {
                    qt[0].OwnerID = SalesRep;
                    qt[0].Manager__c = Mgr;
                    update qt;
                }
                else
                {
                    if(qt[0].OwnerID != qt[0].Rep_Name__c)
                    {
                        qt[0].OwnerID = SalesRep;
                       qt[0].Manager__c = SalesRepID;
                        update qt;
                    }
                }
            }
            catch(Exception e){}
        }
}
mikefmikef
The first major issue is your trigger is not written to process bulk operations, please look at this post, for more info on bulk operations.

And I think your queries won't work because of your where clauses.


Code:
Quote__c[] qt = [Select Quote__c.Id, Quote__c.Rep_Name__c, Quote__c.OwnerID,Quote__c.Manager__c from Quote__c where Quote__c.Id=:q.Id];
User[] Usr = [Select User.Direct_Manager__c from User where User.ID=:q.OwnerId]; 

Change to:
Quote__c[] qt = [Select Quote__c.Id, Quote__c.Rep_Name__c, Quote__c.OwnerID,Quote__c.Manager__c from Quote__c where Id=:q.Id];
User[] Usr = [Select User.Direct_Manager__c from User where Id =:q.OwnerId]; 

Please post your error message so we know what is going on.

fredkafredka
Thanks for responding Mike - I am new to salesforce and have not used java before - that is part of my problem.  The other thing that I find frustrating is that the documenation (in my opinion) has not been very comprehensive.  I have the cookbook and I have the apex language ref book.  However, I find that they give you very basic info and do not pick apart and explain it - perhaps because I am so new @ the language...
 
Anyway, because in this situation, I am trying to get the manager from the user object and everything else is based on the quote object, I am really struggling.
 
Thanks again!!!
 
fredkafredka

MIke - when I run this, I dont get an error, however, the manager field on the quote does not update either.....

I am not sure how to get the manager field to update with the manager from users?  Am I close? thanks!!!!

mikefmikef
please post the error and the new code.
fredkafredka

Thanks again MIke - I really appreciate your time..... I don't get an error... here is the code with your change....

trigger UpdateManagerDirector on Quote__c (after insert, after update) {
    for (Quote__c q : Trigger.new)
        {
                String QuoteId = q.Id;
                String SalesRep = q.Rep_Name__c;
                String SalesRepID = q.OwnerID;               
            try
            {
               Quote__c[] qt = [Select Quote__c.Id, Quote__c.Rep_Name__c, Quote__c.OwnerID,Quote__c.Manager__c from Quote__c where Id=:q.Id];
               User[] Usr = [Select User.Direct_Manager__c from User where Id =:q.OwnerId];
                if(trigger.isInsert)    
                {
                    qt[0].OwnerID = SalesRep;
                    qt[0].Manager__c = SalesRep;
                    update qt;
                }
                else
                {
                    if(qt[0].OwnerID != qt[0].Rep_Name__c)
                    {
                        qt[0].OwnerID = SalesRep;
                       qt[0].Manager__c = SalesRep;

                        update qt;
                    }
                }
            }
            catch(Exception e){}
        }
}
mikefmikef
Try this, please note I did not test this.

Code:
trigger UpdateManagerDirector on Quote__c (before insert, before update) {
    
    Set<Id> quoteOwners = new Set<Id>();
       
    for (Quote__c q : Trigger.new){
     if(q.Rep_Name__c != null){ //assumption that Rep_Name__c is a user lookup
      quoteOwners.add(q.Rep_Name__c);
     }
    }
    
    Map<Id,Id> userToDirectManagerMap = ([select Direct_Manager__c from User where Id in : quoteOwners]);
    
    for(Quote__c q : Trigger.new){
     if(trigger.isInsert){
      q.OwnerId = q.Rep_Name__c;
      q.Manager__c = userToDirectManagerMap.get(q.Rep_Name__c);
     }else{
         if(q.OwnerID != q.Rep_Name__c){
          q.OwnerID = Rep_Name__c;
             q.Manager__c = userToDirectManagerMap.get(q.Rep_Name__c);
         }
     } 
    }        
}

Try this I think that is what you want.
 
 
fredkafredka
THis is great - If you can help me get this to work I can disect and understand.... I am getting an error:
 
Error: Compile Error: Illegal assignment from LIST:SOBJECT:User to MAP:Id,Id at line 11 column 9
 
on this line of code:
 
Map<Id,Id> userToDirectManagerMap = ([select Direct_Manager__c from User where Id in : quoteOwners]);
Also, using the map will allow this to be used in bulk? is that correct.... thanks so much!!!
 
Fred
mikefmikef
Use this;

Code:
Map<Id,Id> userToDirectManagerMap = new Map<Id,Id>();
for(User user : [select Id, Direct_Manager__c from User where Id in : quoteOwners]){
   userToDirectManagerMap.put(user.Id,user.Direct_Manager__c);
}

 And yes this should support bulk.



Message Edited by mikef on 06-18-2008 04:06 PM
fredkafredka

Thanks!!!! I got one more error....

Error: Compile Error: Invalid initial type LIST:SOBJECT:User for MAP:Id,Id at line 11 column 45

Again, thanks so much!!

mikefmikef
I edited my response after posting please use the new code.
fredkafredka

HI - I was able to save it - it did not work... I got this error:

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger UpdateManagerDirector caused an unexpected exception, contact your administrator: UpdateManagerDirector: data changed by trigger for field Owner ID: owner cannot be blank
 
I think I can work it from here (think is the key word in that sentance)  I don't want to use up all your time... I appreciate all that you have done !!!!  Do you have any advice on ref material in order to break down what you have provided so I understant it?
 
Again, thanks for all of your help!!!!
 
mikefmikef
Use this:

Code:
trigger UpdateManagerDirector on Quote__c (before insert, before update) {
    
    Set<Id> quoteOwners = new Set<Id>();
       
    for (Quote__c q : Trigger.new){
     if(q.Rep_Name__c != null){ //assumption that Rep_Name__c is a user lookup
      quoteOwners.add(q.Rep_Name__c);
     }
    }
    
    Map<Id,Id> userToDirectManagerMap = ([select Direct_Manager__c from User where Id in : quoteOwners]);
    
    for(Quote__c q : Trigger.new){
     if(trigger.isInsert){
      if(q.Rep_Name__c != null){
        q.OwnerId = q.Rep_Name__c;
        q.Manager__c = userToDirectManagerMap.get(q.Rep_Name__c);
      }
     }else{
         if(q.OwnerID != q.Rep_Name__c){
           if(q.Rep_Name__c != null){
             q.OwnerID = Rep_Name__c;
             q.Manager__c = userToDirectManagerMap.get(q.Rep_Name__c);
           }
         }
     } 
    }        
}

 you have to add data checks to make sure records are not null.

fredkafredka

I probably made this too complicated as I copied another trigger over that was usign the sales rep field - can this be adjusted to simply use the owner field to look up the direct_manager__c field?

I can't thank you enough!!!!

fredkafredka
Also, one more piece of info.... the field manager__c is a lookup to user... should I change this to a text field ?  not sure if that is effecting the ability to update it. thanks!!!
fredkafredka

I got it working thanks to all of your help!!! Like I said, I complicated things with the if statements that I really did not need.

Just for future ref.  Here is my final code:  The user object has a custom field named Direct_Manager__c. 

Thanks Mike for all of the help!!!!!:smileyhappy:

Code:
trigger UpdateManagerDirector on Quote__c (before insert, before update) {
    
    Set<Id> quoteOwners = new Set<Id>();
       
    for (Quote__c q : Trigger.new){
       quoteOwners.add(q.ownerid);
     }
    
    Map<Id,Id> userToDirectManagerMap = new Map<Id,Id>();
for(User user : [select Id, Direct_Manager__c from User where Id in : quoteOwners]){
   userToDirectManagerMap.put(user.id, user.Direct_Manager__c);
}
    
    for(Quote__c q : Trigger.new){
         q.Manager__c = userToDirectManagerMap.get(q.OwnerID);
      }
             }

 
GL_NSGL_NS

Hi,

Please suggest how to modify schema i.e. how to add a column in standard User Object.

Thanks...

fredkafredka
I am not sure what you are asking? 
GL_NSGL_NS

Hi,

I have a requirement in which i need to add Users explicitly from my salesforce application.Now options i got for this is using upsert method.But upsert method takes ExternalID as a parameter which is a custom field.

Now when I am logging in Salesforce and searching for User Object , I am not getting the User Object.

I am not sure whether we can add a custom field in User Object or Not.Because one comment I got in blogs is "The user object is not updateable in Apex code. ".

Please suggest how to add a field and if possible how to access the User Object from Apex code to insert a new User.

Thanks..

fredkafredka

Keeping in mind that I am also very new to salesforce.....

Are you saying that you just want to import users into salesforce?  If so, you should just use "insert"  ...

Also, there is a user object where you can add custom fields if you need to... just go to "setup" and then "customize"... its toward the bottom.  I have added fields to the user object.

Let me know if I am not understanding what you are saying - thanks!

GL_NSGL_NS

Thanks for your reply.

Actually what I have got uptill now is that Insertion in standard User Object is possible only through API calls.And the only method available there is upsert which require ExternalID field.

fredkafredka
I am still trying to understand.... are you just tring to import users into salesforce?  If so, use the dataloader "insert"
 
thanks!
GL_NSGL_NS
Hi All,
 
Please give some pointers on "How to set the Property of Custom field as ExternalId." for using the Upsert API method.
 
Thanks..
GL_NSGL_NS

No its not the Insert only.Actually whenever a user is created i nmy Application I want that user to be added in Salesforce.So dataloader can't be used.

Thanks.