+ Start a Discussion
Santi Ram RaiSanti Ram Rai 

Trigger in lookup relationship

Hi everybody,

i) I have custom Objests: (Project, Project Task)
ii) They have lookup relationship.[Project can have many Project Tasks]

So, i have custom field called Status Type PickList(Active,Inactive,Closed) in Object Project.
Now, my Logic is: When the Project Task is created under Project the Status should change to Active.

Do some one has idea?
pconpcon
You would simply have a trigger on the Project Task object.  Then you'd update the projects that are related.
 
trigger ProjectTask_StatusUpdate on ProjectTask__c (after insert) {
    Set<Id> projectIds = new Set<Id>();

    for (ProjectTask__c t : Trigger.new) {
        projectIds.add(t.Project__c);
    }

    projectIds.remove(null);

    if (!projectIds.isEmpty()) {
        List<Project__c> projects = new List<Project>();

        for (Id id : projectIds) {
            projects.add(new Project__c(
                Id = id,
                Status__c = 'Active'
            ));
        }

        update project;
    }
}
NOTE: This code has not been tested and may contain typographical or logical errors

You will need to update this code with the right object and field names.

In the code above we get a list of all the projects that are being affected.  Then if we have projects to update we build a list of sparse Projects with their new status.  Then we update those projects.  This code does not take into account the existing status and will always update any Project when a new Task is created.
 
Santi Ram RaiSanti Ram Rai
I got this error message "
Error: Compile Error: Entity is not api accessible at line 1 column 1"
pconpcon
Did you update the code to use your object's API names?  Also, what version of the API are you using for your trigger?
Santi Ram RaiSanti Ram Rai
Yes, i have updated the API names. But how to see the version of the API are you using for your trigger?
pconpcon
What are you using to develop / deploy your trigger to Salesforce?
Santi Ram RaiSanti Ram Rai

Yes, trigger to Salesforce.
 
pconpcon
No, what are you using to develop the Trigger?  Salesforce developer console? Eclipse? Ant? Mavens Mate?
Santi Ram RaiSanti Ram Rai

Salesforce developer console.
Santi Ram RaiSanti Ram Rai
I selected object and created New Trigger under that object.
Santi Ram RaiSanti Ram Rai
Now, i got this error message: "Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger ProjectTask_StatusUpdate caused an unexpected exception, contact your administrator: ProjectTask_StatusUpdate: execution of AfterInsert caused by: System.StringException: Invalid id: ff: External entry point"
Santi Ram RaiSanti Ram Rai
Hi pcon,

Now, i got this error message: "Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger ProjectTask_StatusUpdate caused an unexpected exception, contact your administrator: ProjectTask_StatusUpdate: execution of AfterInsert caused by: System.StringException: Invalid id: ff: External entry point"
 
pconpcon
Is the Project__c field you are using a lookup / M-D to the Project object?
Santi Ram RaiSanti Ram Rai
No, Project__c is Custom object, but Project_Task_Name  is a lookup / M-D to the Project object.

 
pconpcon
Can you please include your updated code as well as a screenshot of the field definitions for your project task object?

NOTE: When including code please use the "Add a code sample" button (icon <>) to increase readability and make referencing code easier.