• Iswarya Sekar 9
  • NEWBIE
  • -1 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 6
    Questions
  • 17
    Replies
@isTest
public class MaintenanceRequestTest {
	@isTest
    static void testMaintenanceRequest(){
        
        List<Case> caseList = new List<Case>();

        Product2 prod = new Product2();
        prod.Cost__c = 50;
        prod.Name = 'Ball Valve 10 cm';
        prod.Lifespan_Months__c = 12;
        prod.Maintenance_Cycle__c = 365;
        prod.Current_Inventory__c = 50;
        prod.Replacement_Part__c = true;
        prod.Warehouse_SKU__c = '100009';
        insert prod;
        System.assertEquals(1, [SELECT count() FROM Product2 WHERE Name = 'Ball Valve 10 cm']);
            
         for(Integer i=1;i<=200;i++) {
            Case caseNew = new Case();
            caseNew.Subject = 'Maintenance';
            caseNew.Type = 'Other';
            caseNew.Status = 'New';
            caseNew.Equipment__c = caseNew.Id;
            caseList.add(caseNew);   
        }
        
        Test.startTest();
        
        insert caseList;
        System.assertEquals(200, [SELECT count() FROM Case WHERE Type = 'Other']);
        
        for(Case a : caseList){
            a.Type = 'Repair';
            a.Status = 'Closed';
        }
		update caseList;
        System.assertEquals(200, [SELECT count() FROM Case WHERE Type = 'Repair']);
        
        Test.stopTest();
        
    }
}




public class MaintenanceRequestHelper {
    
    public static void updateWorkOrders(Map<Id, Case> applicableCases){

	    Map<Id, Integer> mapProduct = new Map<Id, Integer>(); 
   		List<Case> newCases = new List<Case>();
        
        List<Product2> listProduct = [select Id, Maintenance_Cycle__c from Product2];       							
		for (Product2 p : listProduct) {
            if (p != null) {
                if(p.Maintenance_Cycle__c != null){
                    mapProduct.put(p.Id, Integer.valueOf(p.Maintenance_Cycle__c));
                }               
            }
        }

        for(Case a: applicableCases.values()){
            Case newCase = new Case();
            newCase.Vehicle__c = a.Vehicle__c;
            newCase.Equipment__c = a.Equipment__c;
            newCase.Type = 'Routine Maintenance';
            newCase.Subject = String.isBlank(a.Subject) ? 'Routine Maintenance Request' : a.Subject;
            newCase.Date_Reported__c = Date.today();
            newCase.Status = 'New';
            newCase.Product__c = a.Product__c;
            newCase.AccountId = a.AccountId;
            newCase.ContactId = a.ContactId;
            newCase.AssetId = a.AssetId;
            newCase.Origin = a.Origin;
            newCase.Reason = a.Reason;
            newCase.Date_Due__c =  (mapProduct.get(a.Id) != null) ? (Date.today().addDays(Integer.valueOf(mapProduct.get(a.Id)))) : (Date.today());
            newCases.add(newCase);
        }
        if(newCases.size() > 0){
            insert newCases;
        }
		        
        
    }        
    
}




trigger MaintenanceRequest on Case (before update, after update) {
    // call MaintenanceRequestHelper.updateWorkOrders  
    // 
    Map<Id,Case> applicableCases = new Map<Id,Case>();
    
    if(Trigger.isUpdate  && Trigger.isAfter){
        for(Case oCase: Trigger.new){
            if (oCase.IsClosed && (oCase.Type.equals('Repair') || oCase.Type.equals('Routine Maintenance'))){
                applicableCases.put(oCase.Id, oCase);
            }
        }
        if(!applicableCases.values().isEmpty()){
        	MaintenanceRequestHelper.updateWorkOrders(applicableCases);    
        }        
    } 
}

I got 100% code covergae. but i couldn't clear this challenge. the following is the error im getting when i check the challenge.
Challenge Not yet complete... here's what's wrong: 
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Case: id value of incorrect type: 01t7F000003BjDmQAK: [Equipment__c].   

Can anyone please help me to fix this issue?
trigger Giveendclientname on Lead (before insert, before update) {
    List<ID> OppIds = New List<ID>();
    for(lead ld : Trigger.new){
        if(ld.Customer_Type__c == 'partner'){
            OppIds.add(ld.Customer_Type__c);
        }
        List<Opportunity> oppList = [SELECT id, EndClientName__c FROM Opportunity WHERE id in :OppIds];
    //    for(Lead opp : trigger.new){
            if(oppList.EndClientName__c == ''){
                //  oppList[i].status = 'Approved';
                oppList.addError('End client name should be given if it is a partner Account');
            }
           // oppList.add(opp);
            update oppList;
      //  }
    }
}

The requirement is, when the customer type in lead is partner, then the endclient name in opportunity should not be blank. it should throw an error.
Challenge Not yet complete... here's what's wrong: 
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Case: id value of incorrect type: 01t7F000003BFZeQAO: [Equipment__c]
public class countOpenTaskOnOpportunites{
public static  boolean firstRun = true;
public static date nextDate;
List<opportunity> updateOpps=new List<opportunity>();

public static void method1(List<Task> taskListNew,List<Task> taskListOld){
Set<Id> oppIds=new Set<id>();
Set<Id> taskNewIDS=new Set<Id>();
List<opportunity> oppUpdate=new List<opportunity>();
List<Task> updateTaskList=new List<Task>();
for(Task t:taskListNew){
if(((string)t.whatId).startsWith('006')){
oppIds.add(t.whatId);
taskNewIDS.add(t.id);
}
}

system.debug('the opportunity list is'+oppIds);
List<Task> allTasksList=[select id,whatid,status from task where id NOT in:taskNewIDS AND whatid in:oppIds ];
system.debug('the task new list is'+allTasksList);
Map<Id,List<Task>> mapOppAndItsRelatedTask=new Map<Id,List<Task>>();
for(Task tsk:allTasksList){
if(!taskNewIDS.contains(tsk.id)){
        if(!mapOppAndItsRelatedTask.containsKey(tsk.whatid))
            mapOppAndItsRelatedTask.put(tsk.whatid, new list<task>{tsk});
            else
          mapOppAndItsRelatedTask.get(tsk.whatId).add(tsk);
          }

}

system.debug('the opp and task map is'+mapOppAndItsRelatedTask);
for(ID optyId:mapOppAndItsRelatedTask.keySet()){
for(Task eachTask:mapOppAndItsRelatedTask.get(optyId)){
system.debug('inside for');
if(eachTask.status!='Completed'){
system.debug('inside for2');
eachTask.status='Completed';
nextDate=eachTask.ActivityDate;
updateTaskList.add(eachTask);
}

}
}
update updateTaskList;

}


}

 
When an opportunity goes into a certain stage, we would like tasks representing the “next steps” to be automatically generated. Requirements include:
  1. Tasks should only be auto-generated for records with a record type of National Account
  2. All tasks should be assigned to the opportunity owner
  3. If the opportunity is moved to the next stage while there are incomplete auto-generated tasks, those open tasks associated with the prior stage should be closed, with a completed date reflecting the same date that the opportunity stage changed.
    1. Note that tasks that have been manually created by the user should be kept open.
The tasks to be auto-generated for each stage are as follows:
StageTasks
ProspectingTask Name: Complete Sales Information
Due Date: 2 Business Days from Stage Start
Idea GenerationTask Name: Product Sourcing
Due Date: 3 Business Days from Stage Start
 
Task Name: Send Presentation
Due Date: 5 Business Daye from Stage Start
 
Task Name: Idea Presentation Follow Up
Due Date: 8 Business Days from Stage Start
Sample RequestTask Name: Order Samples
Due Date: 2 Business Days from Stage Start
 
Task Name: Sample Follow Up
Due Date: 7 Business Days from Stage Start
Proposal / Price QuoteSend Quote – 5 Business Days from Stage Start
Quote Follow Up – 7 Business Days from Stage Start
Negotiation / ReviewN/A
Closed WonN/A
Closed LostN/A
 

 
 
trigger Giveendclientname on Lead (before insert, before update) {
    List<ID> OppIds = New List<ID>();
    for(lead ld : Trigger.new){
        if(ld.Customer_Type__c == 'partner'){
            OppIds.add(ld.Customer_Type__c);
        }
        List<Opportunity> oppList = [SELECT id, EndClientName__c FROM Opportunity WHERE id in :OppIds];
    //    for(Lead opp : trigger.new){
            if(oppList.EndClientName__c == ''){
                //  oppList[i].status = 'Approved';
                oppList.addError('End client name should be given if it is a partner Account');
            }
           // oppList.add(opp);
            update oppList;
      //  }
    }
}

The requirement is, when the customer type in lead is partner, then the endclient name in opportunity should not be blank. it should throw an error.
Challenge Not yet complete... here's what's wrong: 
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Case: id value of incorrect type: 01t7F000003BFZeQAO: [Equipment__c]
When an opportunity goes into a certain stage, we would like tasks representing the “next steps” to be automatically generated. Requirements include:
  1. Tasks should only be auto-generated for records with a record type of National Account
  2. All tasks should be assigned to the opportunity owner
  3. If the opportunity is moved to the next stage while there are incomplete auto-generated tasks, those open tasks associated with the prior stage should be closed, with a completed date reflecting the same date that the opportunity stage changed.
    1. Note that tasks that have been manually created by the user should be kept open.
The tasks to be auto-generated for each stage are as follows:
StageTasks
ProspectingTask Name: Complete Sales Information
Due Date: 2 Business Days from Stage Start
Idea GenerationTask Name: Product Sourcing
Due Date: 3 Business Days from Stage Start
 
Task Name: Send Presentation
Due Date: 5 Business Daye from Stage Start
 
Task Name: Idea Presentation Follow Up
Due Date: 8 Business Days from Stage Start
Sample RequestTask Name: Order Samples
Due Date: 2 Business Days from Stage Start
 
Task Name: Sample Follow Up
Due Date: 7 Business Days from Stage Start
Proposal / Price QuoteSend Quote – 5 Business Days from Stage Start
Quote Follow Up – 7 Business Days from Stage Start
Negotiation / ReviewN/A
Closed WonN/A
Closed LostN/A