0

I SalesForce 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:

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?

1 Answer 1

1

|Normally, you would not use existing data to test a trigger. If possible, change the code so a new record is created and test the trigger like that.

@isTest
public class SystemChangeRequestTest {
    static testMethod void myUnitTest()
    {
        // get record based on SF ID
        Request_for_System_Change__c req = new Request_for_System_Change__c();
        //add your fields
        insert req;

        // x2T55000000D2hp
        // R-1512027

    }    
}

If it is not possible, this is what you do:

@isTest(SeeAllData=true)
public class SystemChangeRequestTest {
    static testMethod void myUnitTest()
    {
        // get record based on SF ID
        Request_for_System_Change__c req = [select id from Request_for_System_Change__c where id = 'x2T55000000D2hp'];

        // x2T55000000D2hp
        // R-1512027

    }    
}
1
  • yes you ar eright, I was missing the point as my manager put it... and will do it by adding a new record then setting it's status to 'Approved'. Thanks Dec 9, 2015 at 15:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .