1

How to assign an auto number value to relation ship id? is it possible

I have Custom Objects for example Parent_c which has a field 'NAME'(which is similar to Id) which is autonumber datatype. I have an another set of child custom objects (Child_c) which has a field 'ID' which gives the Master-Detail relation ship

Below here i have used aggregate query to fetch max of the id,

  List<AggregateResult> maxId = [SELECT Max(Name)Id FROM Parent__c ];
  Object maximumId = null;

  if(maxId!=null && !maxId.isEmpty()){

        maximumId =    maxId.get(0).get('Id');
        System.debug('Maximum Id ::'+maximumId );                    
  }

  Child__c c = new Child__c();
  c.Id = maximumId; **// But here i will not be able to assign object value to Id filed.**

What is the way i need to proceed? Is it right way i'm doing it?

Then how can i link the records with Parent to Child.

Kindly somebody help me and let me know if more information is needed.

Thanks advance for your answers

1 Answer 1

0

You are grabbing the Name of the Parent__c that has the maximal value. Since this is an aggregation, this Name may belong to multiple records. But since you are auto-numbering the name field, there is a 1-1 correspondence between Name and Id. I see two routes on getting the Id of the record with maximal Name value:

(1) Grab the maximal Name value via an AggregeResult query (like you've done above), and then perform one more query to grab the unique record corresponding to the Name you aggregated (because you really need the Id of the record with maximal Name value, not the Name itself).

Example:

List<AggregateResult> arList = [SELECT Max(Name)maxName FROM Parent__c];
string maxName = '';

if(arList!=null && !arList.isEmpty())
    maxName = (string)arList[0].get(maxName);   // <--- make sure to cast the data type!

list<Parent__c> parentList = [SELECT id,Name /* important fields */
                              FROM Parent__c
                              WHERE Name=:maxName
                              LIMIT 1];
Parent__c myParent = new Parent__c();
if(parentList!=null && !parentList.isEmpty())
    myParent = parentList[0];

Or (2): Find the parent record via one query. (This can only be done because there is a 1-1 map between Parent__c.Name and Parent__c.id! Make sure you understand why.)

Example:

list<Parent__c> parentList = [SELECT id,Name /* import fields */
                              FROM Parent__c
                              ORDER BY Name DESC // <-- grabs the Name with maximal value
                              LIMIT 1];  
Parent__c myParent = new Parent__c();
if(parentList!=null && !parentList.isEmpty())
    myParent = parentList[0];

Using either method, you end up with the desired Parent__c record. Next, just attach the Id to the Child__c record.

Child__c c = new Child__c();
c.Id = myParent.id;
4
  • Not entirely correct. There may be a many-to-one relationship between the Name and Id value; since administrators are free to change the starting point of the auto-number field at any time, this can cause overlap. It is never, ever safe to assume that Name is somehow globally unique.
    – sfdcfox
    Sep 27, 2013 at 21:21
  • @sfdcfox With above given 2nd option, currently suits my requirement. As per your valid comments 'since administrators are free to change the starting point of the auto-number field at any time' what is the best way to mitigate this issue?. Kindly guide us how to proceed?
    – Arun
    Sep 28, 2013 at 4:43
  • my method surely uses the fact that "This can only be done because there is a 1-1 map between Parent__c.Name and Parent__c.id! Make sure you understand why." If you have a many-1 relationship, you need a way to tease out the record you actually want. This is completely determined on how everything is set up. Oct 2, 2013 at 18:00
  • If you are always looking for the oldest record, you can compare records by CreatedDate Oct 2, 2013 at 18:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .