+ Start a Discussion
Santi Ram RaiSanti Ram Rai 

Trigger in lookup relationship(Value from Pick-list)

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.

This is my Trigger:
trigger StatusActive on Project__c (after insert) {

   for (Project__c fc: Trigger.new) {
        if(fc.Project__c.Project_Task_Name__c != "") 
            fc.Status__c = 'Active';
    }
}

But i have this error:
User-added image
Carlos Campillo GallegoCarlos Campillo Gallego
Hi Santi,

Try with '  instead using .

Regards
Arunkumar RArunkumar R
Hi Santi,

Use single quote instead of double quotes in line no 4.
 
trigger StatusActive on Project__c (after insert) {

   for (Project__c fc: Trigger.new) {
        if(fc.Project__c.Project_Task_Name__c != '') 
            fc.Status__c = 'Active';
    }
}

 
Santi Ram RaiSanti Ram Rai
I have added that:
trigger StatusActive on Project__c (after insert) {

   for (Project__c fc: Trigger.new) {
        if(fc.Project__c.Project_Task_Name__c != '') 
            fc.Status__c = 'Active';
    }
}

Now i ahve this error:
User-added image
Krishna SambarajuKrishna Sambaraju
You can only use single quotes (apostrophe) for string comparisons in apex.
Tarun J.Tarun J.
Hello Santi,

You trigger is on Project object and your logic is that whenever a Project Task record gets created it updates Project record. 
First you need to write trigger on Project Task object and on Insert event you have to implement your logic to update related Project record as Active.

-Thanks,
TK
Santi Ram RaiSanti Ram Rai
loopup relatioship between them.
[Project] onr-to-many [Project Task]
Santi Ram RaiSanti Ram Rai
Hi Tarun Khatri (TK),

I am new in salesforce, do you ahve any example, which related to this issue?
Arunkumar RArunkumar R
Hi santi,

You have to write a trigger on Child object(i.e: Project Task).
 
trigger StatusActiveTrigger on Project_Task__c(after insert, after update) {
    
    List<Project__c> prjList = new List<Project__c>();
    
    for(Project_Task__c fc: Trigger.new)
    {
        if(Trigger.oldMap.get(fc.Id).Project__c !=  fc.Project__c)
        {
            Project__c pr = new Project__c();
            pr.Id = fc.Project__c;
            pr.Status__c = 'Active';
            prjList.add(pr);
        }
    }
    
    update prjList;
}

Replace your exact field and object names in the above code. 


1. Project_Task__c == Child object name

2. List<Project__c> prjList = new List<Project__c>();  // This should be parent object name.

3. fc.Project__c // It should be the lookup field API name in the Project task object.

 
Waqar Hussain SFWaqar Hussain SF
trigger StatusActive on Project__c (after insert) {

   for (Project__c fc: Trigger.new) {
        if(fc.Project_Task_Name__c != '') 
            fc.Status__c = 'Active';
    }
}
Waqar Hussain SFWaqar Hussain SF
Please don't forget to mark "BEST ANSWER" for the most helpful solution.
Santi Ram RaiSanti Ram Rai
Hi Arunkumar R,

I have implemented, but i have this error:
User-added image
Santi Ram RaiSanti Ram Rai
Hi Arunkumar R,

 I got this error.

"Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger StatusActiveTrigger caused an unexpected exception, contact your administrator: StatusActiveTrigger: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.StatusActiveTrigger: line 7, column 1"
Waqar Hussain SFWaqar Hussain SF
Can you please paste your code here.
Santi Ram RaiSanti Ram Rai
Hi Vickey,

This is my code:
trigger StatusActiveTrigger on Project_Task__c(after insert, after update) {
    
    List<Project__c> prjList = new List<Project__c>();
    
    for(Project_Task__c fc: Trigger.new)
    {
        if(Trigger.oldMap.get(fc.Id).Project__c !=  fc.Project__c)
        {
            Project__c pr = new Project__c();
            pr.Id = fc.Project__c;
            pr.Status__c = 'Active';
            prjList.add(pr);
        }
    }
    
    update prjList;
}

 
Waqar Hussain SFWaqar Hussain SF
Error is because of the Trigger.OldMap, you can not use Trigger.oldMap in insert trigger. According to the documentation, Trigger.old and Trigger.oldMap are only available in update and delete Triggers. In insert trigger you will get null value by trigger.oldMap. Thats why the error is thrown.
trigger StatusActiveTrigger on Project_Task__c(after update) {
    
	List<Project__c> prjList = new List<Project__c>();
    
    for(Project_Task__c fc: Trigger.new)
    {
        if(Trigger.oldMap.get(fc.Id).Project__c !=  fc.Project__c)
        {
            Project__c pr = new Project__c();
            pr.Id = fc.Project__c;
            pr.Status__c = 'Active';
            prjList.add(pr);
        }
    }
    
    update prjList;
}

 
Santi Ram RaiSanti Ram Rai
No, this solution is not working. With this above code i am not getting error nor working.
i) Project__c, Project_Task__c are two custom object. 
ii) Status__c is field in object Project__c.
iii) and 'Active' is picklist value in field Status__c.

Please can you help me to fix this. 
Waqar Hussain SFWaqar Hussain SF
trigger StatusActiveTrigger on Project_Task__c(after insert) {
    
	List<Project__c> prjList = new List<Project__c>();
    
    for(Project_Task__c fc: Trigger.new)
    {
        if(fc.Project__c !=  null)
        {
            Project__c pr = new Project__c();
            pr.Id = fc.Project__c;
            pr.Status__c = 'Active';
            prjList.add(pr);
        }
    }
    
    update prjList;
}