Visitor
should be Visitor__c
. All custom objects are suffixed with __c
, as should all of your custom fields on either standard or custom objects. So, your code would look something like this:
Visitor__c newVisitor = new Visitor__c ();
newVisitor.Name = 'John Smith';
newVisitor.Identifier__c = 'john@somewhere.com';
insert newVisitor;
Only standard fields are not suffixed with __c
, e.g. Name
, Id
, CreatedDate
etc.
EDIT: boots and braces with Try/Catch
See the docs for a full description of the Insert operation:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_insert.htm
Doing a DML insert/update/upsert could result in an exception depending on a number of factors:
- It could be that the operation will mean that you exceed a Governor limit (see
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm).
If you are inserting multiple records, 'bulkification' will most
likely be required to prevent this.
- It could also (and more likely) be caused by an issue with the record
that you are trying to insert (e.g. missing required field, failed validation etc.).
Surrounding your operation with a Try/Catch is always good practice and will allow you to fail gracefully should the worst happen. Note, you cannot fail gracefully with a Try/Catch for a Governor limit issue.
There are essentially two ways to Try/Catch for DML statements (sorry, formatting has gone screwy):
This will suffice for a single record (though you could use the second option here too):
try
{
insert newVisitor;
}
catch( DmlException ex )
{
// fail gracefully
}
This may be preferable for multiple records, where the 'false' parameter indicates that this is (not) an 'all or nothing' insert:
Database.SaveResult[] MySaveResult = Database.Insert(newVisitors, false);
for (Database.SaveResult sr : MySaveResult) {
if (!sr.isSuccess()) {
// Operation failed, so get all errors
for(Database.Error err : sr.getErrors()) {
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Fields that affected this error: ' + err.getFields());
}
}
}
I'm guessing you've adapted this code from elsewhere, as the constructor for the Summary
class expects a parameter of type AggregateResult
:
public Summary(AggregateResult ar) {
ID = (Integer) ar.get('ID');
// etc.
Aggregate results are used when you use aggregate functions in SOQL, such as SUM()
. Your SOQL query doesn't contain any aggregate functions and so returns a list of Incident__c
records which you're storing in the list results
. Since you want to create summaries for each of these records you need to adapt your constructor accordingly (or even overload it, which means adding a second constructor that takes different parameters):
public class Summary {
public String ID{ get; private set; }
public String INCNAME{ get; private set; }
public String RepName{ get; private set; }
public String RepId{get;private set;}
public Summary(Incident__c i) {
ID = i.Id;
INCNAME= i.Name;
RepName = i.Rep_Lookup_id__r.Name;
RepId = i.Rep_Lookup_id__c;
}
}
Best Answer
Month is apex function and will not work in SOQL. You should use CALENDAR_MONTH
Use query like bellow
http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_soql_select_date_functions.htm
I guess your code will look like this: