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
DoondiDoondi 

custom Settings: To Avoid hard coding Id : Trigger : Test Class

Hi below is my Simple Trigger and Test classes.
I am not able to Cover the code for Custom settings, can anyone help me
 
trigger CreateRecord on Deals__c (after insert, after update) 
{
    
    set<id> triggerIds = trigger.newMap.keyset();
    List <Project__c> Project = [select id, Name from Project__c where id in :triggerIds];
    List <Project__c> newProject = new List<Project__c>() ;
    
    for(Deals__c Deal : [Select id,Name,Property__r.Region__c,Type__c,Status__c,Manager__c,Property__r.Site__c from LMS_Deal__c where id IN :triggerIds])
    {
        if(Deal.Type__c == 'New')
        {
            string Name = Deal.Name + ' - ' + Deal.Property__r.Site__c;
            List <Project__c> ProjectList = [select id,Name,OwnerId,Property__r.Site__c from Project__c where Name =: Name];
            Region__c customSettings = Region__c.getInstance(Userinfo.getUserId());
			if (ProjectList.size() == 0)
            {
                if(deal.Manager__c == Null && Deal.Property__r.Region__c == 'Delhi')
                {
                    Deal.Manager__c = customSettings.Delhi__c;
    	        }     
                    newProject.add(new Project__c
                               (   Name = Deal.Name + ' - ' + Deal.Property__r.Site__c, 
                                   OwnerId = Deal.Manager__c
                    insert newProject;
            }//if
            else if (ProjectList.size() >> 0)
            {
                if(Deal.Manager__c == Null && Deal.Property__r.Region__c == 'Delhi')
				{
                    Deal.Manager__c = customSettings.Delhi__c;
                }
               Project__c NewProjectList = [select id,Name,OwnerId,Property__r.Site__c,Property__r.Region__c from Project__c where Name =: Name];
                NewProjectList.OwnerId = Deal.Manager__c; 
                update NewProjectList;
            }    
        }
    }
}

My test class:
 
@isTest(SeeAllData=true)
private class CreateRecordTest {
    static testmethod void CreateRecord(){
		// creating property
        Property__c prop = new Property__c(Name = 'Test property', Site_c = 'New Site', Region__c = 'Delhi');
        insert prop;
		// creating Units 
        Unit__c units = new Unit__c(Name = 'Test Unit', Property_Unit__c = prop.Id);
        insert units;
		// creating a user 
        Profile objProfile = [SELECT Id, Name FROM Profile WHERE Name='System Administrator'];
        User objUser = new User(Alias = 'Swill', 
                                Email='swill@mymail.com', 
                                EmailEncodingKey='UTF-8', 
                                LastName='Will', 
                                LanguageLocaleKey='en_US', 
                                LocaleSidKey='en_US', 
                                ProfileId = objProfile.Id, 
                                TimeZoneSidKey='America/Los_Angeles', 
                                UserName='will@gmail.com');
        insert objUser;
		// creating another user for assigning custom setting 
        Profile objProfile1 = [SELECT Id, Name FROM Profile WHERE Name='System Administrator'];
        User objUser1 = new User(Alias = 'Kaka', 
                                Email='kaka@kakamail.com', 
                                EmailEncodingKey='UTF-8', 
                                LastName='Mkaka', 
                                LanguageLocaleKey='en_US', 
                                LocaleSidKey='en_US', 
                                ProfileId = objProfile1.Id, 
                                TimeZoneSidKey='America/Los_Angeles', 
                                UserName='kaka_M@gmail.com');
        insert objUser1;
		// creating custom settings 
		Region__c CustomSettings = new Region__c ( Name = 'test custom setting',
													Delhi__c = objUser1.Id);
        insert CustomSettings;
       // creating deal  
       Deal__c deal = new Deal__c(Name = 'Test Deal',
                                  Manager__c = objUser1.Id,
                                  Type__c = 'New');
                          insert LMSdeal1;
       // creating property 
        Project__c TP = new Project__c(
                Name = deal.Name +  ' - '  + deal.Property__r.Site__c,
                OwnerId = deal.Manager__c);
            insert TP;
        
        
            
}
}

The problem -  not able to Pass the values into if conditions 
if(deal.Manager__c == Null && Deal.Property__r.Region__c == 'Delhi')
                {
                    Deal.Manager__c = customSettings.Delhi__c;
    	        }

 
Daniel Probert 10Daniel Probert 10
what error are you getting when you run the test class?

also with (SeeAllData=true) do you even need to create the custom settings in the test class?
devedeve
Hi Doondi,

You have not set Deal.Property__c field value when inserting Deal record.
Change the sequence of insertion. First Property then Deal like:


// creating property
Project__c TP = new Project__c( Name = deal.Name + ' - ' + deal.Property__r.Site__c,
                                                     OwnerId = deal.Manager__c);
insert TP;
// creating deal
Deal__c deal = new Deal__c(Name = 'Test Deal',
                                               Manager__c = objUser1.Id,
                                               Type__c = 'New',
                                                Property__c = TP );
insert LMSdeal1;
 
DoondiDoondi
@deve;
I am not able to insert deal 
it's throwing this error : CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY
DoondiDoondi
@Daniel Probert 10
I am not getting any errors, but code coverage lines were not creasing. 
DoondiDoondi
Hi, any suggestions, please?