You need to sign in to do that
Don't have an account?
Russell Maunder
New to Apex, issue with simple field update
Hi There, as the title says I am just starting to get my head around Apex and triggers. What I am trying to do is create a simple trigger on an Opportunity that will update the owner of that opportunity based on a custom field I have (Opportunity_Sector__c) regardless of who loads the Opportunity
So far I have this but I am getting an error:
trigger OwnerUpdate on Opportunity (before update) { for (Opportunity o: trigger.new) { if (o.Opportunity_Sector__c = 'Australasia'){ o.Owner = 'User Name'; } } }
error is
Error: Compile Error: Illegal assignment from String to SOBJECT:User at line 5 column 1
any help here on what I am doing wrong?
Hi Russel,
1. You have get Boolean value in If condition
if (o.Opportunity_Sector__c == 'Australasia'){
2. You cann't use directly owner : use o.Owner.Username = 'User Name';
o.Owner.Username = 'User Name';
}
}
}
It helps for you, mark as Solution.
Thanks,
Pandu
Thanks, that has helped with the coding but the trigger now produces the error when i try to edit and save an opportunity
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger OwnerUpdate caused an unexpected exception, contact your administrator: OwnerUpdate: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.OwnerUpdate: line 5, column 1
Try this:
trigger OwnerUpdate on Opportunity (after update) {
set<Id> oppId = new set<Id>();
for (Opportunity o: trigger.new)
oppId.add(o.id);
map<Id,Opportunity> oppMap = new map<Id,Opportunity>([select Id, Opportunity_Sector__c, OwnerId from Opportunity where Id IN: oppId]);
List<Opportunity> oppUpdate = new List<Opportunity>();
for(Opportunity opp : oppMap.values()){
if(oppMap.size() > 0 && opp.Opportunity_Sector__c == 'Australasia'){
opp.OwnerId = '005E0000000QHgq';
opp.Opportunity_Sector__c = 'hello';
oppUpdate.add(opp);
update oppUpdate;
}
}
}
If this reply helps and resolves your problem mark it has a solution.
Regards,
Pandu
It gives you a null pointer exception because, by default in a trigger.new list you don't have access to fields on reference objects.
So in your case, when you try to access, o.Owner.UserName, it cannot access UserName field on Owner.
Thanks for the help so far.
So should i create a list of all usernames first? or will that not address the issue of trying to update the Owner.Username field?
That gives me an even more crazy error
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger OwnerUpdate caused an unexpected exception, contact your administrator: OwnerUpdate: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 006M0000004bIwHIAU; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []: Trigger.OwnerUpdate: line 11, column 1
You have to use OwnerId belongs to your Org.
opp.OwnerId = '005E0000000QHgq';
Sorry, errors getting worse
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger OwnerUpdate caused an unexpected exception, contact your administrator: OwnerUpdate: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 006M0000004bIwHIAU; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OwnerUpdate: maximum trigger depth exceeded Opportunity trigger event AfterUpdate for [006M0000004bIwH] Opportunity trigger event AfterUpdate for [006M0000004bIwH] Opportunity trigger event AfterUpdate for [006M0000004bIwH] Opportunity trigger event AfterUpdate for [006M0000004bIwH] Opportunity trigger event AfterUpdate for [006M0000004bIwH] Opportunity trigger event AfterUpdate for [006M0000004bIwH] Opportunity trigger event AfterUpdate for [006M0000004bIwH] Opportunity trigger event AfterUpdate for [006M0000004bIwH] Opportunity trigger event AfterUpdate for [006M0000004bIwH] Opportunity trigger event AfterUpdate for [006M0000004bIwH] Opportunity trigger event AfterUpdate for [006M0000004bIwH] Opportunity trigger event AfterUpdate for [006M0000004bIwH] Opportunity trigger event AfterUpdate for [006M0000004bIwH] Opportunity trigger event AfterUpdate for [006M0000004bIwH] Opportunity trigger event AfterUpdate for [006M0000004bIwH] Opportunity trigger event AfterUpdate for [006M0000004bIwH]: []: Trigger.OwnerUpdate: line 12, column 1
Hi Russell,
So if I am not wrong, you are only trying to update the Opportunity owner to some user.
I will share the code for that , please make appropriate changes :
This should help you change the owner without any issues. Let me know if you face any!
Thanks for the help so far and apologies if this is getting annoying but i am still having trouble understanding apex and how i can edit this to achieve the results i want.
I have 4 different sales sectors (US East Coast, US West Coast, Canada, Australasia) and i want to update the opportunity owner to the sales person responsible for each of those sectors (salesperson 1, salesperson 2, salesperson 3, salesperson 4) whenever an opportunity is created or edited, regardless of who created or edited it initially.
I just cant see in the code you provided where i can enter the user name? or does it need to be the UserID?
many thanks
Russell
You could give that one a try, you have to change the region__c field to however you define those sales sectors, and add the user id for the users for the ownerid
--KC
thanks but it doesnt let me compile it
Error: Compile Error: Expression cannot be a statement at line 37 column 13
Okay, can you copy and paste the exact code you tested?
--KC