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
bharath kuamarbharath kuamar 

Hi

Help me out with the test classs for the trigger below ,

 

trigger updateContact on MyProfile__c (after insert) {
        MyProfile__c profile=new MyProfile__c();
        Contact donorObj=new Contact();
        donorObj.LastName = profile.Name;
        donorObj.FirstName = profile.First_Name__c;
        donorObj.BirthDate = profile.Birthdate__c;
        donorObj.Description=profile.Description__c;
        donorObj.Email=profile.Email__c;
        donorObj.Phone=profile.Phone__c;
        donorObj.Fax=profile.Fax__c;
        donorObj.Mobilephone=profile.Mobile__C;
        update donorObj;
       
}
Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

 

I am not sure which contact you are updating after the insert of MyProfile__c. So whenever I am inserting the test record inside the test method I am getting an exception about which contact I am going to update I have to specify the id of that.  So I have changed your trigger by adding simply try and catch block.

 

Try the below code as reference:

////////////////////////// Trigger ////////////////////////////////////////////

trigger updateContact on MyProfile__c (after insert) {

        try{

        MyProfile__c profile=new MyProfile__c();

        Contact donorObj=new Contact();

        donorObj.LastName = profile.Name;

        donorObj.FirstName = profile.First_Name__c;

        donorObj.BirthDate = profile.Birthdate__c;

        donorObj.Description=profile.Description__c;

        donorObj.Email=profile.Email__c;

        donorObj.Phone=profile.Phone__c;

       donorObj.Fax=profile.Fax__c;

        donorObj.Mobilephone=profile.Mobile__C;

        update donorObj;

        }catch(exception e){}

      

}

////////////////////////// Test Method /////////////////////////////////

@isTest

private class testTriggerTestCoverage

{

        public static testMethod void unitTest()

        {

            MyProfile__c profile=new MyProfile__c(name='test',email__c='asdf@test.com',description__c='aaaaaaaaaaaaaa',First_Name__c='abcd');

            insert profile;

           

        }

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

I am not sure which contact you are updating after the insert of MyProfile__c. So whenever I am inserting the test record inside the test method I am getting an exception about which contact I am going to update I have to specify the id of that.  So I have changed your trigger by adding simply try and catch block.

 

Try the below code as reference:

////////////////////////// Trigger ////////////////////////////////////////////

trigger updateContact on MyProfile__c (after insert) {

        try{

        MyProfile__c profile=new MyProfile__c();

        Contact donorObj=new Contact();

        donorObj.LastName = profile.Name;

        donorObj.FirstName = profile.First_Name__c;

        donorObj.BirthDate = profile.Birthdate__c;

        donorObj.Description=profile.Description__c;

        donorObj.Email=profile.Email__c;

        donorObj.Phone=profile.Phone__c;

       donorObj.Fax=profile.Fax__c;

        donorObj.Mobilephone=profile.Mobile__C;

        update donorObj;

        }catch(exception e){}

      

}

////////////////////////// Test Method /////////////////////////////////

@isTest

private class testTriggerTestCoverage

{

        public static testMethod void unitTest()

        {

            MyProfile__c profile=new MyProfile__c(name='test',email__c='asdf@test.com',description__c='aaaaaaaaaaaaaa',First_Name__c='abcd');

            insert profile;

           

        }

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
bharath kuamarbharath kuamar

thanks a lot, it worked

bharath kuamarbharath kuamar
trigger displayCommitment on Commitment__c (after insert)
{
   if (Trigger.isAfter)
   {
       Set<String> set_string = new Set<String>();
       Map<String, Commitment__c> map_pref = new Map<String,Commitment__c>(); 
       for ( Commitment__c cmmit : Trigger.new )
       {
           if (cmmit.Preference__c != null)
           {
               set_string.add(cmmit.Preference__c);
               map_pref.put(cmmit.Preference__c, cmmit);
           }
       }
       if (set_string.size() > 0)
       {
           List<Opportunity> list_opportunity = new List<Opportunity>();
           for (Opportunity obj : [Select Id, Preference__c From Opportunity Where Preference__c In : set_string])
           {
             Commitment__c cObj = map_pref.get(obj.Preference__c);
             Opportunity updateOpportunity = obj;
             //updateOpportunity.Commitment_Name__c = cObj.Name;
             updateOpportunity.Commitment_TypePreference__c = cObj.Commitment_Type__c;
             updateOpportunity.StartDate__c = cObj.Start_Date__c;
             updateOpportunity.EndDate__c =cObj.End_Date__c;
             updateOpportunity.Commitment_Details__c =cObj.Commitment_Details__c;
             updateOpportunity.contact__c =cObj.Contact__r.name;
             updateOpportunity.Status__c =cObj.Status__c;
             updateOpportunity.Child__c =cObj.Child__r.name;
             system.debug('<<<<<<<<test>>>>>>'+cObj.Child__r.name);
             list_opportunity.add(updateOpportunity);
             System.Debug('<<Sucess>>');
           } 
           if (list_opportunity != null && list_opportunity.size() > 0) update list_opportunity;
       }
   }
   }