• Sha
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 1
    Replies
Hi Friends, 

Need quick help with a trigger for below cenario. 

Lets say i have a "Company" object and "Industry" field in it with values (CIP, FED, OTH) , Now when ever Company record is updated with Industry values child object "Job" field "Industry" should be updated catch is here if CIP is selected in child object it should show 
Consumer and Industrial Products if FED is selected in company in job it should show up Federal (Basically it should map the field)
  • July 25, 2016
  • Like
  • 0
trigger Job_Count_Trigger on Job__c (after insert, after update, after delete, after undelete) 
{
    list <Job__c> Jobs = Trigger.isDelete ? Trigger.old : Trigger.new; 
    set<Id> compIds= new Set<Id>();
    List<company__c> compRollup = new List<company__c>();  
    
    
   // Job_count_trigger_handler.updateMethod(trigger.new);
   
    for (AggregateResult co : [SELECT Company__c compId, Count(id) JobCount FROM Job__c group by Company__c ])
     {
        
      if(co.get('compId')!=null)
      {
        Company__c c= new Company__c();
         system.debug('id in loop + '+ co.get('compId'));
        c.Id = (Id) co.get('compId');
        c.Count_of_Jobs__c = (Integer) co.get('JobCount');
        compRollup.add(c);
      }
         else
         {
             
             System.debug('### Empty Conpany');
         }
         
     }
        system.debug(''+compRollup);
    update compRollup;
    }
Hi Please help on writing the test class for trigger..

Thanks 
shareef,
 
  • July 20, 2016
  • Like
  • 0

i have added one event button in contacts, when clicked it should navigate to events object picking up events in lookup. i have excuted it but i am facing with test class coverage. can any one help me please.

 

Class

 

public class AddToEvents {

//Variable Declaration
ApexPages.StandardSetController setCon;
public Event_Registration__c eventRegObj {get;set;}
public Integer selectedSize {get;set;}
public String selectedMemberStatus{get;set;}
public String selectedOption{get;set;}
Set<Id> sObjectIds = new Set<Id>();
List<Event_Registration__c> eventRegistrationsToCreate = new List<Event_Registration__c>();
Map<Id,Event_Registration__c> eventRegistrationsExisting = new Map<Id,Event_Registration__c>();
public AddToEvents(ApexPages.StandardSetController controller) {
setCon = controller;
eventRegObj = new Event_Registration__c();
selectedSize = setCon.getSelected().size();
selectedOption = '1';
//If no contacts are selected, display an error message.
if(selectedSize==0){
if(setCon.getRecord().getsObjectType() == Contact.sObjectType)
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please select at least one contact'));
else if(setCon.getRecord().getsObjectType() == Lead.sObjectType)
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please select at least one Lead'));
}
for(sObject s:setCon.getSelected())
sObjectIds.add(s.id);
}

//This method adds the selected contacts as Event Registrants.
public pagereference AddeventRegistrations(){
//Fetch existing Event Registrants with the same contact/lead and Recruiting Event Ids.
if(setCon.getRecord().getsObjectType() == Contact.sObjectType) {
// for(Event_Registration__c c:[Select Contact__c,CRM_Status__c from Event_Registration__c where Contact__c in:sObjectIds and Recruiting_Event__c =:eventRegObj.Id])
for(Event_Registration__c c:[Select Id,Contact__c from Event_Registration__c where Contact__c in:sObjectIds and Recruiting_Event__c =: eventRegObj.Recruiting_Event__c])
eventRegistrationsExisting.put(c.Contact__c,c);
}
for(sObject con:setCon.getSelected()){
Event_Registration__c er = new Event_Registration__c();
if(eventRegistrationsExisting.containsKey(con.Id)){
er = eventRegistrationsExisting.get(con.id);

}else{
er.Recruiting_Event__c = eventRegObj.Recruiting_Event__c;
if(setCon.getRecord().getsObjectType() == Contact.sObjectType)
er.Contact__c = con.Id;
//er.CRM_Status__c = selectedMemberStatus;
}
eventRegistrationsToCreate.add(er);
}
try{
//Update or Insert campaign members
upsert eventRegistrationsToCreate;
}catch(Exception e){
ApexPages.addMessages(e);
return null;
}

//Redirect to the campaign detail page.
Pagereference p= new Pagereference('/'+eventRegObj.Recruiting_Event__c);
p.setRedirect(true);
return p;
}
}

 

Test

------

 

 

@isTest
private class Test_AddToEvent
{

static testMethod void AddeventRegistrations(){

Event_Registration__c c = new Event_Registration__c();
insert c;
List<Contact> conList = new List<Contact>();
List<Recruiting_Event__c> recList = new List<Recruiting_Event__c>();
List<Event_Registration__c> eventList = new List<Event_Registration__c>();
Contact con = new Contact(LastName = 'Syed');
Contact con1 = new Contact(LastName = 'Yeturi');
conList.add(con);
conList.add(con1);
insert conList;
Recruiting_Event__c er = new Recruiting_Event__c(Name='Blitz New York Event');//Name='Blitz New York Event');
Recruiting_Event__c er1 = new Recruiting_Event__c(Name='Select Event');//Name ='Select Event');
recList.add(er);
recList.add(er1);
insert recList;
ApexPages.StandardSetController setCon = new ApexPages.StandardSetController(conList);
ApexPages.StandardSetController setCon1 = new ApexPages.StandardSetController(recList);
AddToEvents events = new AddToEvents(setCon);

setCon.setSelected(conList);

AddToEvents event= new AddToEvents(setCon);
event.eventRegObj.Recruiting_Event__c=c.id;
pagereference p = event.AddeventRegistrations();
event.eventRegObj.Recruiting_Event__c=null;
pagereference p1=event.AddeventRegistrations();

AddToEvents event1 = new AddToEvents(setCon1);
setCon1.setSelected(eventList);
event1.eventRegObj.Recruiting_Event__c=c.id;
pagereference p2 = event1.AddeventRegistrations();

}
}

 

 

but it doesnt cover the code so can any one help me in code coverage. 

  • October 23, 2013
  • Like
  • 0
Hi Friends, 

Need quick help with a trigger for below cenario. 

Lets say i have a "Company" object and "Industry" field in it with values (CIP, FED, OTH) , Now when ever Company record is updated with Industry values child object "Job" field "Industry" should be updated catch is here if CIP is selected in child object it should show 
Consumer and Industrial Products if FED is selected in company in job it should show up Federal (Basically it should map the field)
  • July 25, 2016
  • Like
  • 0