function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Churchill19Churchill19 

Apex trigger to link both Children records which have a lookup relationship once parent has been update?

Hi,

i have a parent object called Projects__c which has a one-many relation to stage__c (Which is the child - this is a master detail relation tp the parent) i have a field Next_Stage__c (on stage__c) with is a lookup to its self.

what i want to do is when there is a update on the Projects__c i want to link the following records on Child Object together: -


Project1
|  
|       |
|                |
|                        |
stage1 ------stage2 

 
Can anyone provide me with an example of how to do this please?

thanks...
Keyur  ModiKeyur Modi
Hi,
you can Query the data like

SELECT id,Name,projectID,Next_stage__c FROM stage__C where projectID=:projetcID AND Next_Stage__r.ProjectId=projectId ;
// here bold porjectId will be your parent (project object's ID) and projectID in the query will be the field name on child object(stage__c)

Thanks,
Keyur Modi
Churchill19Churchill19
Hi Keyur,

not sure that i follow you? stage1 & stage2 are not link yet... thats what i am trying to do? 

thanks,

Sean.
Keyur  ModiKeyur Modi
Hi,
I think there was some misunderstanding. You mentioned in your question like "i have a field Next_Stage__c (on stage__c) with is a lookup to its self." So, I conclude that you have SELF lookup relation ship,

Can you tell me the cleare schema for your question ? It will help me to understand your question properly.

Thanks,
Keyur Modi
Churchill19Churchill19
Hi Keyur,

below is what i have so far but not sure how i can link stage1 with stage2 on the lookup field Next_Stage__c?

 
trigger LinkStageRec on Projects__c (After Update) 

{

    set<Id> ParID = new set<Id>();
    
    
    for(Projects__c ParentObj : Trigger.new)
    {
      ParID.add(ParentObj.Id);
    }    
    
    
    Map<id,stage__c> stage2 = new Map<id,stage__c>(
        [SELECT ID,
				Next_Stage__c
                FROM stage__c
                WHERE ProjectID__c IN :ParID
                AND IDNo__c = 2]
    	);
    System.debug('testing' + childMap1.size());
    
    
    Map<id,stage__c> stage1 = new Map<id,stage__c>([SELECT ID
                FROM stage__c
                WHERE ProjectID__c IN :ParID
                AND IDNo__c = 1]);

 
Keyur  ModiKeyur Modi
Hi ,
It means that project  is one parent object having two child object stage1 and stage 2.

In this case you can do two saperate queary on stage1 and stage 2 .

List<stage1> lststage1 =new List<stage1>();
lststage1 =[SELECT id,parentId FROM  stage1 WHERE parentID:-parrentRecordID]; //put query as per our requirment
List<stage2> lststage2 =new List<stage2>();
lststage2 =[SELECT id,parentId FROM  stage2 WHERE parentID:-parrentRecordID]; //put query as per our requirment

then iterate it with for loop and update the filed value. based on parent value.

Thanks,
Keyur Modi
Churchill19Churchill19
Hi Keyur,

how do i do the for loop? and update the value to link them?

thanks,

sean.
Keyur  ModiKeyur Modi
Hi ,
you can creat instance fro the object and then update the value .
just like this.

stage1 stageobj1=new stage1();

for(stage1 eachstage1:lststage1){
stageobj1.id=eachstage1.id;
stageobj1.field=parentobjectvalue. //update the field which oyu want to update.
lstupdatestage1.add(stageobj1);
}

if(lstupdatestage1.size()>0){
update lstupdatestage1;
}

same thing for stage2 object .

please let me know if you need more help .

Thanks,
Keyur Modi
Churchill19Churchill19
Hi,

so how am i able to get the id from stage1 to update the lookup field "Next_Stage__c" on stage2? so that when i go into record for stage1 it show the stage2 record on the page under "Next Stage"?

thanks,

sean.