This is my trigger, for sending SMS when account owner is changed

trigger sms on Account (before insert,before update) {

    for(Account acc:trigger.new)
        if(acc.Status__c=='Qualified' && acc.ownerid != trigger.oldmap.get(acc.id).ownerid)
        //if I include "&& acc.ownerid != trigger.oldmap.get(acc.id).ownerid" i'm not getting test coverage
            user u=[SELECT Alias,Id,MobilePhone,Name,Phone FROM User where Id=: acc.OwnerId];

This is my test class for the above trigger, if i perform any DML operation(insert and update) my method is not passing. I mean I will get 100% test coverage if i don't include && acc.ownerid != trigger.oldmap.get(acc.id).ownerid in the above trigger.

public class sms1 {

    static testmethod void testsms() {
        Account acc= new Account();

        Account acc1= new Account();

This is my apex class to send SMS from 3rd party application

public class sendsms {
    public static void sendsms1(string mobile,string name) {

   // list<account> acc=[SELECT Id,Phone FROM Account where id=:conId];

       String Username ='[email protected]';
       String Password = 'xxxxxx';
       String TempID='11111' ;
       String MobileNo=mobile;
       String F1=name;
       String SenderID = 'XXXXX';
       String F2= '8-09-2013';
       String F3= '07-09-2013';
       String postData = 'username=' +Username + '&pass=' + Password + '&dest_mobileno=' + MobileNo +'&senderid=' + SenderID + '&tempid=' + TempID+'&F1=' + F1+ '&F2=' + F2+ '&F3=' + F3;

       HttpRequest req = new HttpRequest();
       Http http = new Http();
       HttpResponse res = http.send(req);

Can anyone save me from this problem. Thank You in Advance.

  • oldMap used in trigger would not work on insert trigger
    – Sonal
    Jan 27, 2014 at 12:36

1 Answer 1


Well, I see many things wrong here

  1. Trigger.oldmap is not available on Before Insert: trigger.oldmap.get(acc.id)
  2. Account.Id is not available on before Insert: trigger.oldmap.get(acc.id)
  3. Avoid SOQL within a loop, that is not Bulk-Safe!: user u=[SELECT Alias,Id,MobilePhone,Name,Phone FROM User where Id=: acc.OwnerId];
  4. You must insert or update the created account in the Test to fire the trigger event.
  5. You also need a HTTPCalloutMock in order to test the sendsms1 method
  6. Probably you should add the Test.start/stop to cover the future method.
  7. You have to create the user in the Test method, avoid this: acc1.ownerid='005O0000001VQSD';

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .