4

I would like to create a child record for the most recently created record in a list. Currently there would only be the possibility of one of the potential parents existing, so I was using a simple query to pull that one and create a child for it. But now, there is a potential of multiple types of the potential parent existing, and I would like to create a child for just the latest one created.

Here is the portion of my current code which is producing this list :

List<Client_Project__c> lstCP = new List<Client_Project__c>([Select c.Sales_Leader__c, c.Related_Opportunity__c, c.Project_Type__c, 
           c.Project_Status__c, c.Project_Scope__c, c.Name, c.Lead_Country__c, c.Account__c, c.Account__r.Name
           From Client_Project__c c Where c.Account__c in : setAccIds AND c.Related_Opportunity__c in :setOppIds]);

Could I accomplish this by just doing an Order By on the objCP query and just doing a Limit 1 return ? Would that produce the most recently created record in that query ?

Thank you very much for your help and time.

2 Answers 2

4

Yes adding "Order By CreatedDate DESC Limit 1" to that query would return the last created Client_Project__c record fitting your where clause. You can test this in Soqlxplorer or by executing the query in execute anon to ensure that this is what you are after.

also try not to use hardcoded Ids

 oClientProj.RecordTypeID = '0121100000006rc';

Naughty Naughty :)

4
  • Thanks so much Mitch ! btw : I know what you mean about hardcoding the record type - but what would be the best way to do that ?
    – Zoom_v
    Commented Oct 10, 2013 at 14:11
  • 1
    You can run a SOQL query to find the recordtype by the DeveloperName. Commented Oct 10, 2013 at 14:20
  • Yep as Dunc44 said you can run a simple query on DeveloperName or see my other comment for some other options.
    – Mitch Hunt
    Commented Oct 10, 2013 at 14:28
  • @Dunc44 - Thank you ; you can check my other comment I just now put as a response to Mitch's suggestion. Again, I could be completely wrong here with what you guys are proposing.
    – Zoom_v
    Commented Oct 10, 2013 at 14:32
4

With Relation to the recordtype if you only need to get a single recordtype just use a single Soql Query i.e.

ID ClientProjRecordType = [SELECT Id FROM RecordType WHERE sobjectType='Client_Project__c' DeveloperName = 'Your_RecordType_Name'].Id;

I tend to like using Developername because this is less likely to be played around with by system admins.

If you need to retrieve multiple recordtypes in a single class I like to use a map i.e.

        mapRecordTypes = new map<String, Id>();

        for(RecordType sRecordType : [SELECT Id, DeveloperName FROM RecordType WHERE sobjecttype = 'Client_Project__c' OR sobjecttype = 'Other_Object__c'])
        {
            mapRecordTypes.put(sRecordType.DeveloperName, sRecordType.Id);
        }

Then you can use a get method to retrieve the correct recordtype for any object with a single query. I.e.

oClientProj.RecordTypeID = mapRecordTypes.get('Your_RecordType_Name');

If you really want to get cool with it then you can put this method in a global utility class, this is something that we do all the time because you constantly need to query recordtypes. I.e.

public with Sharing Class Utilities
{
    public static map<string,string> getRecordTypes(List <String> objectTypes)
    {
        Map <String, String> tmp = new Map <String, String>();

        try
        {
            for (RecordType rt : [SELECT Id, Name, DeveloperName FROM RecordType WHERE sObjectType IN :objectTypes])
            {
                tmp.put(rt.ID, rt.DeveloperName);
                tmp.put(String.valueOf(rt.ID).substring(0,15), rt.DeveloperName);
                tmp.put(rt.DeveloperName, rt.ID);
                tmp.put(rt.Name, rt.ID);
            }
        }
        catch (Exception e)
        {
            Debug(e.getMessage());
        }

        return tmp;
    }
}

Now you can call this piece of code from any piece of apex you have in order to get a map of your recordtypes for any object so you never have to type all that code again. I.e.

Map <String,String> recordtypes = Utilities.getRecordTypes(new String[]{'Account','Contact'});
6
  • Very thorough, great explanation Commented Oct 10, 2013 at 14:30
  • I might be completely wrong here, but I think you maybe don't understand why I was hardcoding the record type. If there weren't any already existing Client_Project records returned I would need to create one, and I wouild need it to be that recordtype. That's why I needed to hardcode it. Again, I could be completely wrong here on your intentions....? I'm sorry if I am.
    – Zoom_v
    Commented Oct 10, 2013 at 14:31
  • 2
    You do the SOQL query to get the record type you need. Then you do your logic to see if you need tyo create a new Client_Project__c record, if you do, then you create the new record with RecordTypeId = YourSoqlQueryResult.Id (The record type that is returned from your original SOQL query) Commented Oct 10, 2013 at 14:35
  • 2
    So in general practice it is best to avoid hardcoding any type of ID in code see #10 of the best practices guide for more information wiki.developerforce.com/page/Apex_Code_Best_Practices but basically your code is more flexible, it can be deployed as part of packages, to other instances and if the recordtypeId is somehow changed (maybe someone accidently deleted it then recreated with the same name) the code will still work You can still assign that recordtypeId once you have queried via soql but you should always ensure that you retrieve it dynamically as shown in the above examples
    – Mitch Hunt
    Commented Oct 10, 2013 at 14:37
  • ah, ok - I'm sorry for the stupid question. I've never used DeveloperName before, and for a minute I thought you guys were suggesting something different. Thanks again !
    – Zoom_v
    Commented Oct 10, 2013 at 14:38

You must log in to answer this question.

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