You need to sign in to do that
Don't have an account?
fredka
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;
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){}
}
}
}
}
}
catch(Exception e){}
}
}
And I think your queries won't work because of your where clauses.
Please post your error message so we know what is going on.
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!!!!
Thanks again MIke - I really appreciate your time..... I don't get an error... here is the code with your change....
Try this I think that is what you want.
And yes this should support bulk.
Message Edited by mikef on 06-18-2008 04:06 PM
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!!
HI - I was able to save it - it did not work... I got this error:
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
you have to add data checks to make sure records are not null.
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!!!!
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:
Hi,
Please suggest how to modify schema i.e. how to add a column in standard User Object.
Thanks...
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..
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!
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.
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.