function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sree valli 12Sree valli 12 

I want help with this Inserting a new Maintenance Request of type 'Routine Maintenance' and then closing it did not create of a new Maintenance Request based upon the original record correctly.

Below is my code

public class MaintenanceRequestHelper {

     

    public static void updateWorkOrders(List<case>ClosedCaseList){

        // update workorders

         

        list<case> insertCaseList = new list<case>();

         

        for(Case c : ClosedCaseList)

             

            {

                Case newCase = new Case();

                newCase.Type = 'Routine Maintenance';

                newCase.Status = 'New';

                newCase.Vehicle__c = c.Vehicle__c;

                newCase.Subject =  c.Subject;

                newCase.Date_Reported__c = Date.today();

                newCase.Date_Due__c = Date.today();

                newCase.Equipment__c = c.Equipment__c;

                insertCaseList.add(newCase);

            }

         

        if(insertCaseList.size()>0){

        insert insertCaseList;

        }

    }

         

}




Trigger

trigger MaintenanceRequest on Case (before update, after update) {

     

     List<case>ClosedCaseList = [SELECT Id, subject, Vehicle__c, vehicle__r.Name, equipment__c, type FROM Case WHERE status = 'closed'];

     

     for(case c : Trigger.New){

         

        if(c.type == 'Repair' || c.type =='Routine Maintenance'){

             

            MaintenanceRequestHelper.updateWorkOrders(ClosedCaseList);

        }

    }

}
 
Amit Chaudhary 8Amit Chaudhary 8
Update your trigger like below
trigger MaintenanceRequest on Case (before update) 
{
	List<Case> lstOfCasses = new List<Case>();
    for(case c : Trigger.New)
	{ 
        if(c.type == 'Repair' || c.type =='Routine Maintenance') // status == 'closed'
		{
			lstOfCasses.add(c);
        }
    }

	if(lstOfCasses.size() > 0 ){
		MaintenanceRequestHelper.updateWorkOrders(lstOfCasses);
	}
	
}

Let us know if this will help you
 
Sree valli 12Sree valli 12
Hi Amith

Thamks for your reply
But still am facing the same issue. Can you explain me clearly  what to do after renaming the standard objects. Iam thinking that after renaming i am doing mistake.So can you tell me the steps which you followed
Amit Chaudhary 8Amit Chaudhary 8
In trigger i just removed extra query and user updated record only
Can you please let us know your requirement ?
 
Sree valli 12Sree valli 12

Sure Amit .Please find the requirement

Automate record creation
Install the unmanaged package for the schema and stubs for Apex classes and triggers. Rename cases and products to match the HowWeRoll schema, and assign all profiles to the custom HowWeRoll page layouts for those objects. Use the included package content to automatically create a Routine Maintenance request every time a maintenance request of type Repair or Routine Maintenance is updated to Closed. Follow the specifications and naming conventions outlined in the business requirements

Amit Chaudhary 8Amit Chaudhary 8
It look like when type Repair or Routine Maintenance is updated to Closed then you need to create new case record ?

Then try to update your code like below
trigger MaintenanceRequest on Case (After update) 
{
	List<Case> lstOfCasses = new List<Case>();
    for(case c : Trigger.New)
	{ 
        if( (c.type == 'Repair' || c.type =='Routine Maintenance') && status =='Closed' ) 
		{
			lstOfCasses.add(c);
        }
    }

	if(lstOfCasses.size() > 0 ){
		MaintenanceRequestHelper.updateWorkOrders(lstOfCasses);
	}	
}

Let us know if this will work
 
Sree valli 12Sree valli 12
Even this also giving same error
And in maintenanceRequest class am facing some errors



public class MaintenanceRequestHelper {

public static void updateWorkOrders(List<case>  ListOfCases){
    List<Product2> equipments = [select id,Maintenance_Cycle__c from product2];
    map<id,Decimal> productmap = new Map<id,Decimal>();
    for(product2 p : equipments){
        productmap.put(p.Id, p.Maintenance_Cycle__c);
    }   
    List<Case> newCasesToInsert = new List<case>();       
    for(case oldCase : ListOfCases){
        Case newCase = new Case();
        if(oldCase.Status == 'Closed' && (oldCase.Type =='Repair' || oldCase.Type == 'Routine Maintenance')){
            system.debug('Status Closed');
            newCase.Equipment__c =   oldcase.Equipment__c;
            newCase.Type ='Routine Maintenance';
            newCase.Status =  'New';
            newCase.Vehicle__c =  oldcase.Vehicle__c;
            newCase.Date_Reported__c =  system.today();

            newCase.Subject = 'Check Up';
            newCasesToInsert.add(newCase);
        }
    }
    insert newCasesToInsert;
}        
}


ERROR at Row:1:Column:11
No such column 'Maintenance_Cycle__c' on entity 'Product2'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.
Variable does not exist: Equipment__c

please assist on this issue
Amit Chaudhary 8Amit Chaudhary 8
Please check Maintenance_Cycle__c field is available on Product 2 object.

If below code is not in use then please remove same
List<Product2> equipments = [select id,Maintenance_Cycle__c from product2];
    map<id,Decimal> productmap = new Map<id,Decimal>();
    for(product2 p : equipments){
        productmap.put(p.Id, p.Maintenance_Cycle__c);
    }

 
Sree valli 12Sree valli 12
No its on use only .It should be useful in trigger
 
Amit Chaudhary 8Amit Chaudhary 8
Please check Maintenance_Cycle__c field is available on Product 2 object ?
Sree valli 12Sree valli 12
yes its there