I'm writing a trigger to create a new Event on update of Opportunity. There are some required fields and others are optional in the form that the user submits. I'm trying to determine a good way to filter for fields that don't contain any data when creating the new events. The trigger operates on a check box custom Opportunity object field that gets set when the opportunity is submitted/updated which tells the trigger to create the event.
If possible, I'd prefer not to have a series of nested if statements to test each field for data. Only about 40% of the possible fields are required or locked into the form the user submits.
trigger ScheduleAmbassadorEvent on Opportunity (after update) {
/* Get Opp IDs being processed by trigger where box is checked for opps After Update */
Integer s = [SELECT Count() FROM Opportunity WHERE Id IN :Trigger.new AND Opportunity.Schedule_Ambassador__c = true];
If (s != 0) {
list <opportunity> NwEvnt = [SELECT Id, Name, Description, CloseDate, LeadSource, CreatedDate, Account_Address__c, Recap_Type__c, Event_Start_Time__c, Event_Info__c, Schedule_Ambassador__c, Planned_Mintues__c, ECAL_c__c, Campaign.Name, Owner.Name, Program_PO__r.Name, SWS_Recomended_CE__r.Name, SWS_Recomended_CE__r.Phone, SWS_Recomended_CE__r.Email, Customer_Contact__r.Name, Customer_Contact__r.Phone, Customer_Contact__r.Email FROM Opportunity WHERE Id IN :Trigger.new AND Schedule_Ambassador__c = true];
From here, I can either create a huge multitude of maps, or I can begin building the list of Events using the required information, then later add the optional information. Once I'm done, I'll be resetting the Schedule_Ambassor__c field to false so it won't trip the trigger again unless a another opportunity on the acct requires a new event to be created.
I've seen the following code which I'll admit I don't fully understand. Consequently I've wondered how I could apply it to what I have above.
String queryString = 'SELECT Id, Name, ' +
'(SELECT FirstName, LastName FROM Contacts LIMIT 1) FROM Account';
SObject[] queryParentObject = Database.query(queryString);
for (SObject parentRecord : queryParentObject){
Object ParentFieldValue = parentRecord.get('Name');
// Prevent a null relationship from being accessed
SObject[] childRecordsFromParent = parentRecord.getSObjects('Contacts');
if (childRecordsFromParent != null) {
for (SObject childRecord : childRecordsFromParent){
Object ChildFieldValue1 = childRecord.get('FirstName');
Object ChildFieldValue2 = childRecord.get('LastName');
System.debug('Account Name: ' + ParentFieldValue +
'. Contact Name: '+ ChildFieldValue1 + ' ' + ChildFieldValue2);
}
}
}
I'm not accessing a record from a parent, thus part of why it confuses me. I am however accessing related lists. Regardless, it seems that the same concept should work to determine whether or not a record is null. If it's not then add it to the list. Once an Event record was populated, I presume I could then insert it and later do a query to run a system assert for validation that each one was properly inserted into the database; something I've never had to do before in a trigger. I've seen code that would look like this:
Event[] event = new Event[]{};
for (list<Opportunity> e : NwEvnt) {
Event event = new Event(Owner = 'e.Owner.Name' + ',' + 'e.ECAL_c__c'), Start = e.Event_Start_Time__c, ActivityDate = e.CloseDate, Description = e.Description, DurationInMinutes = e.Planned_Minutes__c, Location = e.Event_Info__c, Who = e.Customer_Contact__c, Phone = e.Customer_Contact__r.Phone, Start = e.Event_Start_Time__c, IsVisibleInSelfService = true, CreatedBy = e.Owner.Name, Subject='EventAssignment');
/* my thought was to add the required fields first, and then use If statements to add the optional fields to each new event once I determined whether or not it was null if I couldn't come up with some kind of SObject describe method to determine whether or not a field was null. If I could, it would be inserted here before continuing. */
Event.add(event);
}
try {
insert Event;
} catch (DmlException e) {
System.debug(e.getMessage());
}
Am I on the right track with this final part? Any suggestions for improvement would be appreciated. Thanks!
