+ Start a Discussion
Scott0987Scott0987 

Help with createdby.id in code

I am working on a project that I need to compare one item to another based on a createdby.id.  Here is the code I have written, but for some reason it does not find the createdby.id of the initial object.  Here is the code I have.

 

    Set<ID> cbid = new Set<ID>();
    for (ChargentSFA__Transaction__c cb : trigger.new) {
        cbid.add(cb.createdby.id);
                }

 

timeclock__c tm = [select id, name from Timeclock__c where in_out_day_today__c=1 and in_out_month_this_month__c=1 and in_out_year_this_year__c=1 and Out_Date_Time__c=false and createdby.id in :cbid limit 1];

Best Answer chosen by Admin (Salesforce Developers) 
SamuelDeRyckeSamuelDeRycke
SELECT CreatedById FROM testobject__c

 

 

It is one word, niet CreatedBy.Id, unless you have a lookup/master relatoin to the user object which you named CreatedBy, but that would amaze me.

 

 

 

All Answers

SamuelDeRyckeSamuelDeRycke
SELECT CreatedById FROM testobject__c

 

 

It is one word, niet CreatedBy.Id, unless you have a lookup/master relatoin to the user object which you named CreatedBy, but that would amaze me.

 

 

 

This was selected as the best answer
Scott0987Scott0987

Thank you, Thank you, Thank you.  I am pretty new to this and appreciate the help.

sfdcfoxsfdcfox

As a clarification, CreatedBy.Id is valid, but only if you query for it. CreatedBy is of the type Schema.SObject, and will be populated if you query for it:

 

for(ChargentSFA__Transaction__c cb: [SELECT Id, CreatedBy.ID FROM ChargentSFA__Transaction__c WHERE Id IN :Trigger.new]) {
   ...
}

But in this case is unnecessary, because the previous post advises that you use CreatedById instead of CreatedBy.Id, which is the correct answer. There are times when you might use the "non-ID" form, especially when you're doing something like this:

 

for(contact c:[select id,account.name from contact]) {
  c.accountname__c = c.account.name;
}

This is a trivial example you'd probably never use, but if you find yourself needing a parent's value and you want to save a SOQL query, you can use this method.