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
DarbDarb 

Trigger - update record owner with new field falue

I am trying to write a trigger that will update the owner of a record with a value in one of the records fields.  I have never written a trigger before and this is what I have come up with so far.

 

 

trigger changeorderowner on Oracle_Order__c (before insert) { //Change the order owner before saving to the data base //Select the new orders where the current owner is not equal to the Owner as noted in the ERP system List<Oracle_User_Name__c> changeorderowner = [SELECT Oracle_User_Name__c From Oracle_Order__C WHERE Oracle_User_Name__c != Owner FOR UPDATE]; for (Oracle_User_Name__c li: changeower) { if (Oracle_User_Name__c != owner) { owner = Oracle_User_Name__c;} update Oracle_User_Name__c; }

 Any help would be apprecaited  

 

David81David81

Do you want this trigger to go out and work on other records or just the record(s) that set off the trigger?

 

Your custom field, is it a picklist, lookup field or freeform text entry?


 

 

DarbDarb
I want it to go out and check any records that meet the criteria of record owner not equal to a custom field.  The custom field is a free form text field.  There is one caveat.  The records are imported from another system.  They will never be created in Salesforce. 
David81David81

Will they ever change after import? Or do you just want to set the correct owner upon import?

 

Are there some records already in the system that need correction or are you just worried abour records going forward?

Message Edited by David81 on 03-09-2010 05:36 AM
DarbDarb
They should not change after import, we just need to set them to the corret owner when they are imported.  We do not currently have any records in the system, this is a hurdle we encountered and wanted to address before we started putting records in the system. 
David81David81

Ok. That makes it a bit easier.

 

 

trigger changeorderowner on Oracle_Order__c (before insert) { //Change the order owner before saving to the data base //Create a set of Names to get Ids for Set<string> ownernames = new Set<string>(); //Create of Map of Names and their respective Ids Map<string,id> ownerids = new Map<string,id>(); //Add Oracle_User_Names__c to the Set for(Oracle_Order__c o : trigger.new){ if(ownernames.contains(o.Oracle_User_Name__c){ } else{ ownernames.add(o.Oracle_User_Name__c); } } //Get the Ids for respective Oracle_User_Names__c for(user u : [SELECT Id,Name FROM User Where Name IN :ownernames]){ ownerids.put(u.Name,u.Id) } //Set proper owner if not already set for(Oracle_Order__c o : trigger.new){ if(o.OwnerId!=ownerids.get(o.Oracle_User_Name__c){ o.OwnerId = ownerids.get(o.Oracle_User_Name__c); } } }

 I'm a beginnger myself, so there may be a better way of doing this, but I'm pretty sure this will do what you want.

 

 

DarbDarb
Still no success.  Does anyone else have any ideas?
David81David81

By "no success" what do you mean exactly?

 

Is the code throwning an error? If so, what is the error?

 

Does it just not just not perform the expected action?

DarbDarb

I cleaned the code up a little to get it to compile, Then I started getting this error and am not sure how to get past it.

 

"Error: Compile Error: expecting right curly bracket, found 'EOF' at line 0 column -1 "

David81David81

Sounds like you're missing a closing "}"

 

Try putting one on the last line. If that doesn't work, just post the "cleaned up" code and we'll see what's up.

DarbDarb
That did the trick.  Now I'm trying to test the trigger to so I can deploy it to my production instance.  How is this best accomplished?
David81David81

Well, you'll need a test class written before you can deploy.

 

As far as just testing the functionality, just insert some new Oracle_Order__c records with the Oracle_User_Name__c field filled in with the name of a user. Should update the owner to that user.

DarbDarb

I have pushed records to the object several times, tweaking the code a couple of different ways to see if I had missed something.  At the moment it is still not updating the record

 

Here is the code as it stands.

 

 

trigger changeorderowner on Oracle_Order__c (before insert) { //Change the order owner before saving to the data base //Create a set of Names to get Ids for Set<string> ownernames = new Set<string>(); //Create of Map of Names and their respective Ids Map<string,id> ownerids = new Map<string,id>(); //Add Oracle_User_Names__c to the Set for(Oracle_Order__c o : trigger.new){ if(ownernames.contains(o.Sales_Rep__c)){ } else{ ownernames.add(o.Sales_Rep__c); } } //Get the Ids for respective Oracle_User_Names__c for(user u : [SELECT Id,Name FROM User Where Name IN :ownernames]){ ownerids.put(u.Name,u.Id) ; //Set proper owner if not already set for(Oracle_Order__c o : trigger.new){ if(o.OwnerId!=ownerids.get(o.Sales_Rep__c)) o.OwnerId = ownerids.get(o.Sales_Rep__c); } } }

 

 

 

David81David81

I did notice a couple brackets out of place and fixed those.

 

Probably time to add in some debug statements. Something like:

 

trigger changeorderowner on Oracle_Order__c (before insert) { //Change the order owner before saving to the data base //Create a set of Names to get Ids for Set<string> ownernames = new Set<string>(); //Create of Map of Names and their respective Ids Map<string,id> ownerids = new Map<string,id>(); //Add Oracle_User_Names__c to the Set for(Oracle_Order__c o : trigger.new){ if(ownernames.contains(o.Sales_Rep__c)){ } else{ ownernames.add(o.Sales_Rep__c); } } system.debug('*****ownernames*****'+ownernames); //Get the Ids for respective Oracle_User_Names__c for(user u : [SELECT Id,Name FROM User Where Name IN :ownernames]){ ownerids.put(u.Name,u.Id); } system.debug('*****owners and their ids*****'+ownerids); //Set proper owner if not already set for(Oracle_Order__c o : trigger.new){ system.debug('*****ownerID*****'+o.OwnerId); system.debug('*****Sales Rep ID*****'+ownerids.get(o.Sales_Rep__c)); if(o.OwnerId!=ownerids.get(o.Sales_Rep__c)) o.OwnerId = ownerids.get(o.Sales_Rep__c); } }

 

 

 

 

Try that and turn on debug logging for the user doing the inserts. Then check the logs to make sure the lists and maps are being set properly.