Apex Specialist|| Automate record creation|| Challenge 1 AND 2

Поделиться
HTML-код
  • Опубликовано: 2 дек 2024

Комментарии • 16

  • @practical_deen
    @practical_deen Год назад +8

    Didn't work for me. I got 4 errors in MaintenanceRequestHelper class. The first one is "Invalid type: Equipment_Maintenance_Item_c".

  • @MeghanaSripada-d6c
    @MeghanaSripada-d6c 10 месяцев назад +3

    Challenge Not yet complete... here's what's wrong:
    Closing a Maintenance Request of type 'Routine Maintenance' did not create of a new Maintenance Request related to the same Vehicle. Someone please help :::(

  • @trailheadchallenge
    @trailheadchallenge  Год назад +6

    MaintenanceRequestHelper
    public with sharing class MaintenanceRequestHelper {
    public static void updateWorkOrders() {
    List newCaseList = new List();
    Integer avgAmount=10000;

    List newEMI = new List();
    List caseList = [SELECT id,Vehicle__c,Subject,ProductID,Product__c, (SELECT id from Equipment_Maintenance_Items__r) from case where status='closed' and Type IN ('Repair', 'Routine Maintenance') and ID IN :Trigger.new LIMIT 200];
    Map equip = new map([Select ID, Equipment__c, Quantity__c,Equipment__r.id,Equipment__r.Maintenance_Cycle__c from Equipment_Maintenance_Item__c ]);
    for(case c: caseList){
    case newCase = new Case();
    newCase.Type = 'Routine Maintenance';
    newCase.Status = 'New';
    newCase.Vehicle__c = c.Vehicle__c;
    newCase.Subject = String.isBlank(c.Subject) ? 'Routine Maintenance Request' : c.Subject;
    newCase.Date_Reported__c = Date.today();
    newCase.ProductId = c.ProductId;
    newCase.Product__c = c.Product__c;
    newCase.parentID = c.Id;


    for(Equipment_Maintenance_Item__c emi : c.Equipment_Maintenance_Items__r ){
    avgAmount = Math.min(avgAmount,Integer.valueOf(equip.get(emi.id).Equipment__r.Maintenance_Cycle__c));
    newEMI.add(new Equipment_Maintenance_Item__c(
    Equipment__c = equip.get(emi.id).Equipment__c,
    Maintenance_Request__c = c.id,
    Quantity__c = equip.get(emi.id).Quantity__c));
    }
    Date dueDate = date.TODAY().adddays(avgAmount);
    newCase.Date_Due__c =dueDate;
    newCaseList.add(newCase);

    }
    if(newCaseList.size()>0){
    Database.insert(newCaseList);
    }

    for(Case c2: newCaseList){
    for(Equipment_Maintenance_Item__c emi2 : newEmi){
    if(c2.parentID == emi2.Maintenance_Request__c){
    emi2.Maintenance_Request__c = c2.id;
    }
    }
    }

    if(newEmi.size()>0){
    Database.insert(newEmi);
    }
    }
    }

    • @AshishDesai23
      @AshishDesai23 11 месяцев назад +3

      Corrected Code
      USE THIS
      public with sharing class MaintenanceRequestHelper {
      public static void updateWorkOrders() {
      List newCaseList = new List();
      Integer avgAmount=10000;

      List newEMI = new List();
      List caseList = [SELECT id,Vehicle__c,Subject,ProductID,Product__c, (SELECT id from Equipment_Maintenance_Items__r) from case where status='closed' and Type IN ('Repair', 'Routine Maintenance') and ID IN :Trigger.new LIMIT 200];
      Map equip = new map([Select ID, Equipment__c, Quantity__c,Equipment__r.id,Equipment__r.Maintenance_Cycle__c from Equipment_Maintenance_Item__c ]);
      for(case c: caseList){
      case newCase = new Case();
      newCase.Type = 'Routine Maintenance';
      newCase.Status = 'New';
      newCase.Vehicle__c = c.Vehicle__c;
      newCase.Subject = String.isBlank(c.Subject) ? 'Routine Maintenance Request' : c.Subject;
      newCase.Date_Reported__c = Date.today();
      newCase.ProductId = c.ProductId;
      newCase.Product__c = c.Product__c;
      newCase.parentID = c.Id;


      for(Equipment_Maintenance_Item__c emi : c.Equipment_Maintenance_Items__r ){
      avgAmount = Math.min(avgAmount,Integer.valueOf(equip.get(emi.id).Equipment__r.Maintenance_Cycle__c));
      newEMI.add(new Equipment_Maintenance_Item__c(
      Equipment__c = equip.get(emi.id).Equipment__c,
      Maintenance_Request__c = c.id,
      Quantity__c = equip.get(emi.id).Quantity__c));
      }
      Date dueDate = date.TODAY().adddays(avgAmount);
      newCase.Date_Due__c =dueDate;
      newCaseList.add(newCase);

      }
      if(newCaseList.size()>0){
      Database.insert(newCaseList);
      }

      for(Case c2: newCaseList){
      for(Equipment_Maintenance_Item__c emi2 : newEmi){
      if(c2.parentID == emi2.Maintenance_Request__c){
      emi2.Maintenance_Request__c = c2.id;
      }
      }
      }

      if(newEmi.size()>0){
      Database.insert(newEmi);
      }
      }
      }

    • @surajrathaur692
      @surajrathaur692 4 месяца назад

      Plz send me the code

  • @MeghanaSripada-d6c
    @MeghanaSripada-d6c 10 месяцев назад

    Method does not exist or incorrect signature: void updateWorkOrders() from the type MaintenanceRequestHelper ---> code error

  • @greentech7518
    @greentech7518 Год назад +2

    Please Also Do Service Cloud Specialist Superbadge Bro

  • @venkateshkodali3231
    @venkateshkodali3231 Год назад +1

    Challenge Not yet complete... here's what's wrong:
    The Case object has not been renamed to 'Maintenance Request'.

  • @Lucifer-jl9vh
    @Lucifer-jl9vh 5 месяцев назад

    Thank you bro

  • @trailheadchallenge
    @trailheadchallenge  Год назад +1

    MaintenanceRequest Trigger
    trigger MaintenanceRequest on Case (before update, after update) {
    //ToDo: Call MaintenanceRequestHelper.updateWorkOrders
    if(trigger.isAfter){
    MaintenanceRequestHelper.updateWorkOrders();
    }
    }

  • @Najica
    @Najica Год назад +2

    Thank you for all your videos. Here is what worked for me on this challenge.
    public with sharing class MaintenanceRequestHelper {
    public static void updateWorkOrders(List existingMainReq) {
    // TODO: Complete the method to update workorders
    Integer count = 0;
    Map toGetDueDateMap = new Map();
    Map newCasesToIdsMap = new Map();
    List createNewMainReq = new List();
    List caseIdsList = new List();
    Map EquipMainItemsToProdIds = new Map();
    if(!existingMainReq.isEmpty()){
    for(Case cc : existingMainReq){
    if((cc.Type == 'Repair' || cc.Type == 'Routine Maintenance') && cc.Status == 'Closed'){
    caseIdsList.add(cc);
    }
    }
    }
    List equipMainList = [Select id,Equipment__c,Maintenance_Request__c from Equipment_Maintenance_Item__c where Maintenance_Request__c IN : caseIdsList];
    if(!equipMainList.isEmpty()){
    for(Equipment_Maintenance_Item__c equipMainn : equipMainList){
    EquipMainItemsToProdIds.put(equipMainn,equipMainn.Equipment__c);
    system.debug(EquipMainItemsToProdIds.size());
    if(EquipMainItemsToProdIds.size() > 1){
    count = EquipMainItemsToProdIds.size();
    }
    }
    }
    List EMIList = [Select Equipment__r.Maintenance_Cycle__c,Equipment__c from Equipment_Maintenance_Item__c where Equipment__r.Id IN :EquipMainItemsToProdIds.values() AND Maintenance_Request__c IN:caseIdsList order By Equipment__r.Maintenance_Cycle__c ASC limit 1];
    for(Equipment_Maintenance_Item__c equip : EMIList){
    toGetDueDateMap.put(equip.Id,Integer.valueOf(equip.Equipment__r.Maintenance_Cycle__c));
    for(Case c : caseIdsList){
    Case mainRe = new Case();
    mainRe.Vehicle__c = c.Vehicle__c;
    mainRe.status = 'New';
    mainRe.Type = 'Routine Maintenance';
    mainRe.Subject = 'New Main Request For Vehicle for Apex Specialist';
    mainRe.Date_Reported__c = date.today();
    if(count > 1){
    mainRe.Date_Due__c = Date.today().addDays(toGetDueDateMap.get(equip.Id));
    }
    else{
    mainRe.Date_Due__c = Date.today();
    }
    createNewMainReq.add(mainRe);
    newCasesToIdsMap.put(c.Id,mainRe);
    }
    insert createNewMainReq;
    if(caseIdsList.size()>0 && newCasesToIdsMap.size()>0){
    cloneEquipItems(caseIdsList,newCasesToIdsMap);
    }
    }
    }
    public static void cloneEquipItems(List closedCaseRecords, Map newCreatedCasesMapp){
    List newEquipItems = new List();
    try{
    if(!closedCaseRecords.isEmpty() && newCreatedCasesMapp.size()>0){
    List oldEquipItems = [SELECT Equipment__c, Maintenance_Request__c, Quantity__c,Maintenance_Request__r.Id
    FROM Equipment_Maintenance_Item__c
    WHERE Maintenance_Request__c IN :closedCaseRecords];
    for(Equipment_Maintenance_Item__c equipItem : oldEquipItems) {
    Equipment_Maintenance_Item__c newItem = equipItem.clone(false, true, false, false);
    newItem.Maintenance_Request__c = newCreatedCasesMapp.get(equipItem.Maintenance_Request__c).id;
    newEquipItems.add(newItem);
    }
    insert newEquipItems;
    }
    }
    catch(Exception e){
    System.debug('Exception is'+ e);
    }
    }
    }
    Apex Trigger:
    trigger MaintenanceRequest on Case (before update, after update) {
    // ToDo: Call MaintenanceRequestHelper.updateWorkOrders
    if(Trigger.isAfter && Trigger.isUpdate){
    MaintenanceRequestHelper.updateWorkOrders(Trigger.new);
    }
    }

  • @chandanbabu5192
    @chandanbabu5192 Год назад

    Dont try it!

  • @chandanbabu5192
    @chandanbabu5192 Год назад

    Dont not working full waste