• The Finnster
  • NEWBIE
  • 10 Points
  • Member since 2020

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

I have a trigger that automates the opportunity end date based on the start date and term on the opportunity product.

I need help with the test class to achive 75% coverage to push it to production please.

Any help would be appreciated.

trigger OpportunityProductEndDate on OpportunityLineItem (before insert, before update, before delete) {
    Set<Id> opportunityProduct = new Set<Id>();
    Set<id> idSet = new Set<id>();
    Set<id> OppidSet = new Set<id>();
    List<Revenue__c> revenueList = new List<Revenue__c>();
    if((Trigger.isInsert || Trigger.isUpdate) && Trigger.isBefore){
    for(OpportunityLineItem opli: Trigger.New){
            idSet.add(opli.product2Id); 
            OppidSet.add(opli.OpportunityId);
            System.debug('-------OpportunityRecordType__c '+opli.OpportunityRecordType__c);
            System.debug('-------- contract start date'+ opli.Contract_Start_Date__c);
            System.debug('-------- contract start date'+ opli.Contract_Term__c);
            
            if(opli.OpportunityRecordType__c == 'Display-Direct' || opli.OpportunityRecordType__c == 'Display-Agency'){      
           if( opli.Start_Date__c != null && opli.Product_Family__c == 'Display-Developer Listings' ){
                    opli.End_Date__c = opli.Start_Date__c .addMonths(Integer.valueOf(opli.Term__c))-1;
            }
            
            }
            //if(opli.OpportunityRecordType__c != 'Display-Direct' && opli.OpportunityRecordType__c != 'Display-Agency' && opli.OpportunityRecordType__c != 'Jobs Opportunities' && opli.OpportunityRecordType__c != 'Jobs Dubai' && opli.OpportunityRecordType__c != 'Job Amend'){
            if(opli.OpportunityRecordType__c != 'Display-Direct' && opli.OpportunityRecordType__c != 'Display-Agency'){
                System.debug('recordType check');
                if(opli.Term__c == null && opli.OpportunityRecordType__c != 'Jobs Opportunities' && opli.OpportunityRecordType__c != 'Jobs Dubai' && opli.OpportunityRecordType__c != 'Jobs Amend'){
                    opli.Term__c = String.ValueOf(opli.Contract_Term__c); 
                }
                /*if(opli.Term__c == null && (opli.OpportunityRecordType__c == 'Jobs Opportunities' || opli.OpportunityRecordType__c == 'Jobs Dubai' || opli.OpportunityRecordType__c == 'Jobs Amend')){
                    opli.addError('Please select Term.');   
                }*/
                if( opli.Start_Date__c == null && opli.Contract_start_date__c != null){
                    opli.Start_Date__c = opli.Contract_start_date__c;
                }
                if( opli.Start_Date__c != null && opli.Term__c != null){
                    opli.End_Date__c = opli.Start_Date__c.addMonths(Integer.valueOf(opli.Term__c)).addDays(-1);
                    System.debug('-------'+ opli.End_Date__c); 
                }
          
          }  
        }
    } 
    if((Trigger.isInsert || Trigger.isUpdate) && Trigger.isBefore){
    if(idSet != null && idSet.size()> 0){
        //Calculation for jobs Opportunities Product's End Date.
        map<id,Product2> ProductIdVSDecimalMap = new map<id,Product2>();    
        map<id,String> OpportunityIdVSPaymentTypeMap = new map<id,String>();    
        for(product2 pro : [SELECT id,Duration__c,Duration_in__c from product2 where id IN: idSet]){
            ProductIdVSDecimalMap.put(pro.id,pro);    
        }
        system.debug('**ProductIdVSDecimalMap**' +ProductIdVSDecimalMap);
        for(Opportunity opp : [SELECT id,payment_type__c FROM Opportunity WHERE id IN : OppidSet]){
            OpportunityIdVSPaymentTypeMap.put(opp.id,opp.Payment_Type__c);      
        }
        system.debug('**OpportunityIdVSPaymentTypeMap**' +OpportunityIdVSPaymentTypeMap);
        for(OpportunityLineItem opli: Trigger.New){
            system.debug(opli.start_date__c);
            system.debug('opli.opportunityid' +opli.opportunityid);
            system.debug('OpportunityIdVSPaymentTypeMap-PaymentType' +OpportunityIdVSPaymentTypeMap.get(opli.opportunityid));
            if(OpportunityIdVSPaymentTypeMap.get(opli.opportunityid) != 'Offline' && (opli.OpportunityRecordType__c == 'Jobs Opportunities' || opli.OpportunityRecordType__c == 'Job Amend') && opli.start_date__c != null && ProductIdVSDecimalMap.containsKey(opli.Product2Id) != null){
                if(ProductIdVSDecimalMap.get(opli.Product2Id).Duration_in__c == 'M'){
                    opli.End_date__c = opli.start_date__c.addMonths(ProductIdVSDecimalMap.get(opli.Product2Id).Duration__c.intValue()) - 1; 
                    system.debug('**opli.End_date__c**' +opli.End_date__c);  
                    String str = String.valueOf(ProductIdVSDecimalMap.get(opli.Product2Id).Duration__c);
                    opli.Term__c = str;
                    system.debug('**opli.Term__c**' +opli.Term__c); 
                }
                else if(ProductIdVSDecimalMap.get(opli.Product2Id).Duration_in__c == 'D'){
                    opli.End_date__c = opli.start_date__c.addDays(ProductIdVSDecimalMap.get(opli.Product2Id).Duration__c.intValue()); 
                    system.debug('**opli.End_date__c===**' +opli.End_date__c);   
                    opli.Term__c = '1';
                    system.debug('**opli.Term__c===**' +opli.Term__c); 
                }
                
            }      
        }    
    } 
}

    if(Trigger.isBefore && Trigger.isDelete ){
        for(OpportunityLineItem OLI:Trigger.Old){
                opportunityProduct.add(OLI.id);
         }   
        revenueList=[SELECT id FROM Revenue__c WHERE OpportunityLineItemID__c in :opportunityProduct];
        system.debug('revenueList-->'+revenueList.size());    
        if(revenueList.size()>0){
        delete revenueList;    
        }   
    }            
}
 
Hi,

I need help with building a custom lightning page to filter leads based on  follow up date, lead source, and lead date. The follow up date and lead date fields should be from and to date filters and lead source should be a drop down filter. 

Any help would be greatly appreciated.

Thanks
Finn
Hi,

I have found this Spring 20 release where we can create object based callendars and share them to public groups.

https://developer.salesforce.com/docs/atlas.en-us.224.0.object_reference.meta/object_reference/sforce_api_objects_calendarview.htm

There is also a code snippet at the bottom of the article but I am not sure on how to use it.

Group userGroup = [SELECT Id FROM Group WHERE Name = 'Sales Group' LIMIT 1 ]; List < Id > groupId = new List < Id > (); groupId.add(userGroup.id); List < GroupMember > groupMembers = [SELECT UserOrGroupId FROM GroupMember WHERE GroupId IN: groupId]; List < CalendarView > calendarViews = new List < CalendarView > (); for (GroupMember groupMember: groupMembers) { CalendarView calendarView = new CalendarView(name = 'Opportunity Close Dates', SobjectType = 'Opportunity', StartField = 'CloseDate', DisplayField = 'Name', OwnerId = groupMember.UserOrGroupId); calendarViews.add(calendarView); } insert calendarViews;



Can anyone please tell me on how to setup the CalendarView using code?
Hi,

I have a trigger than creates and edits an event when a custom object record name Calendar Event is created/edited.

I need help with writing a test class please. Any help will be appreciated.
 
trigger CreateEvent on Calender_Event__c (after insert,before update) {

    if(trigger.isInsert)
    {
        List<Event> eventList = new List<Event>();

        for(Calender_Event__c fac : Trigger.new)
        {
            if(fac.Name != null)
            {
                Event e = new Event();
                e.StartDateTime = fac.Start_Date_Time__c;
                e.EndDateTime = fac.End_Date_Time__c;
                e.Subject = fac.Subject__c;
                e.WhatId = fac.Id;

                eventList.add(e);
            }
        }

        insert eventList;
    }

    if(trigger.isUpdate)
    {
        List<Id> facIds = new List<Id>();

        Map<Id, Event> eventMap = new Map<Id, Event>();

        List<Event> eventList = new List<Event>();

        for(Integer i = 0; i < Trigger.new.size(); i++)
        {
            if(Trigger.new[i].Start_Date_Time__c <> Trigger.Old[i].Start_Date_Time__c)
            {
                facIds.add(Trigger.new[i].Id);
            }
        }
        
        for(Event ev : [SELECT Id, WhatId, StartDateTime, EndDateTime FROM Event WHERE WhatId IN :facIds])
        {
            eventMap.put(ev.WhatId, ev);
        }
        
        for(Calender_Event__c dfc : Trigger.new)
        {
            Event evnt = eventMap.get(dfc.Id);
            if(evnt <> null)
            {
                evnt.StartDateTime = dfc.Start_Date_Time__c;
                evnt.EndDateTime =dfc.End_Date_Time__c;

                eventList.add(evnt);
            }
        }

        update eventList;
    }
}

 
Hi,

I have found this Spring 20 release where we can create object based callendars and share them to public groups.

https://developer.salesforce.com/docs/atlas.en-us.224.0.object_reference.meta/object_reference/sforce_api_objects_calendarview.htm

There is also a code snippet at the bottom of the article but I am not sure on how to use it.

Group userGroup = [SELECT Id FROM Group WHERE Name = 'Sales Group' LIMIT 1 ]; List < Id > groupId = new List < Id > (); groupId.add(userGroup.id); List < GroupMember > groupMembers = [SELECT UserOrGroupId FROM GroupMember WHERE GroupId IN: groupId]; List < CalendarView > calendarViews = new List < CalendarView > (); for (GroupMember groupMember: groupMembers) { CalendarView calendarView = new CalendarView(name = 'Opportunity Close Dates', SobjectType = 'Opportunity', StartField = 'CloseDate', DisplayField = 'Name', OwnerId = groupMember.UserOrGroupId); calendarViews.add(calendarView); } insert calendarViews;



Can anyone please tell me on how to setup the CalendarView using code?
Hi,

I have a trigger than creates and edits an event when a custom object record name Calendar Event is created/edited.

I need help with writing a test class please. Any help will be appreciated.
 
trigger CreateEvent on Calender_Event__c (after insert,before update) {

    if(trigger.isInsert)
    {
        List<Event> eventList = new List<Event>();

        for(Calender_Event__c fac : Trigger.new)
        {
            if(fac.Name != null)
            {
                Event e = new Event();
                e.StartDateTime = fac.Start_Date_Time__c;
                e.EndDateTime = fac.End_Date_Time__c;
                e.Subject = fac.Subject__c;
                e.WhatId = fac.Id;

                eventList.add(e);
            }
        }

        insert eventList;
    }

    if(trigger.isUpdate)
    {
        List<Id> facIds = new List<Id>();

        Map<Id, Event> eventMap = new Map<Id, Event>();

        List<Event> eventList = new List<Event>();

        for(Integer i = 0; i < Trigger.new.size(); i++)
        {
            if(Trigger.new[i].Start_Date_Time__c <> Trigger.Old[i].Start_Date_Time__c)
            {
                facIds.add(Trigger.new[i].Id);
            }
        }
        
        for(Event ev : [SELECT Id, WhatId, StartDateTime, EndDateTime FROM Event WHERE WhatId IN :facIds])
        {
            eventMap.put(ev.WhatId, ev);
        }
        
        for(Calender_Event__c dfc : Trigger.new)
        {
            Event evnt = eventMap.get(dfc.Id);
            if(evnt <> null)
            {
                evnt.StartDateTime = dfc.Start_Date_Time__c;
                evnt.EndDateTime =dfc.End_Date_Time__c;

                eventList.add(evnt);
            }
        }

        update eventList;
    }
}

 

hi i write trigger  on my custom object when it will update first time i have to create event ,if i update next time its only update

i write trigger it will not updating its only creating can any one tel me where am doing mistak.

 

my trigger scenario is i have a filed called sales ready date in my custom object if i change tht date my event details will be updated .

below is my trigger

 

trigger TGR_EventDateOppName on Factory__c (after insert,before update) {

try{
Factory__c dgital=[select id,Sales_Ready_Date__c from Factory__c where id=:trigger.new ];
list<Event> newevent=new list<Event>();
Opportunity opp=[select id,name from Opportunity where id in (select Opportunity__c from factory__c where id=:trigger.new)];
system.debug('&&&'+opp);
for(factory__c df:trigger.new)
{


if(df.Sales_Ready_Date__c !=null)
{
Event e = new Event();
    e.StartDateTime =df.Sales_Ready_Date__c;
    e.EndDateTime =df.Sales_Ready_Date__c.addDays(1);
    e.Subject =opp.name;
    e.WhatId=df.id;
    newevent.add(e);
}

}
insert newevent;
Event e=[select id,StartDateTime,EndDateTime  from event where WhatId in(select Opportunity__c from factory__c where id=:trigger.new)];
system.debug('&&&'+e);
for(factory__c dfc:trigger.new)
{
    e.StartDateTime =dfc.Sales_Ready_Date__c;
    e.EndDateTime =dfc.Sales_Ready_Date__c.addDays(1);
 
}

update newevent;
system.debug('^^^^^^^^^^^^'+newevent);
}catch(Exception e){system.debug('&*&*'+e);}
}