• LoganMoore
  • NEWBIE
  • 25 Points
  • Member since 2010

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 11
    Replies

Here's an interesting test that I need to work out.

The method sendConfirmations in this class gets a list of deals older than two minutes and sends a confirmation email to the customer (I've got it hard coded to send to me while I'm testing it). The reason for this delay is that there's an external system that takes a little while to insert all of the information and related objects under this deal. This method is then scheduled to run every 5 minutes.

Because I only query deals older than 2 minutes, I can't create objects to test, as the new objects are not older than 2 minutes, so they aren't seen.

How can I get around this and test the method without relying on existing objects in the salesforce?

global class ConfirmationsScheduledSend implements Schedulable {
	
	global void execute(SchedulableContext sc) {
		sendConfirmations();
	}
	
	global static void sendConfirmations() {
		Id template = '00XD0000001WNxW';
		String debugEmail = 'logan.moore@velocitytrade.co.nz';
		Boolean debugEnabled = true;
		
		// We need to ensure that we've given felix enough time to upload any
		// outgoing payment information, so we only want to query deals older than 2 minutes
		Datetime twoMinutesAgo = Datetime.now().addMinutes(-2);
		List<Deal__c> deals = [SELECT Id, Felix_Trading_Account__r.OwnerId, Felix_Trading_Account__r.Contact__c FROM Deal__c WHERE Send_Confirmation_Flag__c = true AND CreatedDate < :twoMinutesAgo];
		
		// We're also going to need a list of all salesforce users, as we
		// will be blind carbon copying the owner in on all of their auto confirmations.
		Map<Id,User> users = new Map<ID,User>([Select Id, Name, Email FROM User]);
		
		// Set up the email queue and reserve email capacity
		List<Messaging.SingleEmailMessage> emailQueue = new List<Messaging.SingleEmailMessage>();
		Messaging.reserveSingleEmailCapacity( users.size() );
		
		for (Deal__c deal : deals) {
			// Figure out the deal owner
			User owner = users.get(deal.Felix_Trading_Account__r.OwnerId);
			
			// Compiling a list of BCCs for this email
			List<String> bccList = new List<String>();
			//bccList.add(String.valueOf(owner.Email)); // bcc the owner in all cases
			if (debugEnabled)
				bccList.add('logan.moore@velocitytrade.com');
			
			// Build up the email message object
			Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
			email.setTemplateID(template);
			email.setWhatId(deal.Id);
			//email.setTargetObjectId(deal.Felix_Trading_Account__r.Contact__c);
			email.setSaveAsActivity(false);
			email.setTargetObjectId('00520000001ZoRC');
			email.setBccAddresses(bccList);
			emailQueue.add(email);
			
			// now that the email is queued, we remove the flag and set the last send datetime.
			deal.Send_Confirmation_Flag__c = false;
			deal.Last_Confirmation_Sent__c = Datetime.now();
		}
		
		// Try sending the email in a try-catch block. If something goes wrong, I NEED to know about it ASAP.
		try {
			if (emailQueue.size() > 0) {
				Messaging.sendEmail(emailQueue);
			}
			update deals;
		}
		catch (EmailException ex) {
			Messaging.SingleEmailMessage errorEmail = new Messaging.SingleEmailMessage();
			errorEmail.setToAddresses(new List<String>{ debugEmail });
			errorEmail.setSubject('ALERT: Confirmations Email Failed');
			errorEmail.setPlainTextBody('HIGH PRIORITY ALERT:\n\nThere was an error trying to send automatic confirmations. The error reads as follows:\n\n' + ex);
			Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{ errorEmail });
		}
	}
	
}

 



 

Hi Everyone,

 

I've run into an 'out of the box' problem that I could do with someone advice solving.

 

 

The situation

There's a third party system that inserts new order records (custom object) into salesforce through the API. The third party system may or may not insert one or more payment instruction records (a master-detail object of order) 90 seconds after the order is inserted. I've been asked to make a PDF order confirmation that gets emailed to the customer automatically when one of these orders is received.

 

The problem

The confirmation email needs to include payment instructions that we've received, but orders don't require payment instructions. Sometimes we get orders with no instructions initially. We might get those instructions much later on, but we still need to send a confirmation. This has two implications:

 

  1. I can't trigger this email from the order object because we haven't got the payment instructions by then so all confirmations will appear as if we haven't received the instructions.
  2. I can't trigger this email from the payment instruction object because if an order doesn't have payment instructions, we won't send a confirmation at all.

A failed solution

I thought I'd be smart and set up a scheduled apex task that runs every 5 minutes, and collects all orders, older than three minutes that haven't had a confirmation sent (using a checkbox to indicate whether the confirmation has been sent or not). This sounded like a decent hack on paper, but then I found out you can't send emails from scheduled apex. This fact alone has thrown my only remaining option on the fire.

 

 

Are there any ideas out there on how I could solve this problem?

Hi,

 

I am trying to copy some set of data into an custom object. I am hitting the governor limits. Can anyone suggest better means of not exceeding governor limits.

 

Changing the for loops to use SOQL query for loops might help, but i am not sure how to go about doing it. Any pointers would be of great help.

 


public with sharing class move_records_to_reports { List<Reporting__c> Rep = new List<Reporting__c>(); List<Timecard_base__c> TB = new List<Timecard_base__c>(); List<TimeCard_Line_Items__c> TL = new List<TimeCard_Line_Items__c>(); public List<Assigned_Consultant__c> AC= new List<Assigned_Consultant__c>(); public void move_records_to_reports() { } public void transfer() { TL=[select Id,name,Timecard__r.consultant__r.Employment_Notes__c,assigned_consultant__c,Timecard__r.WeekStartdate__c,project__c,consultantId__c, Monday_Hours__c,Tuesday_Hours__c,Wednesday_Hours__c,Thursday_Hours__c,Friday_Hours__c,Saturday_Hours__c,Sunday_Hours__c from TimeCard_Line_Items__c]; for(integer j=0;j<TL.size();j++) { if(TL[j].Assigned_Consultant__c==null) { AC=[select id from Assigned_Consultant__c where Project__c =: TL[j].Project__c and Contact__c=:TL[j].consultantId__c and (Start_Date__c <=: (TL[j].Timecard__r.WeekStartdate__c.addDays(6)) or Start_Date__c = null) and (End_Date__c >=: TL[j].Timecard__r.WeekStartdate__c or End_Date__c = null) limit 1]; if (ac.size()>0) { TL[j].Assigned_Consultant__c=AC[0].id; } } upsert TL; } for(integer i=0;i<TL.size();i++) /*for(List<TimeCard_Line_Items__c> TL :[select Id,name,Timecard__c,Timecard__r.WeekStartdate__c, Monday_Hours__c,Tuesday_Hours__c,Wednesday_Hours__c,Thursday_Hours__c,Friday_Hours__c,Saturday_Hours__c,Sunday_Hours__c from TimeCard_Line_Items__c]) */{ //update assigned consultant field in timecard line items Reporting__c r = new Reporting__c(); r.TimeCard_Line_Item__c=TL[i].Id; r.name='From Moved Class'; r.Employment_Notes__c=TL[i].Timecard__r.consultant__r.Employment_Notes__c; r.timelineitemDate__c=TL[i].Timecard__r.WeekStartdate__c; r.hours__c=TL[i].Monday_Hours__c; Rep.add(r); Reporting__c r1 = new Reporting__c(); r1.TimeCard_Line_Item__c=TL[i].Id; r1.name='From Moved Class'; r1.Employment_Notes__c=TL[i].Timecard__r.consultant__r.Employment_Notes__c; r1.timelineitemDate__c=TL[i].Timecard__r.WeekStartdate__c.AddDays(1); r1.hours__c=TL[i].Tuesday_Hours__c; Rep.add(r1); Reporting__c r2 = new Reporting__c(); r2.TimeCard_Line_Item__c=TL[i].Id; r2.name='From Moved Class'; r2.Employment_Notes__c=TL[i].Timecard__r.consultant__r.Employment_Notes__c; r2.timelineitemDate__c=TL[i].Timecard__r.WeekStartdate__c.AddDays(2); r2.hours__c=TL[i].Wednesday_Hours__c; Rep.add(r2); Reporting__c r3 = new Reporting__c(); r3.TimeCard_Line_Item__c=TL[i].Id; r3.name='From Moved Class'; r3.Employment_Notes__c=TL[i].Timecard__r.consultant__r.Employment_Notes__c; r3.timelineitemDate__c=TL[i].Timecard__r.WeekStartdate__c.AddDays(3); r3.hours__c=TL[i].Thursday_Hours__c; Rep.add(r3); Reporting__c r4 = new Reporting__c(); r4.TimeCard_Line_Item__c=TL[i].Id; r4.name='From Moved Class'; r4.Employment_Notes__c=TL[i].Timecard__r.consultant__r.Employment_Notes__c; r4.timelineitemDate__c=TL[i].Timecard__r.WeekStartdate__c.AddDays(4); r4.hours__c=TL[i].Friday_Hours__c; Rep.add(r4); Reporting__c r5 = new Reporting__c(); r5.TimeCard_Line_Item__c=TL[i].Id; r5.name='From Moved Class'; r5.Employment_Notes__c=TL[i].Timecard__r.consultant__r.Employment_Notes__c; r5.timelineitemDate__c=TL[i].Timecard__r.WeekStartdate__c.AddDays(5); r5.hours__c=TL[i].Saturday_Hours__c; Rep.add(r5); Reporting__c r6 = new Reporting__c(); r6.TimeCard_Line_Item__c=TL[i].Id; r6.name='From Moved Class'; r6.Employment_Notes__c=TL[i].Timecard__r.consultant__r.Employment_Notes__c; r6.timelineitemDate__c=TL[i].Timecard__r.WeekStartdate__c.AddDays(6); r6.hours__c=TL[i].Sunday_Hours__c; Rep.add(r6); } try{ upsert Rep; // upsert TL; System.debug('completed insertion'); } catch (Exception e) { ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. '); ApexPages.addMessage(errormsg); } } }

 

  • October 10, 2011
  • Like
  • 0

Hi,

 

Does anyone know if there's a new soql query record limit in winter 12? We're seeing a 1000 query record limit in winter that wasn't there in summer 11

Hi Everyone,

 

I've run into an 'out of the box' problem that I could do with someone advice solving.

 

 

The situation

There's a third party system that inserts new order records (custom object) into salesforce through the API. The third party system may or may not insert one or more payment instruction records (a master-detail object of order) 90 seconds after the order is inserted. I've been asked to make a PDF order confirmation that gets emailed to the customer automatically when one of these orders is received.

 

The problem

The confirmation email needs to include payment instructions that we've received, but orders don't require payment instructions. Sometimes we get orders with no instructions initially. We might get those instructions much later on, but we still need to send a confirmation. This has two implications:

 

  1. I can't trigger this email from the order object because we haven't got the payment instructions by then so all confirmations will appear as if we haven't received the instructions.
  2. I can't trigger this email from the payment instruction object because if an order doesn't have payment instructions, we won't send a confirmation at all.

A failed solution

I thought I'd be smart and set up a scheduled apex task that runs every 5 minutes, and collects all orders, older than three minutes that haven't had a confirmation sent (using a checkbox to indicate whether the confirmation has been sent or not). This sounded like a decent hack on paper, but then I found out you can't send emails from scheduled apex. This fact alone has thrown my only remaining option on the fire.

 

 

Are there any ideas out there on how I could solve this problem?

Hi,

 

This seems quite complex to me, but I am sure one of you experts out there will see this as easy!!

 

I have a number of custom objects:

 

  1. Service_Agreement__c
  2. Training__c
  3. Employee_Training_Action__c (Joining Object - joining Employee__c and Training__c)
  4. Employees__c
  5. Operator_Skill_Level__c
  6. OSLSA_Join__c (Joining Object - joining Service_Agreements__c and Operator_Skill_Level__c)
  7. EmployeeOSL_A__c (Joining Object - joining Employees__c and Operator_Skill_Level__c)

Effectivly where I am at the moment is that when a value is checked on Service_Agreements__c a new Training__c record is created linked to the SA. Also, The Service_Agreement__c is linked to one or many Operator_Skill_Level__c records through the OSLSA_Join__C object.

 

Each Employee__c record is also linked to one or many Operator_Skill_Level__c records through the EmployeeOSL_A__c object.

 

What I need, is when the Training__c record is created, the relevent Operator Skill Level(s) are retrieved from the related SA and then all of the employees__c that belong to the OSL(s) are related back to the Training__c record.

 

I think I have covered everything off their, Im guessing this is just a complex SOQL query . but any help would be really apprecaited.

 

Thank you in advance.