0

I've been working on a test method but I've had a problem creating the setup of it.

public static List<Job_Posting__c> createPostings(){
        List<Job_Posting__c> testPostings = new List<Job_Posting__c>();
        List<Position__c> testPositions = new List<Position__c>();
        List<Employment_Website__c> testEmploymentWebsites = new List<Employment_Website__c>();

        for(Integer i=0; i<10; i){
            testPositions.add(new Position__c(Name = 'Test '+ i, Status__c = 'Closed - Filled'));
            insert testPositions;
            testEmploymentWebsites.add(new Employment_Website__c(Name = 'Test' +i));
            insert testEmploymentWebsites;
            testPostings.add(testPostions, testEmploymentWebsites);

        }
        return testPostings;

I know my insertion of into testPostings is wrong but how would I insert both into it. I know both Position and EmploymentWebsites will share a name. So would I insert that way?

1

You would create the parents first, then children later. This means using multiple for loops and moving the DML operations out of the loop. Here's a basic modification with some assumptions thrown in.

List<Job_Posting__c> testPostings = new List<Job_Posting__c>();
List<Position__c> testPositions = new List<Position__c>();
List<Employment_Website__c> testEmploymentWebsites = new List<Employment_Website__c>();

for(Integer i=0; i<10; i){
    testPositions.add(new Position__c(Name = 'Test '+ i, Status__c = 'Closed - Filled'));
    testEmploymentWebsites.add(new Employment_Website__c(Name = 'Test' +i));
}
insert testPositions;
insert testEmploymentWebsites;
for(Integer i=0; i<10; i){
    testPostings.add(new Job_Posting__c(Position__c=testPositions[i].Id, Employment_Website__c=testEmploymentWebsites[i].Id));
}
insert testPostings;
return testPostings;

This code assumes that Job Posting is a many-to-many relationship object, and the field names match the object names. You may need to adjust the field names accordingly.

  • Thanks! So in this case my objects are in a master-detail relationship. The master being the testPostings and the detail being EmploymentWebsites and tesPositions so I would just change those to __r right? – YGarcia Feb 1 at 15:23
  • @YGarcia No, you need to link the child to the parent. In other words, insert the postings first, then the others. You need to link them using the lookup field, which will end in __c. – sfdcfox Feb 1 at 15:38
  • So how would I go about populating the list of testPostings first if I don't have testPostings and testEmploymentsWebsites initialized? – YGarcia Feb 1 at 16:16
  • @YGarcia The children don't exist without their parents; you'd create the the parents first and the children after. – sfdcfox Feb 1 at 16:23
  • OK so just put the for loop for testPostings before the other two should work. – YGarcia Feb 1 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.