Auto Naming for Opportunity, Lead, etc.


In standard installation, for every opportunity, lead, case, etc. we need to enter manual names during new object creation. These causes different naming conventions from sales reps to sales reps. We want to standardize these namings. For example, we want to name any opportunity as Opp. - Sales Rep. Name - Opportunity Number. How can we make this work? Do you have any suggestions and example codes?


The best thing to do would be to remove the name field from the editview in studio and add an on save logic hook to automatically set the name every time an opportunity is saved.

If you create a php file (what you name it doesn’t matter) in custom/Extension/modules/Opportunities/LogicHooks then add a line like the following:

$hook_array[‘after_save’][] = Array( 10, ‘Set Name’, ‘/custom/modules/Opportunities/NameHook.php’, ‘NameHook’, ‘setName’);

then after you do a repair and rebuild Suite will know to call the setName method of the NameHook class which can be found at custom/modules/Opportunities/NameHook.php everytime an Opportunity is saved.

You also need to create the above class. The setname method should look something like this:

function setName($bean, $event, $arguments)
    $bean->name = 'Opp. - ' . $bean->assigned_user_name . ' - ' . $bean->opportunity_number;    

I’m assuming opportunity number is a field you’ve added to the opportunity and just guessed at how it will be named. You can see all the fields and their correct names in this file: /cache/modules/Opportunities/OpportunityVardefs.php

If the number is something more advanced than that you can add more logic to deal with it. Hopefully that’s enough information to get you started.

1 Like

Thank you very much for explanations. I will try it.

Do you have additional suggestions on how I can add autonumbering for Opportunity Number like in Invoice and Quotes?

First thing to do is add a field for it using studio if you don’t have one already. Then the simplest thing would be to alter the table in your database so the field is auto incremented.

If you don’t feel confident doing that you could add code to your logic hook to query the database to get the max value for that field and add 1 to it.

I am not well on coding. Do you have any example code for the logic hook to get the max. value and add 1 to it?

If you’re not confident coding it maybe best to get a consultant to carry out the work for you. We can put you in touch with one if you fill in this form:

Thank you very much. I will do it.

Before that, for altering db to autoincrement, do I need to put the field on edit view, or only adding the field to db is enough?

I wouldn’t add it to the edit view as then the user would be able to change the value of it which I presume you wouldn’t want.

Tried to do the above as it is, and cannot get it to work on my install. I have checked the log file, and there doesn’t seem to be any errors. Is there a separate area I should be turning Logic Hooks on?