You need to sign in to do that
Don't have an account?
Trigger for OpptType
I have posted this a question about this trigger problem once before. I want to write a trigger for whenever an opportunity is created. When it is created I would like to check the role of the opportunity owner and based on the owner's role I would like the "type" field to change accordingly. If the owner's role is "Sales" I would like the "type" field to default to "initial sale". Likewise, if the owner's role is "Success" I would like the "type" field to default to "add/subtract". Below is a sample code I received from another post I made by another contributor. I have tried to implement it but it does not work.
trigger myOppType on Opportunity (after insert)
{
List<Opportunity> oppList = new List<Opportunity>();
for(Opportunity opp : trigger.New) {
if( opp.owner.userrole.name == 'Success') {
opp.type = 'add/subtract';
oppList.add(opp);
}
if( opp.owner.userrole.name == 'Sales') {
opp.type = 'initial Sale';
oppList.add(opp);
}
}
if(oppList.size() > 0 ) {
Database.update(oppList, false);
}
}
You probably need to query for the UserRole's Name value. The trigger.new would not contain the values across relationships, but would instead return a null value.
Try this... List<Opportunity> oppList = [select Id, Owner.UserRole.Name from Opportunity where Id in trigger.new];
You could also add a debug statement to write the value to a debug log to see why your fields aren't being set as expected.
Can you give me an example of how to write to the debug log?
it's just system.debug(aString);
it's one of the system methods
by including it as below, you can see what your IF statements are comparing.
for(Opportunity opp : trigger.New) {
system.debug('UserRole Name is ' + opp.owner.userrole.name);
if( opp.owner.userrole.name == 'Success') {
opp.type = 'add/subtract';
oppList.add(opp);
}
if( opp.owner.userrole.name == 'Sales') {
opp.type = 'initial Sale';
oppList.add(opp);
}
}