+ Start a Discussion
DeptonDepton 

How to set the ID field (look up) in a test method?

Hi,

 

I got the error:

 

Failure Message: "System.StringException: Invalid id: xx", Failure Stack Trace: "Class.take_ownership.test: line 13, column 90 External entry point"

 

the field, is a look up = Project_Milestone__c

 

so I have written in the test:

 

Project_Milestone__c= 'xx'

 

but then I´ve got the error.

 

How should I set the look up field ID for the test method?

 

Thank you!

Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower

Well, I expect that you don't really have a "Project_Milestone__c" object with that exact name.  So, it's telling you that's not a valid type.  Check your spellings, check the API name of your object.  -S

All Answers

SteveBowerSteveBower

You should create a Project_Milestone__c object, and then insert it.  After the insert you will have the id.

 

Project_Milestone__c pm = new Project_Milestone__c(Name='Peanuts'  ... other fields if you need them, etc.);

insert pm;

 

Then later, when you need an Id for a Project_Milestone__c you use pm.id.

 

 

You would be well served by reading the "Testing Apex" section of the Apex documentation.

 

Best, Steve.

DeptonDepton

I got it!!!

 

Grand!!! thank you very much!!!

 

I was reading the "workbook" this morning on the train and I remember i read this part, I think I understood perfectly how to set it up.

 

:)

DeptonDepton

  public static testmethod void test() {

 

   Project_Milestone__c pm = new Project_Milestone__c(Name='Peanuts', Stage__c= 'Production',   Kickoff__c=Date.today(), Deadline__c= Date.today() 

 

 ); 

 

insert pm;   

 

  Milestone1_Task__c record = new Milestone1_Task__c(Name='test',

Project_Milestone__c= 'pm.id',    Task_Stage__c= 'Planned',Start_Date__c=Date.today(),Due_Date__c =Date.today(),Assigned_To__c='Marketing VSN' );   

 

insert record;

 

 

I have finished it and got this error:

 

Error: Compile Error: Invalid type: Project_Milestone__c at line 13 column 34 : "in Bold"

 

 

Thank you

SteveBowerSteveBower

......, Project_Milestone__c = pm.id, ......

 

Don't put it in quotes, you're referring to a property of a declared sobject variable, not a string.   -Steve

DeptonDepton
public class take_ownership {
  private ApexPages.StandardController con;
  public take_ownership(ApexPages.StandardController controller) {
    con = controller;
  }
  public ApexPages.PageReference Take_and_Go_Back() {
    SObject record = con.getRecord();
    record.put('Assigned_To__c',UserInfo.getUserId());
    update record;
    return con.view();
  }
  public static testmethod void test() {
   Project_Milestone__c pm = new Project_Milestone__c(Name='Peanuts', Stage__c= 'Production',
   Kickoff__c=Date.today(), Deadline__c= Date.today();
  insert pm;
  
    Milestone1_Task__c record = new Milestone1_Task__c(Name='test',Project_Milestone__c= pm.id,
    Task_Stage__c= 'Planned',Start_Date__c=Date.today(),Due_Date__c =Date.today(),Assigned_To__c='Marketing VSN' );
    insert record;
    Test.setCurrentPage(Page.take_ownership);
    ApexPages.StandardController controller = new ApexPages.StandardController(record);
    take_ownership extension = new take_ownership(controller);
    extension.Take_and_Go_Back();
  }
}

 




Error: Compile Error: expecting a right parentheses, found ';' at line 14 column 53

 

In bold the error

DeptonDepton

I am trying to create first the Project_Milestone__c

 

and then Milestone1_Task__c 

 

I have added a ) in this line

 Kickoff__c=Date.today(), Deadline__c= Date.today() );

 

but then I got the error:

 on the previous line:

 

Error: Compile Error: Invalid type: Project_Milestone__c at line 13 column 34

 

Any ideas!!??

Thank you!!

SteveBowerSteveBower

Well, I expect that you don't really have a "Project_Milestone__c" object with that exact name.  So, it's telling you that's not a valid type.  Check your spellings, check the API name of your object.  -S

This was selected as the best answer
DeptonDepton

Thank you, 

 

you were right!!

 

Deployed!!

 

Thank you!