+ Start a Discussion
Russell MaunderRussell 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?

SFDCStarSFDCStar

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

Russell MaunderRussell Maunder

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

 

 

SFDCStarSFDCStar

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

vishal@forcevishal@force

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.

Russell MaunderRussell Maunder

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?

 

 

Russell MaunderRussell Maunder

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

SFDCStarSFDCStar

You have to use OwnerId belongs to your Org.

 

opp.OwnerId = '005E0000000QHgq'; 

Russell MaunderRussell Maunder

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

vishal@forcevishal@force

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 :

 

trigger OwnerUpdate on Opportunity (before update) {

// querying a random active user for now, you can add filters if you want a specific user // like where Name = 'any name'

User u = [Select Id From User Where IsActive = true limit 1];

// iterating through all the opportunities that are updated
for (Opportunity o: trigger.new) {
if (o.Opportunity_Sector__c = 'Australasia'){

// when you want to change the owner or any reference field, update the Id field
// so here the field you should be updating is OwnerId and give it appropriate id value
// I am giving the id of the user that I have queried above

o.Owner = u.Id;
    }
  }
}

 This should help you change the owner without any issues. Let me know if you face any!

 

Russell MaunderRussell Maunder

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

kcpluspluskcplusplus
trigger assignOwner on Opportunity (after insert, after update) {
    LIST<ID> oppID = new LIST<ID>(); 
        //get new opp ids
        if(trigger.isInsert){
            for(Opportunity o : trigger.new){
                oppID.add(o.Id); 
                }
               }
    //get old opp ids
        if(trigger.isUpdate){
            for(Opportunity o : trigger.old){
                oppID.add(o.Id);
                }
            }
    //clean list of opportunities to update
    LIST <Opportunity> updateOpps = new LIST<Opportunity>(); 
    //loop through all the necessary opps
    for(Opportunity o : oppId){
        //for each conditional, check the region
        if(o.region__c == 'US East Coast'){
            //set the owner id - add the user id between the parens
            o.OwnerId = '';
            updateOpps.add(o);
        }
        
        if(o.region__c == 'US West Coast'){
            o.OwnerId = ''; 
            updateOpps.add(o);
        }
        
        if(o.region__c == 'Canada'){
            o.OwnerId = ''; 
            updateOpps.add(o);
        }
        
        if(o.region__c == 'Australasia'){
            o.OwnerId == '';
            updateOpps.add(o);
        }
    }
    update updateOpps;
}

 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

Russell MaunderRussell Maunder

thanks but it doesnt let me compile it

 

Error: Compile Error: Expression cannot be a statement at line 37 column 13 

kcpluspluskcplusplus

Okay, can you copy and paste the exact code you tested?

 

--KC