+ Start a Discussion
yarramyarram 

Urgent: How to write a Test class for My below Apex Trigger

Hi,

 

How to write a trigger for below Trigger.

 

trigger creatingPathRecordsusingcontacts on Contact (after insert, after update) {
        for (Contact cont: Trigger.new){ 
            if(cont.Due_Date__c!=null){       
                
                Contact NewContact=[Select id,name,Account.id,Account.name from Contact where id=:cont.id];
                
                Path__c newPath=new Path__c();
                newPath.Name=NewContact.Account.name;
                newPath.AccountName__c=NewContact.Account.id;
                newPath.Path_Type__c='Evaluation';
                newPath.Product__c='REDACTED';                 
                insert newPath;                
                Path__c  PathExpDate=[Select id,name,CreatedDate from Path__c  where id=:newPath.id];
                    Datetime expdate=PathExpDate.CreatedDate;
                    expdate=expdate.addDays(14);
                    newPath.Expiration_Date__c=expdate;
                    update newPath;
           }
    
        }
}

 please help me out this.

 

Thanks,

Yarram.

hitesh90hitesh90

Hi ram,

 

You have to insert contact record using apex in your test class with due date field value.

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

Subhash GarhwalSubhash Garhwal

Hi,

 

You only need to insert contact record for test class

 

Like :- 

 

//If Due_Date__c is Date field than Use System.today() Or if Due_Date__c is DateTime field than use System.now()

Contact con = new Contact(LastName = 'Test', Due_Date__c = System.today());

 

Insert con;

 

it will conver your trigger 

 

For More Help you can go through this link

 

http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html

 

Thanks

 

Hit the Kudos button (star)  and Mark as solution if it post helps you

yarramyarram

 

Hi,

 

as you mentioned i written test class, But its failed and it gives error message like......

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, LicenseTrigger: execution of AfterInsert

caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [AccountName__c]: [AccountName__c]

Trigger.PathTrigger: line 11, column 1: []

 

 

@isTest
private class PathTriggerTestSuite {
    static testMethod void ContactTest() {
        
        Contact contact= new Contact();
        contact.Due_Date__c=System.now();
        contact.FirstName='Test';
        contact.LastName='Contact';
        insert contact;   
        System.assertEquals('Test',contact.FirstName);
        
       
    }


}

 please help me out.

thanks,

Yarram.

Subhash GarhwalSubhash Garhwal

Hi,

 

For the second error you neeed to put value in accountName__c field. If this field is lookup on account than you need to insert a account record than assign it to accountName__c field 

Like

 

//acc is account record that you insert

Contact.AccountName__c = acc.Id

 

Tahnks

 

Hit the Kudos button (star)  and Mark as solution if it post helps you 

yarramyarram

Hi,

 

here i am getting error ..... while passing acc.id to contact object....

 

contact.Account=account.id; // Compile Error: Illegal assignment from Id to SOBJECT:Account 

insert contact;

 

 

@isTest
private class PathTriggerTestSuite {
    static testMethod void ContactTest() {
        Account Acc=new Account(Name='Alcero');
        insert Acc;
        Account account= [Select id,name from Account where name='Alcero'];
        
        Contact contact= new Contact();
        contact.Due_Date__c=System.now();
        contact.FirstName='Test';
        contact.LastName='LicenseTrigger';
        contact.Account=account.id;// here i am getting error like.. Compile Error: Illegal assignment from Id to SOBJECT:Account
        insert contact;
        
    }


}

 please help me out.

 

Thanks,

Yarram.

sunny522sunny522
Hi Yarram,
just replace the error line with
contact.AccountId =account.id;

while doing this type of tasks see relations from validation rules.

If u found my post as solution to ur problem,mark it as a solution.