function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sure@DreamSure@Dream 

Test method to disable external community user

Hi All,

I am having a object-Registrant .
On inserting a registrant record with Status__c='Approved', I am creating a contact and an authenticated user with the details provided.
I am using a future method to create a user.

If the registrant status__c is made as 'Deactivated', I need to make the corresponding user as inactive.
I am using a future method to deactivate the corresponding user.

How can I replicate deactivating, in test methods??

Thanks

Vinita_SFDCVinita_SFDC
Hi,

Create test data in your test class. Check for the condition and deactivate user using isActive=false.

Refer test class in following link for sample code:

http://knthornt.wordpress.com/2011/06/08/trigger-to-deactivate-users/

Sure@DreamSure@Dream
Hi Vinita,

Thanks for the reply.

But what I need is different.
In Registrant__c object I am having a field Status__c.
If a registrant record is inserted/updated with 'Approved' value.
I am calling a method to insert a contact. And from that method I am calling a future method to create a user.

If a registrant is updated with status as 'Deactivated', then I am calling a future method to deactivate the corresponding user.
//trigegr to create users, if registrant status is 'Approved'
trigger insertTrigger on Registrant__c(after insert)
{
      List<Registrant__c> listOfRegistrants=new List<Registrant__c>();
      for(Registrant__c reg:Trigger.new)
      {
        //checking if status is 'Approved' and adding to listOfRegistrants
     }
     HandlerClass.createContact(listOfRegistrants);
}

//trigger to create/deactivate users based on the registrant record's status
trigger updateTrigger on Registrant__c(after insert)
{
         List<Registrant__c> listOfActivateRegistrants=new List<Registrant__c>();
          List<Registrant__c> listOfDeactivateRegistrants=new List<Registrant__c>();
          for(Registrant__c reg:Trigger.new)
          {
            //checking if status has become 'Approved' and adding tolistOfActivateRegistrants
           //checking if status has become 'Deactivated' and adding to listOfDeactivateRegistrants
         }
        HandlerClass.createContact(listOfActivateRegistrants);
       Set<String> setOfUserNames;
         ---prepared the SetOfUserNames by iterating and adding email ids of registrants from listOfDeactivateRegistrants
         HandlerClass.deActivateUsers(setOfUserNames);
}
//handler class
public class HandlerClass
{
     public static void  createContact(List<Registrant__c> listOfRegs)
     {
         //inserting the contacts
         CreateUsers(requiredParameters);
     }
    //Method to create users asynchronously
      @future
       public static void  CreateUsers(requriedParameters)
      {
           //creating users
     }
      //Method to deactivate users, asynchrounously
         @future
          public staitic void deActivateUsers(Set<string> setOfUsernames)
           {
                //deactivating the users by making isActive as false
           }

}

//Test class for checking Deactivation of user
public class sampeTestClass()
{
      public static void insertTestdata()
      {
              registrant__c newreg=new registrant__c(FirstName__c='Sam',LastName__c='Ram', Email='Sample@test.com',  Status__c='Approved' (mailto:Email='Sample@test.com',  Status__c='Approved'));
             insert newReg;
    }
    public static testMethod void deactivateUser()
    {
          Test.startTest();
         
           insertTestdata();// -- to create a registrant and a user
           newReg=[SELECT id,Status__c FROM Registrant__c  WHERE EmaiL__c=='Sample@test.com'];
           newReg.status__c='Deactivated'
           update newReg;
            Test.stopTest();
            User testUsr=[SELECT id,isActive FROM User  WHERE  Email='Sample@test.com'];
           System.assertEquals(false,testUsr.isActive);
    }
}

--Assert statement in the above test class is failing
Vinita_SFDCVinita_SFDC
Hi,

The method which is deactivating is a future method. You will have to explicitly call this method between test.startTest() and test.stopTest() method. Include debug statements in your code to check if the method for deactivating the user is being called or not.

Refer: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_invoking_future_methods.htm