+ Start a Discussion
NordbergNordberg 

Brand New to Apex and Coding in general - Simple trigger help

Hi,

 

I am currently trying to update a field on an opportunity record after the opportunity has been inserted or updated from the opportunity owners - user record.  I really don't know what I am doing but this is what I have pieced together.

 

 

This is the trigger

 

trigger InsuranceUpdate on Opportunity (after insert, after update) { for (Opportunity o : Trigger.new){ User u =[Select IS_Consultant__c from User Where Id=: o.OwnerId]; o.Iteam_Member__c = o.Owner.IS_Consultant__c; } }

 

 

This is the test class

public class testOpp{ static testMethod void InsuranceUpdate() { //find the test opp //flip it's status and update // Opportunity opp = new Opportunity(); opp.Name= 'JD Nordberg30'; opp.StageName = 'Active'; opp.CloseDate = Date.today(); insert opp; for (Opportunity o : [SELECT Id FROM Opportunity WHERE Name = 'JD Nordberg30']) { o.StageName = 'Prospect' ; update o; } } }

 

 

I am receiving the error below 

 

 

Description Resource Path Location Type System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, InsuranceUpdate: execution of AfterInsert caused by: System.Exception: Record is read-only Trigger.InsuranceUpdate: line 4, column 9: [] testOpp.cls InsuranceUpdate/src/classes line 11 Force.com run test failure

 


 

 

Also, how do I run a test of my code in eclipse?

 

Any help would be great


 

 

Best Answer chosen by Admin (Salesforce Developers) 
AlsoDougAlsoDoug

Nordberg,

 

1. You can't change the value of the object from Trigger.new in an after insert/update. The object has already been inserted into the db.

 

You should change the value before the update/insert (which looks like it should work for what your doing).

 

2. To run the test right click on the file with the test and select "Run Tests" from the Force.com menu.

All Answers

AlsoDougAlsoDoug

Nordberg,

 

1. You can't change the value of the object from Trigger.new in an after insert/update. The object has already been inserted into the db.

 

You should change the value before the update/insert (which looks like it should work for what your doing).

 

2. To run the test right click on the file with the test and select "Run Tests" from the Force.com menu.

This was selected as the best answer
AlsoDougAlsoDoug
On the chance I am wrong on point 1, the Force documentation on triggers lays out when you can change data. Double check that.
CaptainObviousCaptainObvious

Im not sure what type of fields IS_Consultant__c and ITeam_Member__c are, so I'll just assume they are checkboxes...

 

Sample Trigger:

 

trigger InsuranceUpdate on Opportunity (after insert, after update) { List<Opportunity> OppsToUpdate = new List<Opportunity>(); for (Opportunity o : Trigger.new){ User u =[Select Is_Consultant__c from User Where Id=: o.OwnerId]; //You'll want to update the opportunity ITeam Member only if //the value is not equal to the Is Consultant //Otherwise, you'll run an endless loop! if ( o.ITeam_Member__c != u.Is_Consultant__c) { opportunity opp = new opportunity( id = o.id, ITeam_Member__c = u.Is_Consultant__c ); //Be bulk-safe...

OppsToUpdate.add(opp); } }

//...run DML statements outside for loops! update OppsToUpdate; }

 

and the test class:

 

 

public class testOppUpdate{ static testMethod void InsuranceUpdate() { //Set up a profile (you may want to try different profiles) Profile p = [select id from profile where name='System Administrator']; //Create a new user and set Is Consultant to 'true' User u = new User(alias = 'user1', email='user1@abc.com', emailencodingkey='UTF-8', lastname='Test', languagelocalekey='en_US', localesidkey='en_US', timezonesidkey='America/New_York', is_consultant__c=true, profileid = p.id, country='United States', username='testUser1@abc.com'); insert u; //Create another user and set Is Consultant to 'false' User u2 = new User(alias = 'user2', email='user2@xyz.com', emailencodingkey='UTF-8', lastname='Test', languagelocalekey='en_US', localesidkey='en_US', timezonesidkey='America/New_York', is_consultant__c=false, profileid = p.id, country='United States', username='testUser2@abc.com'); insert u2; //Create an opportunity, set the owner to new user 1 //Leave ITeam Member unchecked Opportunity opp = new Opportunity(); opp.Name= 'JD Nordberg30'; opp.StageName = 'Active'; opp.CloseDate = Date.today(); opp.OwnerId = u.id; opp.ITeam_Member__c = false; //Test Creation of new opportunity (after insert) insert opp; //Verify that the ITeam_Member__c is now checked at the opportunity Boolean result; result=[select ITeam_Member__c from Opportunity where id=:opp.id].ITeam_Member__c; System.AssertEquals(true,result); //Change the opportunity owner //and Test update of opportunity (after update) opp.OwnerId = u2.id; update opp; //Because we changed the opportunity owner, verify that //the ITeam_Member__c is now unchecked at the opportunity result=[select ITeam_Member__c from Opportunity where id=:opp.id].ITeam_Member__c; System.AssertEquals(false,result); } }

Good Luck!