1

I have a below requirement -

  1. I have two custom objects Product and childproduct. childproduct is a related list (lookup) to Product . When i will create a childproduct i want the name should be appended with an autonumber.

Example -

Product1
    childproduct1--0
    childproduct2--1
    childproduct3--2

Product2
    childproduct1--0
    childproduct2--1
    childproduct3--2

If i create a childproduct of any existing Product, the autonumber should start where the number is ended. For Ex - from above it should start with

childproduct4--3

How to create this type of autonumber?

7
  • Possible duplicate of Configure Auto-Number with initials related to another object Jan 7, 2016 at 8:08
  • The key word here is an auto. Such auto-numbers are generated automatically, you can only define a custom format (limited). Jan 7, 2016 at 8:09
  • 1
    i want to use custom auto number rather going for standard one.. Jan 7, 2016 at 8:13
  • Could you update your question and mention a custom auto-number field? Thanks. Jan 7, 2016 at 8:15
  • You can add one more field called child number, which would be like 1,2,3 etc. In case if you are creating new child -- you just query your child for parent with maximal value of this field (in our case -- its 3) and set new name based on this value as well as set incremented value into this techical field (as 4 in our case). Good point in that field is that you can use limit 1 and order by to query only single record. (cause you need to query more in case if you order by name of child object). All the logic may happen in before insert trigger. Ensure that catch multiple records insert
    – kurunve
    Jan 7, 2016 at 8:18

1 Answer 1

2

One way to do this would be to use a combination of an autonumber field and a custom formula field.

First, the autonumber field simply uses {0} as the text format. Let's call it Auto_Number__c.

Then for the formula.

I would use a Text return type and have the formula be something like:

"childproduct" & Parent_Product__r.Name & "--" & Auto_Number__c

This would append the hard coded strings childproduct and -- to the parent product name and the autonumber value from the child.

The only downside is that autonumber does not restart for each parent. But you would have a sequential order of children.

One caveat to this strategy. If you plan to use this field for searches, queries, report filters, etc. any formula field that contains a cross-object reference is by definition nondeterministic, therefore, cannot be indexed, removing one potential query optimization in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.