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
RKRK 

Trigger Question - Updating A Contact Custom Field With Data From Contact Owner Object

Hey All,
Well, I finally realized that I was going to in some way learn to do some remedial code in Apex if I'm going to extend the app to where I want it to go.

I've created a very basic trigger that, when a contact is updated, will go out to the Contact Owner user record and grab data from a custom field on the Contact Owner record.

I have figured out how to query the OwnerID just fine.  However, I do not seem to be able to dynamically query for the custom field data on the Contact Owner User Record by using hte OwnerID.  When I attempt to complie, I get a message stating: Error: Compile Error: unexpected token: contactOwnerID at line 10 column 82.  I've highlighted the line in my code that is causing the error.

Also - if I hard code in a UserID (mine, for example), the trigger works just fine.  A working example of a modified version of the red code in my Trigger code would be:
User[] AThreshValue = [Select u.A_Producer_Threshold__c from User u where u.Id = '00530000000hxwA' limit 1];

Any thoughts?  I'm sure I'm missing something (which is probably obvious), but I just can get my non-programming-mind around what else I need to do.  And I have spent a good deal of time searching the forums & wikis for information.

Thanks, as always,
RK

Code:
trigger UpdateAThreshold on Contact (before update) {//1
Id contactOwnerID;
for (Contact c :trigger.new)

{//2
Contact[] OwnerIdArray = [Select OwnerId from Contact where Id in :Trigger.new limit 1];
for(Integer i=0;i<OwnerIdArray.size();i++){//3
contactOwnerID = OwnerIdArray[0].OwnerId;
c.l_Total_Widget_B_Production__c = contactOwnerID ; //This is a test line - I did this just to test if I could get a field to update
User[] AThreshValue = [Select u.A_Producer_Threshold__c from User u where u.Id = contactOwnerID limit 1]; //This is my problem area.
//I want to be able to query the A Producer Threshold data from the Contact Owner, but the code will not compile

for(Integer j=0;i<AThreshValue.size();i++){//4
double contactOwnerID2;
contactOwnerID2 = AThreshValue [0].A_Producer_Threshold__c; //This is where I want to query the custom field on the record of the Contact Owner.
c.A_Producer_Threshold__c = contactOwnerID2; // And here is where I want to push the custom field data to the contact object
}//4
}//3
}//2
}//1

 

Message Edited by RK on 09-23-2007 10:31 PM

Message Edited by RK on 09-23-2007 10:31 PM

RKRK
Figured it out, and as suspected, was something small - just needed a colon.

Do I intrepret the colon correctly as an indicator that a variable is going to be passed?

Code:
User[] AThreshValue = [Select u.A_Producer_Threshold__c from User u where u.Id = :contactOwnerID  limit 1];

 

Ron HessRon Hess
Apex Code Variables in SOQL Queries


SOQL statements in Apex Code can reference Apex Code
script variables and expressions if they are preceded by a colon ( :  ).
This use of a local script variable within a SOQL statement is called a bind.
The Apex Code parser first evaluates the local
variable in script context before executing the SOQL statement.
Bind expressions can be used as:
  • The filter literals in WHERE clauses
  • The numeric value in LIMIT clauses
  • The value of the IN or NOTIN operator in WHERE clauses, allowing filtering on a dy

//A simple bind
B=[select id from account where id= : A.id];
//A bind with arithmetic
B=[select id from account
where name= : ('x'+'xx')];

Message Edited by Ron Hess on 09-24-2007 01:34 PM