You need to sign in to do that
Don't have an account?
Our Man In Bananas
How to get an existing record to test Apex trigger
we have created a custom object called "Request for System Change" (API name **Request_for_System_Change__c**) and a trigger that runs when the approval process sets the status to "Approved".
The trigger creates a new record on another custom object called "Demand"
This is the only Apex code I have, but I need to write a test method that will update the status of an existing *Request for System Change* record, setting it's status to **New**. This should kick off the trigger, creating a new record on the *Demand* object...
Here is my trigger:
So now I need to write a test method that will get that existing record, and update the status to "New"
Here is where I am stuck:
What is the correct way to do this?
The trigger creates a new record on another custom object called "Demand"
This is the only Apex code I have, but I need to write a test method that will update the status of an existing *Request for System Change* record, setting it's status to **New**. This should kick off the trigger, creating a new record on the *Demand* object...
Here is my trigger:
trigger SystemChangeRequest on Request_for_System_Change__c (after update, before insert, before update) { system.debug('step 1'); string systemChangeRecordType = Schema.SObjectType.Request_for_System_Change__c.getRecordTypeInfosByName().get('System Change Request').getRecordTypeId(); string mailshotDataRequestRecordType = Schema.SObjectType.Request_for_System_Change__c.getRecordTypeInfosByName().get('Mailshot data request').getRecordTypeId(); string dataRequestAndApprovalRecordType = Schema.SObjectType.Request_for_System_Change__c.getRecordTypeInfosByName().get('Data request and approval').getRecordTypeId(); system.debug('recort type = '+ systemChangeRecordType); system.debug('step 2'); if (trigger.isBefore && (trigger.isInsert || trigger.isUpdate)) { system.debug('step 3 - set Manager ID & department name'); for (Request_for_System_Change__c r : Trigger.new) { string LineManagerid = [SELECT Id, ManagerId from User where Id =: r.OwnerId].ManagerId; string DeptName = [SELECT Id, Department from User where Id =: userinfo.Getuserid()].Department; r.Manager__c = LineManagerId; r.Department_Name_String__c = DeptName; } } if (Trigger.IsUpdate && trigger.isbefore) { Boolean convertToDemand=false; for (Request_for_System_Change__c rq : Trigger.new) { system.debug('step 4a Record Type='+rq.RecordType.Name); // Access the "old" record by its ID in Trigger.oldMap Request_for_System_Change__c oldrecord = Trigger.oldMap.get(rq.Id); system.debug('get LineManagerName'); string lineManagerName = [SELECT Name from User where Id =: rq.Manager__c].Name; system.debug('Line Manager name for Demand: ' + lineManagerName); if (rq.Status__c != OldRecord.Status__c && rq.Status__c == 'Approved' && rq.RecordTypeId == systemChangeRecordType ) { system.debug('step 4b - Approved and create demand for '+ systemChangeRecordType); apm2__Demand__c newD = new apm2__Demand__c(); newD.Demand_request_date__c = rq.Date__c; newD.Project_Department__c = 'Systems'; newD.Demand_Title__c = rq.Request_Title__c; newD.apm2__Criticality__c = rq.Request_Priority__c; newD.Demand_Requested_by__c = lineManagerName; newD.Prioritisation_points__c = rq.Benefits_Reasons__c; newD.Demand_request_date__c = system.today(); newD.apm2__Business_Driver__c = rq.Benefits_Reasons__c; newD.Demand_Status__c = 'New'; newD.apm2__Description__c = rq.Description_of_change_requested__c; newD.Additional_Information__c = rq.Anticipated_benefits_reason_for_change__c + ' \r\n' + rq.Additional_information_comments__c; newD.Department_Requested_by__c = rq.Department_Requested_by__c; newD.Systems_Affected__c=rq.Systems_Affected__c; insert newD; rq.Demand__c = newD.Id; } } }I have an existing record, which has a SalesForce ID (like 'x2T55000000D2hp') (and the record ID within the object is **R-1512027**)
So now I need to write a test method that will get that existing record, and update the status to "New"
Here is where I am stuck:
@isTest public class SystemChangeRequestTest { static testMethod void myUnitTest() { // get record based on SF ID Schema.SObjectType.Request_for_System_Change__c.getRecord req = Trigger.newMap.get('x2T55000000D2hp'); // x2T55000000D2hp // R-1512027 } }I have an *invalid type* error where I am trying to get the record
What is the correct way to do this?
First thing you can't get teh Trigger,new and all other value of realted to trigger in Test class.
Test class is different and Trigger is different.
You need to create a record in test class first, and send it for approal through the code (if you have created any workflow approval process than donn't need just make the reocrd will met the crieteria of workflow approval ) and then approved it through apex code. All the thing will need to do in Test class.
When you approve the record through apex in test class trigger will fire and cover the code for test coverage.
here is example
I've created a opporutnity first .
here is code to approval code
-Thanks
Ashlekh Gera