+ Start a Discussion
YuckleYuckle 

Help with Trigger

This trigger was written by a consultant, but it doesn't work.  I think it is becuase we have account managers whose names are less than 15 characteers long.  It is suppose to add the correct account manager as owner of the new opportunity as long as the account manager field on the account isn't empty.  Comments?  How do I fix if true?  Obvisously, setting 15 to 5, but what is the process to change it?

 

trigger OpportunityTrigger on Opportunity (before insert) {

  for(Opportunity o : Trigger.new) {
    String accountManager = (String)o.Q2_Account_Mgr__c;
    
    if((accountManager != null && accountManager.length() >= 15)) {      
      o.OwnerId = o.Q2_Account_Mgr__c;  
    }
  }

}

_Prasu__Prasu_

Is "Q2_Account_Mgr__c" on opportunity is an lookup field? or is it a text field?

YuckleYuckle

Lookup field

YuckleYuckle

Related to User

_Prasu__Prasu_

If thats an lookup field then it stores the Id and checking a length with 15 is valid way. Its not comparing it with account manager name but with a Salesforce Id of the account manager record which is of length 15 or 18. Logical I don't find anything wrong in code. Could you check the log while inserting a new Opportunity?

YuckleYuckle

Understand.  will do.

YuckleYuckle

Exactly how do i do this.  Never used the system log before.

John De SantiagoJohn De Santiago

From what I can tell of the code there is actually no reason to cast the Account Manager lookup field to a string to test if it has a value or not. This is of course assuming that the Account Manager field is a lookup to a user. 

 

A better trigger would look like this.

 

for (Opportunity o : trigger.New) {

if (o.Q2_Account_Mgr__c != null) {

o.OwnerId = o.Q2_Account_Mgr__c;

  }

}

 

Id data types in Salesforce either contain a valid Id or they are null. So you only have to test for null. Casting it to a string is an extra step that is not needed. Not sure why the code would break though. since the length of an Id is either 15 or 18 characters. You might also check to make sure the trigger is 'active' it could be a developer accidentally de-activated the trigger.

 

 

YuckleYuckle

The IS VALID field is checked and the last editied date is 10 months ago, whcih is when it was written

YuckleYuckle

and what is happening is that the opportunity is being created with the current user as the owner.  So, we're getting opps owned by Support users instaead of account managers.  This uised to work.

YuckleYuckle

As always, the question to ask first is "what has changed/".  The opporutunities now have one of two record types (new business, cross sell).  They might not have had any record types when this trigger was written.  Support should only be creating opps for live customers, all of whom should have an assigned account manager.  All of these opps should be cross sells opps.   Off to a meeting.  back in 1 hour.

John De SantiagoJohn De Santiago

Is Valid is different from active if you look at the class in Salesforce you can see a Status field and it should be set to Active. If you are in Eclipse you open the trigger class then at the bottom there are two tabs. Click on the metadata tab and you can see the xml for that trigger class. You should see a status entry and it should be set to "Active" as well. 

 

If it is active and it is still not working then I would look to see if there are any workflow fields updates that are maybe changing the owner id to the current user.

YuckleYuckle

Have done a little testing....  Neither the original nor the proposed simpler alternative work properly.  I have confirmed that the trigger is being called.  The reason that neither one works is that the IF is always evaluating to FALSE.  If you remove the IF, both triggers fail when SF tries to assign an opportunity owner that is blank.  That is, the field Q2_Account_Mgr__c is always empty at this point.  But, on the associated Account record, where Q2_Account_Mgr is a lookup field linked to USER, the field is NOT empty.

 

Is the problem that since this trigger runs on the Opportunity that it can't see a lookup field on the Account?