Automate record creation || Apex Specialist

Поделиться
HTML-код
  • Опубликовано: 13 дек 2024
  • Thanks for watching
    Subscribe for more videos
    Turn on notification for latest updates 🔔

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

  • @VISHALKUMAR-qy3mb
    @VISHALKUMAR-qy3mb 11 месяцев назад

    Bro how to find " How to roll Maintaince"

  • @Solo-Tech-World10001
    @Solo-Tech-World10001  Год назад

    MaintenanceRequestHelper :
    public with sharing class MaintenanceRequestHelper {
    public static void updateworkOrders(List updWorkOrders, Map nonUpdCaseMap) {
    Set validIds = new Set();


    For (Case c : updWorkOrders){
    if (nonUpdCaseMap.get(c.Id).Status != 'Closed' && c.Status == 'Closed'){
    if (c.Type == 'Repair' || c.Type == 'Routine Maintenance'){
    validIds.add(c.Id);


    }
    }
    }

    if (!validIds.isEmpty()){
    List newCases = new List();
    Map closedCasesM = new Map([SELECT Id, Vehicle__c, Equipment__c, Equipment__r.Maintenance_Cycle__c,(SELECT Id,Equipment__c,Quantity__c FROM Equipment_Maintenance_Items__r)
    FROM Case WHERE Id IN :validIds]);
    Map maintenanceCycles = new Map();
    AggregateResult[] results = [SELECT Maintenance_Request__c, MIN(Equipment__r.Maintenance_Cycle__c)cycle FROM Equipment_Maintenance_Item__c WHERE Maintenance_Request__c IN :ValidIds GROUP BY Maintenance_Request__c];

    for (AggregateResult ar : results){
    maintenanceCycles.put((Id) ar.get('Maintenance_Request__c'), (Decimal) ar.get('cycle'));
    }

    for(Case cc : closedCasesM.values()){
    Case nc = new Case (
    ParentId = cc.Id
    ,
    Status = 'New',
    Subject = 'Routine Maintenance',
    Type = 'Routine Maintenance',
    Vehicle__c = cc.Vehicle__c,
    Equipment__c =cc.Equipment__c,
    Origin = 'Web',
    Date_Reported__c = Date.Today()

    );

    If (maintenanceCycles.containskey(cc.Id)){
    nc.Date_Due__c = Date.today().addDays((Integer) maintenanceCycles.get(cc.Id));
    } else {
    nc.Date_Due__c = Date.today().addDays((Integer) cc.Equipment__r.maintenance_Cycle__c);
    }

    newCases.add(nc);
    }

    insert newCases;

    List clonedWPs = new List();
    for (Case nc : newCases){
    for (Equipment_Maintenance_Item__c wp : closedCasesM.get(nc.ParentId).Equipment_Maintenance_Items__r){
    Equipment_Maintenance_Item__c wpClone = wp.clone();
    wpClone.Maintenance_Request__c = nc.Id;
    ClonedWPs.add(wpClone);

    }
    }
    insert ClonedWPs;
    }
    }
    }
    2.MaitenanceRequest:
    trigger MaintenanceRequest on Case (before update, after update) {
    if(Trigger.isUpdate && Trigger.isAfter){
    MaintenanceRequestHelper.updateWorkOrders(Trigger.New
    , Trigger.OldMap);
    }
    }