You need to sign in to do that
Don't have an account?
Churchill19
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
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...
thanks...
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
not sure that i follow you? stage1 & stage2 are not link yet... thats what i am trying to do?
thanks,
Sean.
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
below is what i have so far but not sure how i can link stage1 with stage2 on the lookup field Next_Stage__c?
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
how do i do the for loop? and update the value to link them?
thanks,
sean.
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
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.