0

I have made a trigger for the object Assignment. When an assignment is inserted the after insert trigger should check if there is a linked project. If this is not the case it should make one.

Now I am working on a test class. All the other triggers on Assignment are being called, but for some reason after insert is not.... In the test class I do insert an assignment, so it should fire, right?

Is there a logical reason for this?

thanks in advance

EDIT:

public static void isAfterInsert(List<Assignment__c> newList, Map<id,sObject> newMap){
    List<Project__c> projectList = new List<Project__c>();

    for(Assignment__c item: newList){
        if(item.Project__c != null){
            Project__c project; 
            project = new Project__c();
            project.Id = item.Project__c;
            project.RecordStatus__c = item.RecordStatus__c;
            project.OwnerId = item.OwnerId;
            project.EmployeeID__c = item.Employee__c;
            projectList.add(project);
        }
    }
    update projectList;
}
3
  • yes if you are inserting any assignment record then trigger should be fire. You can track by inserting debug or you can test by creating a assignment in developer console and test it there.
    – D-Horse
    Jul 11, 2014 at 9:56
  • So this is the code called from your trigger which is fired after (or before) insert? In this case I am wondering why you create a new Project if the Assignment has already assigned one - your condition says "if(item.Project__c != null)". That is a not equal to null. Also, how can you assign the Id value to project? I'd think that is not possible as Id is a read-only field. It might work though if you create it using "project = new Project__c(id = item.Poject__c);" Furthermore, the code of your test might help here.
    – FrankZ
    Jul 11, 2014 at 11:58
  • 1
    Can you add your actual trigger? This code doesn't prove that anything should happen in an after context. Jul 11, 2014 at 12:17

1 Answer 1

1

which of this objects Assignment or Project is the master object?

if you have the lookup field in Assigment object, your trigger shouldn't be after insert, because you need to update your assigment with project Id.

if you have the lookup field in project object, we need to see your code.

2
  • Thanks a lot! That was indeed a mistake. Project is the master object. However, I have changed it to before insert and, while I do make an insert call, it is still not being covered..
    – Thomas
    Jul 11, 2014 at 10:35
  • You could also add a simple validation rule that prevents you from creating an Assignment for a Project that doesn't exist. That forces the User to create a new Project or look up the correct one. If you think about it, do you want to randomly create new Projects when one might already exist?
    – crmprogdev
    Jul 12, 2014 at 15:33

You must log in to answer this question.

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