• Algoworks
  • NEWBIE
  • 0 Points
  • Member since 2014
  • Algoworks


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 4
    Replies
Hi,

I am having a problem that should be really easy to solve.  It's easy to retrieve a user id in a trigger or class using soql.  But I am hitting a soql 101 limit error and thus need to have my code run some of one of my trigger when a specific user creates a record.  I can also do this by simply hardcoding the user's id in the trigger, but I don't want to do that.

Basically the algorithm is: trigger on object x updates a few fields on a record after it is interted, but only when user "a" creates the record.  The trigger should simply call a class that updates some fields on that newly inserted record by that specific user.  The trick is, in the trigger, specify the user who should be allowed to call that class without hardcoding the user's ID.

Basically what I need is a utiluty class I can call that returns the userId of the current user based on his/her alias or username.  Any ideas? 
  • February 25, 2014
  • Like
  • 0
Hi,
I had created a batchable class for inserting records in custom objects through a csv file.

now i want to create a test class for the same.
global class CampaignReminder
{

  WebService static void SendReminder(string id)
    {
     
      id cmpid = id;
      Integer batchSize = 1; 
      database.executebatch(new SendeMailinBatch(cmpid) , batchSize);
       
    }
}
========================================================
batch apex process
===================
global class SendeMailinBatch implements Database.Batchable<sObject>,Database.Stateful 

  Public id campaignid;
  global SendeMailinBatch(id campid) 
   { 
       campaignid = campid;
   }
  global Database.QueryLocator start(Database.BatchableContext BC) 
   { 
      //Batch on all campaign memeber who belongs to the requetsed campaign 
      String query = 'select id, emailtobesentid__c from campaignmember where campaignid = :campaignid'; 
      return Database.getQueryLocator(query); 
   }
  global void execute(Database.BatchableContext BC, List<sObject> scope) 
   { 
      //***  Select the campaign details
      List<campaignmember> cmx = new list<campaignmember>();
      campaign cmp = new campaign();
      cmp = [ select id, From_eMail__c, Reply_To__c, templatetobesent__c from campaign where id = :campaignid ];  
      for(Sobject s : scope) 
      { 
          //Type cast sObject in campaign object 
          campaignmember CM = (campaignmember)s ; 
          //Sending Mail
          Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
          mail.setSenderDisplayName(cmp.From_eMail__c);
          mail.setReplyTo(cmp.Reply_To__c);
          mail.setTargetObjectId(CM.emailtobesentid__c);
          mail.setTemplateId(cmp.templatetobesent__c);
          Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
      }
   }
   global void finish(Database.BatchableContext BC) 
   { 
     
   } 
}
======================================
TEST CLASS
@isTest(seealldata=true)
private class testCampaignReminder
{
 
 
  static testMethod void testMyWebSvc()
  {
      CampaignReminder cm = new CampaignReminder();
     // cm.id='123';
      //cm.Name='test1';
     // SendReminder sr=new SendReminder();
     // sr.Name='test1';
     //CampaignReminder.SendReminder( cm );
     
      Campaign[] a = [select id from Campaign where Name= 'confirmation2days'];
      System.assertEquals(a.size(), 1);
     
      //database.executebatch(new SendeMailinBatch(cmpid) , batchSize);

  }
}