• Shriya Bhalchandran
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 12
    Replies
<b>Apex Class</b>

public class RenewOppEndate
{
     public static final string CRenewOppEndate = 'RenewOppEndate';
     public static final string UPDATEENDATE = 'updateEndate';
    
    public static void updateEndate(List<Opportunity> oppNewList)
    {
        try
        {
            set<Id> accIdSet = new set<Id>();
            Date TempEffDate = NULL;
            
            for(Opportunity opp: oppNewList)
            {
                if(opp.AccountId != NULL && opp.Type == Label.Renewal)
                {
                    accIdSet.add(opp.AccountId);
                }
            }
            Map<ID,Account> accMap = new Map<ID,Account>([select Id,(select Id,Effective_Date__c from Opportunities Order By Effective_Date__c DESC LIMIT 1) 
                                     from Account where ID IN:accIdSet LIMIT 50000]);
            
            for(Opportunity opp : oppNewList)
            {
                if(String.isNotBlank(opp.AccountId) && opp.Type == Label.Renewal){
                    Account accObj = accMap.get(opp.AccountId);
                    List<Opportunity> oldOppList = accObj.opportunities;
                    if(oldOppList != NULL && !oldOppList.isEmpty()){
                        Date tempEffectiveDate = oldOppList[0].Effective_Date__c; 
                        opp.Effective_Date__c = tempEffectiveDate != NULL ? tempEffectiveDate.addYears(1) : tempEffectiveDate ;
                    }
                }               
            }
        }
        catch(exception ex)
        {UTIL_LoggingService.logHandledException(ex);}
    }
    
}


<b>Test Class

</b>
<b>@isTest
public class RenewOppEndate_Test
{
    
    private static testMethod void renewOpptyTest()
    {   
        User testUser = Util02_TestData.createUser();
        
        Account testAccount = Util02_TestData.createAccountData();   
        
        System.runAs(testUser)
        {
           
            Database.insert(testAccount);
            
            Opportunity opp = Util02_TestData.createOpportunity();
            opp.AccountId = testAccount.id;
            opp.stageName = Label.Renewal;
            database.insert(opp);   
        }       
    }
        
}     </b>

 
It should display all the related activities for the Account object through visualforce force. 

                
Activity Date    Activity Type    Created By    Name    Title
10/26/2015          Call              
10/08/2015          Call              
06/29/2015          Call              

Activity Date: Refers to the Due Date in the record.
Activity Type: Refers to the Type in the record.
Created By:  Refers to the Created By in the record.
Name: Refers to the Name in the record. (Lookup to Contact Field)
 
Req: one custom object: Test...which has lookup relationship with Account object.
So now as we know in Account related list:> open Activities> we can create new event.
so on creating new event, wherein if status is Closed...that same record should automatically create in test object.

Solution:
trigger records on Event (before insert) {

Map<Id, Event> eventsByAccount = new Map<Id, Event>();   // Map all the events to their account.
    for(Event ev : (Event[]) Trigger.new) {
        eventsByAccount.put(ev.WhatId, ev);
}

Map<Id,Employee__c> EmpsById = new Map<Id,Employee__c>([SELECT Id,EmpAcc__c FROM Employee__c WHERE EmpAcc__c IN: eventsByAccount.keyset()]);  // Bring all the employee records related to your accounts.

List<Event> testEventsToInsert = new List<Event>();
  for(Employee__c emp : EmpsById.values()) {                           // Get the event of the test's account.
      Event testEvent = eventsByAccount.get(emp.EmpAcc__c);

      testEvent.WhatId = emp.Id;                                                 // Associate it to Employee__c record.

      testEvent.Id = null;                                                              // Clean its Id so you can insert the new event.

      testEventsToInsert.add(emp);                                              // Add it to a list for later insertion.
}

     insert testEventsToInsert;                                                    // Insert all the new events related to test records.
    



I have created this trigger but 1 error is coming on line 23 as Method does not exist or incorrect signature: void add(Employee__c) from the type List<Event>. Please help me in resolving this. 
Test object is in lookup relation with account,When we will create Event Record with Condition (Event Status = Closed) and once we click on save ,it should create test record automatically.What will be the trigger for it?
There is one custom object, which has lookup to Account object. On creating new event in Account object, the same record should get updated in Custom object.
How can this be acheived?
<b>Apex Class</b>

public class RenewOppEndate
{
     public static final string CRenewOppEndate = 'RenewOppEndate';
     public static final string UPDATEENDATE = 'updateEndate';
    
    public static void updateEndate(List<Opportunity> oppNewList)
    {
        try
        {
            set<Id> accIdSet = new set<Id>();
            Date TempEffDate = NULL;
            
            for(Opportunity opp: oppNewList)
            {
                if(opp.AccountId != NULL && opp.Type == Label.Renewal)
                {
                    accIdSet.add(opp.AccountId);
                }
            }
            Map<ID,Account> accMap = new Map<ID,Account>([select Id,(select Id,Effective_Date__c from Opportunities Order By Effective_Date__c DESC LIMIT 1) 
                                     from Account where ID IN:accIdSet LIMIT 50000]);
            
            for(Opportunity opp : oppNewList)
            {
                if(String.isNotBlank(opp.AccountId) && opp.Type == Label.Renewal){
                    Account accObj = accMap.get(opp.AccountId);
                    List<Opportunity> oldOppList = accObj.opportunities;
                    if(oldOppList != NULL && !oldOppList.isEmpty()){
                        Date tempEffectiveDate = oldOppList[0].Effective_Date__c; 
                        opp.Effective_Date__c = tempEffectiveDate != NULL ? tempEffectiveDate.addYears(1) : tempEffectiveDate ;
                    }
                }               
            }
        }
        catch(exception ex)
        {UTIL_LoggingService.logHandledException(ex);}
    }
    
}


<b>Test Class

</b>
<b>@isTest
public class RenewOppEndate_Test
{
    
    private static testMethod void renewOpptyTest()
    {   
        User testUser = Util02_TestData.createUser();
        
        Account testAccount = Util02_TestData.createAccountData();   
        
        System.runAs(testUser)
        {
           
            Database.insert(testAccount);
            
            Opportunity opp = Util02_TestData.createOpportunity();
            opp.AccountId = testAccount.id;
            opp.stageName = Label.Renewal;
            database.insert(opp);   
        }       
    }
        
}     </b>

 
Req: one custom object: Test...which has lookup relationship with Account object.
So now as we know in Account related list:> open Activities> we can create new event.
so on creating new event, wherein if status is Closed...that same record should automatically create in test object.

Solution:
trigger records on Event (before insert) {

Map<Id, Event> eventsByAccount = new Map<Id, Event>();   // Map all the events to their account.
    for(Event ev : (Event[]) Trigger.new) {
        eventsByAccount.put(ev.WhatId, ev);
}

Map<Id,Employee__c> EmpsById = new Map<Id,Employee__c>([SELECT Id,EmpAcc__c FROM Employee__c WHERE EmpAcc__c IN: eventsByAccount.keyset()]);  // Bring all the employee records related to your accounts.

List<Event> testEventsToInsert = new List<Event>();
  for(Employee__c emp : EmpsById.values()) {                           // Get the event of the test's account.
      Event testEvent = eventsByAccount.get(emp.EmpAcc__c);

      testEvent.WhatId = emp.Id;                                                 // Associate it to Employee__c record.

      testEvent.Id = null;                                                              // Clean its Id so you can insert the new event.

      testEventsToInsert.add(emp);                                              // Add it to a list for later insertion.
}

     insert testEventsToInsert;                                                    // Insert all the new events related to test records.
    



I have created this trigger but 1 error is coming on line 23 as Method does not exist or incorrect signature: void add(Employee__c) from the type List<Event>. Please help me in resolving this. 
Test object is in lookup relation with account,When we will create Event Record with Condition (Event Status = Closed) and once we click on save ,it should create test record automatically.What will be the trigger for it?
There is one custom object, which has lookup to Account object. On creating new event in Account object, the same record should get updated in Custom object.
How can this be acheived?