You need to sign in to do that
Don't have an account?
CharlyG
Trigger sends SMS with HTTP - need help with test method
Hi, I have a couple of triggers that sends out sms's based on lead and contact data changes. We are sending these sms's via HTTP to a sms service provider. I get 100% test coverage on my triggers, but 0% on my class... Any help will be appreciated.
TRIGGER trigger SMSNewLead on Lead (after insert) { for(Lead l : Trigger.new){ //check that the status is new and the owner is not a queue, rating must be hot if(l.Status == 'New' && (''+l.OwnerId).startsWith('005') && l.Lead_Rating__c == 'Hot'){ SmsNewLead.basicSmsNewLead(l.Id, l.Name); } } }
CLASS public class SmsNewLead { //Future annotation to mark the method as async. @Future(callout=true) public static void basicSmsNewLead(String Id, String Name){ //get the lead details Lead l = [Select Id, Name, Initials__c, Salutation, LastName, Status, OwnerId, MobilePhone FROM Lead WHERE Id=:id ]; //Get the user details User u = [SELECT Id, LastName, FirstName, Name, Title, MobilePhone FROM User Where id = :l.ownerId]; String Clientsalutation = l.Salutation; String ClientInitials = l.Initials__c; String Clientlastname = l.LastName; String clientcellphonenumber = l.MobilePhone; string CurrentDate = string.valueof(System.now()); CurrentDate = CurrentDate.replace('-',''); CurrentDate = CurrentDate.replace(' ',''); CurrentDate = CurrentDate.replace(':',''); String Data = 'data=ZCF\nChanged for the purpose of this post|New Lead: Please contact .......|'+id+'1||'; Http http = new Http(); HttpRequest req = new HttpRequest(); req.setEndpoint('http://correcturl/'); req.setMethod('POST'); req.setBody(Data); HttpResponse res = http.send(req); //check the response if (res.getStatusCode() == 200) { //update lead l.SMS_To_Consultant__c = true; l.SMS_To_Consultant_Result__c = res.getBody(); update l; } else { //update lead l.SMS_To_Consultant__c = false; l.SMS_To_Consultant_Result__c = res.getBody(); update l; } } }
TEST METHOD Global Class TestSMSNotifications{ static testMethod void SMSLeadTest(){ //SMS to Consultant - New Lead Lead lt = new Lead(FirstName='Test1', LastName='Method', Status = 'New', MobilePhone='0833004553', Initials__c='T', Company='Test', phone='012345678', LeadSource='Client', Area__c='Free State', Gender__c='Female', Qualification_1__c='Q1', Qualification_2__c='Q2', Duration_1__c='D1', Institution_1__c='I1', Known_As__c='test' ); insert lt; Test.startTest(); SmsNewLead.basicSmsNewLead(lt.id, lt.Name); Test.stopTest(); } }
The @future method is not called because it occurs later. Instantiate a copy of the class and call it directly from the test code. Please note that you'll need a Test.isRunningTest() conditional branch since you don't want to actually send a SMS while testing. This will cost you a few points of code coverage, but you should still achieve over 90% in most cases.
All Answers
The @future method is not called because it occurs later. Instantiate a copy of the class and call it directly from the test code. Please note that you'll need a Test.isRunningTest() conditional branch since you don't want to actually send a SMS while testing. This will cost you a few points of code coverage, but you should still achieve over 90% in most cases.
Thank you so much for your reply - it worked! I managed to get 82% test coverage! For anyone interested here is the forst part of my test method
I have problem in class
( Error: Compile Error: Illegal assignment from Boolean to Decimal at line 36 column 16) ,
plz help in that and if i write 1 instade of true then anather problem
(Error: Compile Error: Illegal assignment from String to Decimal at line 37 column 16)
i think i making mistake while choosing datatype of fields.......or what ?
i have installed " SMS Magic " from appexchange ,so how to integrate that with your code .
OR
""WHAT SHOULD I DO FOR AUTOMATICALLY SENDING SMS WITHOUT ANY PAY "