You need to sign in to do that
Don't have an account?
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
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; }
All Answers
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?
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.
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; }
Thanks . I just realised that. Thanks again.
I will implement your logic and see if it is working
Appreciate you spending time
Thanks Whyser. Its working. Your logic helped me. Initially in a different logic I was getting a duplicate id list error now thats gone.
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!
Can you pls let me know how to avoid this?