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
LudivineLudivine 

Help for Test Trigger

Dear all,

I don't manage to have at least 1% code coverage for my trigger , could you help me please?

What it does, From clicktools url, when a contact answers 'Yes' on the Survey, as soon as it is created in Salesforce , 
this triggers looks in the Field Q6_NeedAmcorToContactYou__c, if value is 'Yes', it creates an Event with the following values.

trigger TriggerEventForCustomerToContact on Amcor_Surveys__c (After insert)
{
     set<id> setamcorIds= new set<id>();
For (Amcor_Surveys__c ams : trigger.new)
{
  If(ams.Q6_NeedAmcorToContactYou__c == 'Yes')
  {
     setamcorIds.add(ams.Id);
  }
}

list<Amcor_Surveys__c> listSelectedAmcor= [select Account_ID__r.OwnerId,Contact_ID__c
                                            from  Amcor_Surveys__c
              where id in :setamcorIds];
System.debug('Creating event record');

list<Event> listEventToCreate= new list<Event> ();
for(Amcor_Surveys__c ams :listSelectedAmcor)
{
  //create an event record
  Event ev = new Event();
  Ev.Business_group__c ='Amcor Flexibles Europe & Americas';
  Ev.Whatid = ams.id;
  ev.RecordTypeId='012g00000004ajF';//'Visit Report VOC';
  ev.Type ='Customer Satisfaction Survey';
  ev.DurationInMinutes = 720;
  ev.Ownerid = ams.Account_ID__r.OwnerId;
  ev.whoId =ams.Contact_ID__c;
  ev.Subject ='Customer Satisfaction Survey - Customer contact' ;
  ev.objective__c = 'Improve Relationship';
  //System.debug('************ ' + system.today());
  Date startDate = system.today();
  ev.ActivityDate = startDate;
  ev.StartDateTime = Datetime.newInstanceGmt(startDate.year(), startDate.month(), startDate.day(), 0, 0, 9);
   ev.ActivityDateTime = Datetime.newInstanceGmt(startDate.year(), startDate.month(), startDate.day(), 0, 0, 09);
  Date EndDate = startdate +5;
  System.debug('Attempting to insert...');
  
  listEventToCreate.add(ev);
    }
try{
insert listEventToCreate;
}
catch(Exception e)
{
  System.debug(e.getMessage());
  }
}

Could you write it for me?
Many thanks for your help.

Best Answer chosen by Ludivine
rohitsfdcrohitsfdc
Hello there,
Create a new class as below. Hope this helps you.

@istest(seealldata=true)
private class MyTestclass_Test {
 static testMethod void myUnitTest() {

Account acc = new account(name='test');
insert acc;

Contact con = new contact(accountid=acc.id,lastname='lastname');
insert con;

Amcor_Surveys__c ams = new Amcor_Surveys__c();
ams.Q6_NeedAmcorToContactYou__c='Yes';
ams.account_Id__r = acc;
ams.contact_id__c = con.id;

insert ams;


}
}


All Answers

rohitsfdcrohitsfdc
Hello there,
Create a new class as below. Hope this helps you.

@istest(seealldata=true)
private class MyTestclass_Test {
 static testMethod void myUnitTest() {

Account acc = new account(name='test');
insert acc;

Contact con = new contact(accountid=acc.id,lastname='lastname');
insert con;

Amcor_Surveys__c ams = new Amcor_Surveys__c();
ams.Q6_NeedAmcorToContactYou__c='Yes';
ams.account_Id__r = acc;
ams.contact_id__c = con.id;

insert ams;


}
}


This was selected as the best answer
LudivineLudivine
Fantastic!!

Many many thanks rohit, I have spent too much time to built a big class and just this simple one covered 96% of my trigger, much more than expected!
Again many thanks :)