You need to sign in to do that
Don't have an account?

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
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?
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?
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.
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?
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 "
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.
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.
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); } } }
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.