+ Start a Discussion
tnstns 

Need help creating an Apex Test Class

Am new to Apex and just  starting to learn it, so I would appreciate any help here.

 

I was informed that I should include a few Contact creations (with Ing user id field populated), and deletions of Contacts that are created by this class

 

Can anyone help me write a Test Class for this simple Apex trigger? The trigger restricts all profiles (except the Sys Admin one), from deleting Contacts IF the record contains an integer > 0 in the Ing User ID custom field.

 

trigger CheckFieldValues on Contact (before delete) 
{
        if(UserInfo.getProfileId()=='00e50000000sCkdAAE')
        {
            System.debug('User Profile Id--'+UserInfo.getProfileId());
        }
         else
         {
             for (Contact c : Trigger.old)
             {
 
                if (c.ING_USER_ID__c > 0 )
                {
                    c.addError('**This Contact record contains a unique Ingenuity User ID and cannot be deleted. Deletion would have a detrimental impact on data integration processes**');
                }
             }
            
         }
        
}
Best Answer chosen by Admin (Salesforce Developers) 
osamanosaman

ere you go

 

 

@isTest
public class CheckFieldValues
{
    static testMethod void delContact()
    {
        Contact cn = new Contact();
        cn.Account = 'Ingenuity Sys';
        cn.LastName = 'test1_ Apex_Trigger ';
        cn.Phone = '123123121';
        cn.ING_USER_ID__c = 5555555;
        cn.LeadSource = Other;
        cn.Do_you_have_data_to_analyze_today__c= Yes;
       
        insert cn; 
        User sysAdmin = [Select id from Profile where Name = 'Sys Admin for API User Account' limit 1];
        System.runAs(sysAdmin)
       
        {
        delete cn;
        }
        Contact cn2 = new Contact();
        cn.Account = 'Ingenuity Sys';      
        cn2.LastName = 'test2_Apex_Trigger';
        cn2.Phone = '123123122';
        cn2.ING_USER_ID__c =  5555556;
        cn.LeadSource = Other;
        cn.Do_you_have_data_to_analyze_today__c= Yes;
        insert cn2; 
        User NonsysAdmin = [Select id from Profile where Name = 'Power User - Sales Management' limit 1];
        System.runAs(NonsysAdmin)
        {
            delete cn2;
        }
    
        }
}

 

All Answers

osamanosaman

@IsTest

public class trigger_ContactBeforeDelete()

{

static testMethod void delContact()

{

Contact cn = new Contact()

cn.LastName = 'test';

cn.Phone = '12312312';

cn.ING_USER_ID__c = 5;

 

insert cn;

 

User sysAdmin = [Select id from Profile where Name != 'System Administrator' limit 1];

System.runA(sysAdmin)

{

delete cn;

}

 

  Contact cn2 = new Contact()

  cn2.LastName = 'test';

  cn2.Phone = '12312312';

  cn2.ING_USER_ID__c = 5;

 

  insert cn2;

 

  User NonsysAdmin = [Select id from Profile where Name = 'System Administrator' limit 1];

  System.runA(NonsysAdmin )

  {

  delete cn2;

  }

}

}

tnstns

This is awesome. Thank you Osama!

 

A couple of things. After I substituted certain values to match our org and test situation, I tried to save the class but it choked on the left curly bracket before the delete cn; line (Error: Compile Error: unexpected token: '{' at line 16 column 8).

 

If I remove the curly bracket, then it complains about delete cn; (Error: Compile Error: unexpected token: 'delete' at line 17 column 8)

 

Do you see anything that needs to be corrected in the test you wrote, or better, could you look at the modified one I have below?

 

@IsTest

public class CheckFieldValues

{

    static testMethod void delContact()

    {

        Contact cn = new Contact();

        cn.Account = ‘Ingenuity Sys’;

        cn.LastName = 'test1_ Apex_Trigger ';

        cn.Phone = '123123121';

        cn.ING_USER_ID__c = 5555555;

        cn.LeadSource = Other;

        cn.Do_you_have_data_to_analyze_today__c= Yes;

        insert cn;

        User sysAdmin = [Select id from Profile where Name = 'Sys Admin for API User Account' limit 1];

        System.runA(sysAdmin)

        {

        delete cn;

        }

        Contact cn2 = new Contact()

        cn.Account = ‘Ingenuity Sys’;      

        cn2.LastName = 'test2_Apex_Trigger';

        cn2.Phone = '123123122';

        cn2.ING_USER_ID__c =  5555556;

        cn.LeadSource = Other;

        cn.Do_you_have_data_to_analyze_today__c= Yes;

        insert cn2;

        User NonsysAdmin = [Select id from Profile where Name = ' Power User - Sales Management' limit 1];

        System.runA(NonsysAdmin )

        {

            delete cn2;

        }

    }

}

 

osamanosaman

Change System.runA(NonsysAdmin ) to

 

System.runAs(NonsysAdmin )

tnstns

Thanks. I made that change and then it asked for left curly brackets instead of ; after System.runAs(sysAdmin), which I modified.

 

Now it complains that "Error: Compile Error: expecting right curly bracket, found 'EOF' at line 0 column -1"


Below is the current test. Please advise how I can addres this error.

 

@IsTest

public class CheckFieldValues

{

    static testMethod void delContact()

    {

        Contact cn = new Contact();

        cn.Account = 'Ingenuity Sys';

        cn.LastName = 'test1_ Apex_Trigger ';

        cn.Phone = '123123121';

        cn.ING_USER_ID__c = 5555555;

        cn.LeadSource = Other;

        cn.Do_you_have_data_to_analyze_today__c= Yes;

       

        insert cn; {

        User sysAdmin = [Select id from Profile where Name = 'Sys Admin for API User Account' limit 1];

        System.runAs(sysAdmin){

       

        {

        delete cn;

        }

        Contact cn2 = new Contact();

        cn.Account = 'Ingenuity Sys';      

        cn2.LastName = 'test2_Apex_Trigger';

        cn2.Phone = '123123122';

        cn2.ING_USER_ID__c =  5555556;

        cn.LeadSource = Other;

        cn.Do_you_have_data_to_analyze_today__c= Yes;

        insert cn2; {

        User NonsysAdmin = [Select id from Profile where Name = 'Power User - Sales Management' limit 1];

        System.runAs(NonsysAdmin){

        {

            delete cn2;

        }

    }

}

osamanosaman

ere you go

 

 

@isTest
public class CheckFieldValues
{
    static testMethod void delContact()
    {
        Contact cn = new Contact();
        cn.Account = 'Ingenuity Sys';
        cn.LastName = 'test1_ Apex_Trigger ';
        cn.Phone = '123123121';
        cn.ING_USER_ID__c = 5555555;
        cn.LeadSource = Other;
        cn.Do_you_have_data_to_analyze_today__c= Yes;
       
        insert cn; 
        User sysAdmin = [Select id from Profile where Name = 'Sys Admin for API User Account' limit 1];
        System.runAs(sysAdmin)
       
        {
        delete cn;
        }
        Contact cn2 = new Contact();
        cn.Account = 'Ingenuity Sys';      
        cn2.LastName = 'test2_Apex_Trigger';
        cn2.Phone = '123123122';
        cn2.ING_USER_ID__c =  5555556;
        cn.LeadSource = Other;
        cn.Do_you_have_data_to_analyze_today__c= Yes;
        insert cn2; 
        User NonsysAdmin = [Select id from Profile where Name = 'Power User - Sales Management' limit 1];
        System.runAs(NonsysAdmin)
        {
            delete cn2;
        }
    
        }
}

 

This was selected as the best answer
tnstns

Thanks Osama. Am good now. Got some help from SFDC too.