• Mehul p
  • NEWBIE
  • 10 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 8
    Questions
  • 3
    Replies
Hi everyone,

     I have this trigger on contact and I have no idea how to write the test class for this. Any help will be greatly appreciated. 

Thank you in advance!
public class contactHandlerClass {
     public static boolean contactRecursion =false; // to avoid the "maximum trigger depth exceeded" error


    public static void getRecentTaskInformation(List<Contact> contacts) {
       
        System.debug('print the list' + contacts);
 //Tasks     
        List<Contact> contactsWithLastTasks = [
            SELECT Id, Last_Activity_Subject__c, Last_Activity_Date__c,Contact_Task_Date__c,	Most_Recent_Contact_Task_Subject__c,
                Last_Activity_Name__c , Last_Activity_Assigned_to__c ,
                Last_Activity_TypeOfInteraction__c , Name, (
                    SELECT Subject, ActivityDate, Owner.Name,Task.WhoId,
                        Type_of_Interaction__c
                    FROM Tasks
                    ORDER BY ActivityDate DESC 
                    LIMIT 1
                )
            FROM Contact WHERE Id IN :contacts
        ];
      
        for (Contact c : contactsWithLastTasks) {
            if (!c.tasks.isEmpty()) {
                Task lastTask = c.tasks[0];
                System.debug('print Tasks' + lastTask);

                c.Most_Recent_Contact_Task_Subject__c = lastTask.Subject;
                c.Contact_Task_Date__c = lastTask.ActivityDate;
                // may be create field and set it text "Task" to identify if it is Task or Event
                
                System.debug('Print contacts list one by one?'+ contactsWithLastTasks);
                System.debug('Field one?'+ c.Most_Recent_Contact_Task_Subject__c);
                System.debug('Field two?'+ c.Contact_Task_Date__c);
        
            }
        }
        If(!contactsWithLastTasks.isEmpty()){
        Update contactsWithLastTasks;
        }
 //Events
        List<Contact> contactsWithLastEvents = [
            SELECT Id, Last_Activity_Subject__c, Last_Activity_Date__c,Contact_Task_Date__c,	Most_Recent_Contact_Task_Subject__c,
                Last_Activity_Name__c , Last_Activity_Assigned_to__c ,
                Last_Activity_TypeOfInteraction__c , Name, (
                    SELECT Subject, StartDateTime
                    FROM Events
                    ORDER BY ActivityDate DESC 
                    LIMIT 1
                )
            FROM Contact WHERE Id IN :contacts
        ];
      
        for (Contact c : contactsWithLastEvents) {
            if (!c.events.isEmpty()) {
                Event lastEvent = c.events[0];
                System.debug('print Tasks' + lastEvent);

                c.Most_Recent_Contact_Task_Subject__c = lastEvent.Subject;
                //c.Contact_Task_Date__c = lastEvent.;
                // may be create field and set it text "Task" to identify if it is Task or Event
                
                System.debug('Print contacts list one by one?'+ contactsWithLastEvents);
                System.debug('Field one?'+ c.Most_Recent_Contact_Task_Subject__c);
                System.debug('Field two?'+ c.Contact_Task_Date__c);
        
            }
        }
        If(!contactsWithLastEvents.isEmpty()){
        Update contactsWithLastEvents;
        }
    }

}


 
This trigger inserts and updates contact fields fine for the first contact but when there are multiple contacts in Task, the task does not update contact fields for the rest of the contacts. What am I doing wrong? Thank you in advance!
 
if (Trigger.isafter && Trigger.isInsert){ 
              ActivityHandlerClass.insertContact(Trigger.New);
     System.debug('I RAN INSERT');

     } 



public class ActivityHandlerClass {
        public static void insertContact(List<Task> tks){ // Trigger.New comes as List here
        Set<Id> ContactIds = new Set<Id>();
        List<Contact> ContactList = new List<Contact>();

        List<Task> taskContacts = [SELECT Id, Subject, (SELECT RelationId, Relation.Name from TaskRelations) FROM Task WHERE id in:tks];
        System.debug('Print Task Contacts: ' + taskContacts);

        for(Task ti: taskContacts)
        {
            for(TaskRelation tRel: ti.TaskRelations)
            {
               System.debug('Print Task Contacts: ' + tRel);
                System.debug('Print Task Contact Size: ' + ti.TaskRelations.Size());
                If(ti.TaskRelations.Size() >0){
                       ContactIds.add(tRel.RelationId);
                  System.debug('Print added to ContactIds: ' + ContactIds);

                }
            }

        }
                List<Contact> relatedContacts = [select id,Last_Contact_Date__c,LastActivityDate, Last_Activity_Subject__c from Contact where id in:ContactIds];
                System.debug('Print RelatedContacts :' + relatedContacts);

        for(Task t :tks)
        {
                for(Contact con : relatedContacts)
                {  
                  If(t.ActivityDate >=con.Last_Contact_Date__c)
                    {
                    con.Last_Activity_Subject__c = t.subject;
                    con.Last_Activity_Date__c = t.ActivityDate;

                    ContactList.add(con);
                         System.debug('Printining that ADDDDDDDDDDDDDDDDDDDDD   ran');  
                         System.debug('ContactList is being printed:' + ContactList); 
                         System.debug('ContactList ID is being printed:***********' + con.id); 
                    } 
                 } 
            System.debug('Update ContactList***************');
        } 

           If(ContactList.size()>0)
           {   
            upsert ContactList; 
           }
  } // End of Insert Method

 
  • September 25, 2019
  • Like
  • 0
Hi guys,
 
        I am stuck on this problem where I want to loop through task id and their task relation contacts ids. I am not sure how to do it. 
 
List<Contact> relatedContacts = [select id,Last_Activity_Subject__c,
                                                 (select id,Subject from Tasks Order By ActivityDate DESC LIMIT 1)
                                                                      from Contact where id in:ContactIds];
                
        
                for (Contact relatedContact : relatedContacts)
                    {
                        relatedContact.Last_Activity_Subject__c = relatedContact.Tasks[0].Subject;

                    }
                    update relatedContacts;
this code I have it does not update properly for the task relation names. 

Thank you!
 
  • September 16, 2019
  • Like
  • 0
Hi guys,

      We have an object called deals and I want to clone it to Opportunity object. What is best to copy all the field and bring it over to Opportunity objects?

Thanks,
Hi guys,

    I have this trigger on the account and it is not updating at all on contacts. there are no errors either. I tried updating fields on the account and it did not update them on contacts.
 
trigger contactAccountTrigger on Account (after update) {
   Set<Id> accountIds = new Set<Id>();
   List<Contact> contactList1 = new List<Contact>();
    for(Account acc: Trigger.new){
        Account oldAccount = Trigger.oldMap.get(acc.ID);
           If(acc.Contacts.size()>0){
          
                    accountIds.add(acc.id);  

            }
    }//End of Loop

    If(accountIds.size()>0){
         Map<Id,Account> mapAccount = new Map<Id,Account> ([select id,Tier__c,Type, (select id,Account_Tier__c,Account_Type__c from contacts ) from account where id in :accountIds ]);
         List<Contact> contactList = new List<Contact>();
        for(account acc :Trigger.new)
        {
            //Account oldAccount = Trigger.oldMap.get(acc.ID);
      
                 
                If(mapAccount.containsKey(acc.id))
                {
                    Account accObj = mapAccount.get(acc.id);
                    List<Contact> lstCont = accObj.contacts;
                  	for(contact c:lstCont)
                   		  {
                           c.Account_Tier__c=acc.Tier__c;
                           c.Account_Type__c = acc.Type;
                           contactList.add(c);     
                    	  }//end of for loop  
                        System.debug('2nd Loop- Inside');
               		 }         
           
        } //end of Acc1 for loop 
        
          if(contactList.size()>0)
        {
           update contactList1;
        }
            
    }// End of accountIds.size()
      
  }//End of trigger

Thanks in advance!!
Hi guys,
       My code coverage is at 77% at this time and I need to increase it or try to get it 100%. Here' s the red section which is not covered. 

User-added image

and here 's the test code.
 
Account acc = new Account();
		acc.Name ='Test';
		insert acc;
		
		Contact cont = new Contact();
		cont.FirstName ='Test';
		cont.LastName ='testContact';
        cont.Last_Activity_Subject__c='Run Test Trigger';
		cont.accountid =acc.id;
        cont.Last_Activity_Date__c = Date.parse('6/15/2019');
        
		insert cont;
		
	
		Task u = new Task();
        	u.ownerId = UserInfo.getUserId();
			u.whatid = acc.id;
			u.whoid = cont.id;
			u.Subject='Run Test Trigger';
			u.Status='Not Started';
			u.Priority='Normal';
            u.ActivityDate = Date.parse('6/15/2019');
        	
        insert u;
		
	
		Test.startTest();
		
			update u;
            for (Task t : [SELECT Id, Subject, Status FROM Task WHERE Id = :cont.Id]) {
   
       		 	System.assertEquals('Run Test Trigger',t.Subject); // asserts that your test worked properly
        		System.assertEquals('Not Started', t.Status); // asserts that your test worked properly
   				 }
        
            for (Contact u1 : [SELECT Id, Last_Activity_Subject__c, Last_Activity_Date__c FROM Contact WHERE Id = :cont.Id]) {
                
       		 	System.assertEquals('Run Test Trigger', u1.Last_Activity_Subject__c); // asserts that your test worked properly
        		
   				 }
        	
		Test.stopTest();

How do I increase the code coverage from here? What am I missing the test class?
Hi guys,
       I need a help here.. I am trying to extract Name from Tasks but I got the ID instead. Is there any way we could get the name from Task.
User-added image
Map<Id,Contact> ContactMap =  new Map<Id,Contact>([select id, Last_Activity_Subject__c,	Last_Activity_Date__c, Last_Contact_First_Name__c, Last_Contact_Last_Name__c from Contact where id in:ContactIds]);
        
        for(Task t :Trigger.new)
    
            for(Contact l : ContactMap.Values())
            {  
               If(t.subject does not contain "words" ) // how to filter some subject from here?
                l.Last_Activity_Subject__c = t.subject;
                l.Last_Activity_Date__c = t.ActivityDate;
                l.Last_Contact_First_Name__c = t.WhoId;  //<= I need a Name here Not an ID.
                
                
                ContactList.add(l);
            }
Thanks in advance!
Hello Everyone,
       I am stuck on this error and I am not sure what  I am doing wrong.
 
trigger testTriggeronContact on Contact (before insert) {

List<Contact> contactCheck = new List<Contact>();
List<Account> AccountCheck = new List<Account>();
          

If(trigger.isBefore && trigger.isinsert){


For(Contact c : trigger.new){
     
     //Get contactId from Trigger

      string contactId = c.Id;

        
     //Gets Current AccountId Related to Contact
     Contact AccId = [SELECT AccountId FROM Contact WHERE Id = :contactId limit 1];
      
    //Gets Account Team Members from Account Related to Contact 
    List<AccountTeamMember> atmID =[Select UserId From AccountTeamMember WHERE AccountId  = :accId.AccountId ];

    for (User x : atmID){
          If (x == User.Id){
           some code here...
          }
          else{
            some code here....         
          }
        }

}
}
}
I am getting this error at line If( x==User.Id). 

Not sure How I can compare AccountTeamRoles UserIds to Contact's UserIds without getting an error?

Any guidance will be of great help.

Thank you,
 
Hi guys,
       My code coverage is at 77% at this time and I need to increase it or try to get it 100%. Here' s the red section which is not covered. 

User-added image

and here 's the test code.
 
Account acc = new Account();
		acc.Name ='Test';
		insert acc;
		
		Contact cont = new Contact();
		cont.FirstName ='Test';
		cont.LastName ='testContact';
        cont.Last_Activity_Subject__c='Run Test Trigger';
		cont.accountid =acc.id;
        cont.Last_Activity_Date__c = Date.parse('6/15/2019');
        
		insert cont;
		
	
		Task u = new Task();
        	u.ownerId = UserInfo.getUserId();
			u.whatid = acc.id;
			u.whoid = cont.id;
			u.Subject='Run Test Trigger';
			u.Status='Not Started';
			u.Priority='Normal';
            u.ActivityDate = Date.parse('6/15/2019');
        	
        insert u;
		
	
		Test.startTest();
		
			update u;
            for (Task t : [SELECT Id, Subject, Status FROM Task WHERE Id = :cont.Id]) {
   
       		 	System.assertEquals('Run Test Trigger',t.Subject); // asserts that your test worked properly
        		System.assertEquals('Not Started', t.Status); // asserts that your test worked properly
   				 }
        
            for (Contact u1 : [SELECT Id, Last_Activity_Subject__c, Last_Activity_Date__c FROM Contact WHERE Id = :cont.Id]) {
                
       		 	System.assertEquals('Run Test Trigger', u1.Last_Activity_Subject__c); // asserts that your test worked properly
        		
   				 }
        	
		Test.stopTest();

How do I increase the code coverage from here? What am I missing the test class?
Hi guys,
       I need a help here.. I am trying to extract Name from Tasks but I got the ID instead. Is there any way we could get the name from Task.
User-added image
Map<Id,Contact> ContactMap =  new Map<Id,Contact>([select id, Last_Activity_Subject__c,	Last_Activity_Date__c, Last_Contact_First_Name__c, Last_Contact_Last_Name__c from Contact where id in:ContactIds]);
        
        for(Task t :Trigger.new)
    
            for(Contact l : ContactMap.Values())
            {  
               If(t.subject does not contain "words" ) // how to filter some subject from here?
                l.Last_Activity_Subject__c = t.subject;
                l.Last_Activity_Date__c = t.ActivityDate;
                l.Last_Contact_First_Name__c = t.WhoId;  //<= I need a Name here Not an ID.
                
                
                ContactList.add(l);
            }
Thanks in advance!
I wonder if someone might help me with this.   I am trying to clone a custom object using eclipse and get most of the way before I run into a problem.  I used Eclipse to create a project in a Sandbox.  Basically I copied and pasted the xml from an existing object to a new object.  I then, according to a video on You Tube, I changed the name in the label and plural label,  and the relationship names and labels.  I then saved it and did a "save to server".   I had no errors in the problems window.   I looked at the object in the sandbox and the new object was there with all the fields.   I then created a Change Set and uploaded it to the production envionment.  Still no problems.   I then deployed it in Production and received this error "In field: searchResultsFields - no CustomField named BIPP_Profile_Clone3__c.Date_of_Enrollment__c found".   I am stumped as I don't know what this error message means.   Hopefully someone can help.   
Thanks,
Steve