• Jay Parikh 38
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
Hello Developers,

I have a requirement when all incident (Incident is a custom object) related task closed to close incident automatically.

I have written below trigger for the single task when the task is closed incident close automatically.

Note: Incident and Task both are the Custom object

trigger ClostTaskRelatedIncident on BMCServiceDesk__Task__c (after  update) {
    List<BMCServiceDesk__Task__c> cse = Trigger.new;

    Set<ID> cseIds = new Set<ID>();
    System.debug('------------------->'+cseIds);
    for (BMCServiceDesk__Task__c t:cse)
    {  
      System.debug('Before If condition of t.BMCServiceDesk__Status_ID__c');
      if (t.BMCServiceDesk__FKStatus__c=='a3w0w0000000AslAAE')
      {
         cseIds.add(t.BMCServiceDesk__FKIncident__c);
      }
      System.debug('After Case IDs'+cseIds);
      for (BMCServiceDesk__Incident__c c : [select id,BMCServiceDesk__incidentDescription__c from BMCServiceDesk__Incident__c where id in :cseIds])
      {
          if(c.BMCServiceDesk__incidentDescription__c!='')
          {     
              c.BMCServiceDesk__FKStatus__c='a3w0w0000000AslAAE';
              c.BMCServiceDesk__incidentDescription__c='Trigger Fire';
              update c;
              
          }    
      }
    }

}
Regards,
SFDC16
  • April 01, 2019
  • Like
  • 0
Hi Friends,

I had done with my scheduler and test class but I got stuck in code coverage issue and could not able to deploy into production.

Kindly need your help to complete the code.

Class :
global class orderItemsSchedulable Implements Schedulable {

    global void execute(SchedulableContext sc) {
        insertOrder();
    }

    public void insertOrder() {
    
        List<OrderItems__c> orderitemsList = new List<OrderItems__c>();
        
        orderitemsList = [SELECT Id, Name, Item_Id__c, Due_Date__c, Name__c, Discount_Amount__c, Price__c, Product_Id__c, Product_Type__c, Qty_Backordered__c, 
                                 Qty_Canceled__c, Qty_Invoiced__c, Qty_Ordered__c, Qty_Refunded__c, Qty_Returned__c, Qty_Shipped__c, Sku__c
                            FROM OrderItems__c WHERE CreatedDate = TODAY AND HOUR_IN_DAY(CreatedDate) > 1];
        
        System.debug('--- Order Items List Created ---' + orderitemsList.size());
        
        Set<String> orderIds = new Set<String>();
        List<Opportunitylineitem> oliinsertList = new List<Opportunitylineitem>();
        
        for(OrderItems__c oi : orderitemsList) {
            orderIds.add(oi.Name);
            orderIds.add(oi.Name__c);
            System.debug('--- OrderId Name: ---' + oi.Name);
            System.debug('--- OrderId Name: ---' + oi.Name__c);        
        }
        
        Map<String, List<Opportunitylineitem>> oliMap = new Map<String, List<Opportunitylineitem>>();
    
        Map<String, List<Opportunity>> oppMap = new Map<String, List<Opportunity>>();
        
        List<Opportunity> oppsList = new List<Opportunity>();
        oppsList = [SELECT Id, Name, (SELECT Id FROM Opportunitylineitems) FROM Opportunity WHERE Name IN: orderIds];
        
        if(!orderIds.isEmpty()) {
            for(Opportunity opps : oppsList) {
                if(!oppMap.containsKey(opps.Name)){
                    oppMap.put(opps.Name, new List<Opportunity> {opps});
                    System.debug('--- Inside OppMap ---' + oppMap);
                }
                else{
                    List<Opportunity> oppList = oppMap.get(opps.Name);
                    oppList.add(opps);
                    oppMap.put(opps.Name, oppList);
                    System.debug('--- Else oppMap ---' + oppList);
                }
            }
        }
        
        Pricebookentry pbe = [SELECT Id, Name, isActive FROM Pricebookentry WHERE isActive = true AND Name = 'Sample Product' Limit 1];
            
        for(OrderItems__c oi : orderitemsList) {
             if(oppMap.containsKey(oi.Name)) {
                for(Opportunity opp : oppMap.get(oi.Name)) {                    
                    if(opp.opportunitylineitems.size() == 0) {
                        Opportunitylineitem oli = new Opportunitylineitem ();
                        oli.OpportunityId = opp.Id;
                        oli.PricebookEntryId = pbe.Id;
                        oli.Quantity = 1;
                        oli.TotalPrice = oi.Price__c;
                        oli.item_id__c = oi.item_id__c;
                        oli.Name__c = oi.Name__c;
                        oli.product_id__c = oi.product_id__c;
                        oli.Due_Date__c = oi.Due_Date__c;
                        oli.product_type__c = oi.product_type__c;
                        oli.qty_backordered__c = oi.qty_backordered__c;
                        oli.qty_canceled__c = oi.qty_canceled__c;
                        oli.qty_invoiced__c = oi.qty_invoiced__c;
                        oli.qty_ordered__c = oi.qty_ordered__c;
                        oli.qty_refunded__c = oi.qty_refunded__c;
                        oli.qty_returned__c = oi.qty_returned__c;
                        oli.qty_shipped__c = oi.qty_shipped__c;
                        oli.Discount_Amount__c = oi.Discount_Amount__c;
                        oli.Sku__c = oi.Sku__c;
                        oliinsertList.add(oli);
                    }
                }
            }
        }
        
        if(oliinsertList.size() > 0) {
            insert oliinsertList;
        }
    }
}

test class :

@isTest(SeeAllData=true)
public class orderItemsSchedulableTest {
    
    Static testmethod void schedulerTest() {    
        
        String CRON_EXP = '0 05 * * * ?';
        
        Id RecordTypeIdAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
  
        Account acc = new Account(LastName = 'Test Account', recordtypeid = RecordTypeIdAccount);
        insert acc;
        
        Id pricebookId = Test.getStandardPricebookId();
            
        List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry WHERE PriceBook2.isStandard = true LIMIT 1];
                  
        //Create your product
        Product2 prod = new Product2(
             Name = 'Product X',
             ProductCode = 'Pro-X',
             isActive = true
        );
        insert prod;
        
        //Create your pricebook entry
        PricebookEntry pbEntry = new PricebookEntry(
             Pricebook2Id = pricebookId,
             Product2Id = prod.Id,
             UnitPrice = 100.00,
             IsActive = true
        );
        insert pbEntry;
        
        Opportunity opp = [SELECT Id, Name FROM Opportunity WHERE Name ='Sample']; 
            
        OpportunityLineItem oli = new OpportunityLineItem();                
        oli.OpportunityId = opp.Id;
        oli.PricebookEntryId = pbEntry.Id; 
        oli.Quantity = 1;
        oli.TotalPrice = 100;                
        oli.item_id__c = 123;
        oli.Name__c = 'Testing';
        oli.product_id__c = '1254';
        oli.Due_Date__c = System.today();
        oli.product_type__c = '32675';
        oli.qty_backordered__c = 0;
        oli.qty_canceled__c = 0;
        oli.qty_invoiced__c = 2;
        oli.qty_ordered__c = 1;
        oli.qty_refunded__c = 0;
        oli.qty_returned__c = 0;
        oli.qty_shipped__c = 2;
        oli.Sku__c = 'BUYCDF786';
        insert oli;
        
        Test.startTest();
            String jobId = system.schedule('ScheduleJobTest', CRON_EXP, new orderItemsSchedulable());
            CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id=: jobId];
            System.assertEquals(CRON_EXP, ct.CronExpression);
            System.assertEquals(0, ct.TimesTriggered);                        
        Test.stopTest();
    }
}

Thanks.