2015-11-08 117 views
0

我在Salesforce APEX中記錄了觸發器。它工作正常。爲Salesforce/APEX觸發器編寫測試類

代碼爲觸發器​​是:

trigger SDRDemoUpdate_test on Event (before update) { 
    Map<ID,Event> records = New Map<ID,Event>([SELECT CreatedBy.Name FROM Event WHERE ID IN: trigger.new]); 

    for (Event obj :Trigger.new){ 
     obj.SDR_Original_Demo__c = records.get(obj.id).CreatedBy.Name; 
    } 
} 

現在我嘗試寫代碼的測試類。它在線發出錯誤,表示對象不能被解析爲字符串。

代碼爲測試類:

@isTest 
public class originalDemo { 
    static testMethod void test_original_demo() { 
     Event obj = new Event(); 
     obj.CreatedBy = 'Tom'; 
     obj.Owner = 'Jack'; 
     obj.What = 'Opportunity'; 

     insert.obj; 

     userInfo.getName(); 
    } 
} 

期待找出解決方案。任何幫助,將不勝感激。

感謝

回答

0

您的問題在這些線路

obj.CreatedBy = 'Tom'; 
    obj.Owner = 'Jack'; 
    obj.What = 'Opportunity'; 

你試圖通過String到所需Id領域。

User user = [SELECT Id FROM User LIMIT 1]; 
    obj.CreatedById = UserInfo.getUserId(); 
    obj.OwnerId = user.Id; 
    obj.WhatId = opportunity.Id;