function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
gvgv 

Auto Number

Hi

 

My requirement is as follows

 

I have a master detail relationship between 2 objects. For 1 master record there can be multiple detail record. I need to find the highest detail record for a particular master record. If there are 3/4 detail for 1 master I need to find out the highest detail record.

 

The detail record has a field a auto number field which is listed under the standard fields.  How would I extract the highest auto number that was inserted (in a detail record) for a master record

 

Also the apex explorer does not list the auto number field . how would I view it?

 

Thanks much for all your time 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
WhyserWhyser

Okay, as for not being able to see the field in Sandbox, perhaps your APEX explorer is set to log into your production instance? If you go to Tools -> Options, is your Endpoint pointing to https://www.salesforce.com/services/Soap/u/14.0 or is it pointing to https://test.salesforce.com/services/Soap/u/14.0? If it's pointing to test, then you are looking at your sandbox version in Apex Explorer

 

As for your problem I hope this is what you were looking for

trigger checkHighestRecord on customAccountObject__c( after insert ) { List< customAccountObject__c > caoListToUpdate = new List< customAccountObject__c >(); for( customAccountObject__c cao : Trigger.new ) { // Get all the related custom objects associated to the account // I'm ordering the results by ID desc because the newest ID's should contain the largest autonumber // This way you can set the first result to be checked while you uncheck the rest of the results List< customAccountObject__c > caoList = [select id, Account__c, autonumber_field__c, check_if_highest_number__c, other_fields__c from customAccountObject__c where Account__c = :cao.Account__c order by id desc]; for( Integer i = 0; i < caoList.size(); i++ ) { // Set the first one to be checked. all the rest are false if ( i == 0 ) caoList[i].check_if_highest_number__c = true; else caoList[i].check_if_highest_number__c = false; } // Put the updated results into a list to be updated later caoListToUpdate.addAll( caoList ); } // Update the items update caoListToUpdate; }

 

 

 

Message Edited by Whyser on 03-11-2009 03:32 PM

All Answers

WhyserWhyser

What do you plan to do when you know what the highest detail record is?

 

As for APEX explorer, I'm not sure why it's not showing up. It SHOULD be there. You're not looking at a Sandbox version of your org data, are you?

gvgv

Once I know the latest record I am planning to make a checkbox in that record . In other words out of the all the detail records for a particular record identify the highest auto number and mark a checkbox in that record

 

I am looking at the sandbox apex explorer. 

WhyserWhyser

Okay, as for not being able to see the field in Sandbox, perhaps your APEX explorer is set to log into your production instance? If you go to Tools -> Options, is your Endpoint pointing to https://www.salesforce.com/services/Soap/u/14.0 or is it pointing to https://test.salesforce.com/services/Soap/u/14.0? If it's pointing to test, then you are looking at your sandbox version in Apex Explorer

 

As for your problem I hope this is what you were looking for

trigger checkHighestRecord on customAccountObject__c( after insert ) { List< customAccountObject__c > caoListToUpdate = new List< customAccountObject__c >(); for( customAccountObject__c cao : Trigger.new ) { // Get all the related custom objects associated to the account // I'm ordering the results by ID desc because the newest ID's should contain the largest autonumber // This way you can set the first result to be checked while you uncheck the rest of the results List< customAccountObject__c > caoList = [select id, Account__c, autonumber_field__c, check_if_highest_number__c, other_fields__c from customAccountObject__c where Account__c = :cao.Account__c order by id desc]; for( Integer i = 0; i < caoList.size(); i++ ) { // Set the first one to be checked. all the rest are false if ( i == 0 ) caoList[i].check_if_highest_number__c = true; else caoList[i].check_if_highest_number__c = false; } // Put the updated results into a list to be updated later caoListToUpdate.addAll( caoList ); } // Update the items update caoListToUpdate; }

 

 

 

Message Edited by Whyser on 03-11-2009 03:32 PM
This was selected as the best answer
gvgv

Thanks . I just realised that. Thanks again.

 

I will implement your logic and see if it is working

 

 

Appreciate you spending time

gvgv

Thanks Whyser. Its working. Your logic helped me. Initially in a different logic I was getting a duplicate id list error now thats gone.

 

 

WhyserWhyser

Oops, yeah I think I forgot to write i++ in the for loop, which would have made it run indefinitely. I have fixed that, unlesss the error was due to something else!

gvgv
I added the i++ in the loop so luckily I dint get into the indefinite loop. :womanhappy:But thanks for updating it. No the duplicate id error was not due to that. That was in a earlier logic. not with this:womanvery-happy:
Message Edited by gv on 03-11-2009 03:36 PM
gvgv
I ran into governor limits when executed this code. I think its because the SOQL query is executed inside a for loop?

Can you pls let me know how to avoid this?
Message Edited by gv on 03-16-2009 02:01 PM