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
anjaanja 

cross object access with visualforce

I have a parent object "admission form"

it has two related lists:

"survey 1"

"survey 2"

 

we would like to add a single field on survey 1 that concatenates 14 fields from survey 2

 

my difficulty is in accessing the data from survey 2

 

tried using a formula field but ran into difficulty with # of unique relationships per object (vlookup)

 

now trying visualforce, possibly with controller

any ideas?

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Your trigger should be like this

trigger concatFields on Discharge_Survey__c (before insert, before update){

    List<ID> ids = new List<ID>();   
    for (Discharge_Survey__c disChargeSurvey : trigger.new)   
    {     
       //In below Clinical_Pathway__c this should be the APIName of reference field ( lookup or master detail which eer you have) 
       ids.add(disChargeSurvey.Clinical_Pathway__c);   
    }
   
List<Clinical_Pathway__c> survey2s = new List<Clinical_Pathway__c>([Select Patient_Admission_Readmission__c, Fluid_status_assessment__c,Fluid_management_orders__c        
from Clinical_Pathway__c        
where ID IN: ids]);
    for (Discharge_Survey__c s1: trigger.new)   
{     
for (Clinical_Pathway__c s2: survey2s)     
{       
if  (s1.Patient_Admission_Readmission__c == s2.admission_form__c)       
{           
s1.CHFFluid_Management_Pathway__c = s2.Fluid_status_assessment__c + s2.Fluid_management_orders__c;       
}     
}   
}
}

 This trigger is only work correct for single record, for bulk it will not give perfect results, just check for single if it work I will help you to make it for bulk as well. let me know if any issues in it.

All Answers

Jake GmerekJake Gmerek

Sounds like you need a trigger.

 

trigger concatFields on Survey_1__c (before insert, before update){

List<ID> ids = new List<ID>
for (Id thisID: trigger.new){
  ids.add(thisID);
}
List<Survey_2__c> survey2s = new List<Survey_2__c>([Select admission_form__c, field1__c, field2__c, ... from Survey_2__c where ID IN: ids]);

for (survey_1__c s1: trigger.new){
  for (surver_2__c s2: survey2s){
    if  (s1.admission_form__c == s2.admission_form__c){
       s1.field_to_hold_values = s2.field1__c + s2.field2__c + ...;
   }
  }
}
}

 The above gives you the main idea, however you will need to modify it to fit your environment.  Also, it will only populate the field when a survey 1 is inserted or updated, so you will have to consider that.

 

Good Luck!

anjaanja

this is very close, however I am getting the following error and not sure what I am doing wrong: 

Error: Compile Error: Loop variable must be of type SOBJECT:Discharge_Survey__c at line 5 column 13

 

trigger concatFields on Discharge_Survey__c (before insert, before update){


    List<ID> ids = new List<ID>();   

    for (ID thisID: trigger.new)   

{     

ids.add(thisID);   

}
   

List<Clinical_Pathway__c> survey2s = new List<Clinical_Pathway__c>([Select Patient_Admission_Readmission__c, Fluid_status_assessment__c,Fluid_management_orders__c        

from Clinical_Pathway__c        

where ID IN: ids]);
    for (Discharge_Survey__c s1: trigger.new)   

{     

for (Clinical_Pathway__c s2: survey2s)     

{       

if  (s1.Patient_Admission_Readmission__c == s2.admission_form__c)       

{           

s1.CHFFluid_Management_Pathway__c = s2.Fluid_status_assessment__c + s2.Fluid_management_orders__c;       

}     

}   

}

}

Shashikant SharmaShashikant Sharma

Your trigger should be like this

trigger concatFields on Discharge_Survey__c (before insert, before update){

    List<ID> ids = new List<ID>();   
    for (Discharge_Survey__c disChargeSurvey : trigger.new)   
    {     
       //In below Clinical_Pathway__c this should be the APIName of reference field ( lookup or master detail which eer you have) 
       ids.add(disChargeSurvey.Clinical_Pathway__c);   
    }
   
List<Clinical_Pathway__c> survey2s = new List<Clinical_Pathway__c>([Select Patient_Admission_Readmission__c, Fluid_status_assessment__c,Fluid_management_orders__c        
from Clinical_Pathway__c        
where ID IN: ids]);
    for (Discharge_Survey__c s1: trigger.new)   
{     
for (Clinical_Pathway__c s2: survey2s)     
{       
if  (s1.Patient_Admission_Readmission__c == s2.admission_form__c)       
{           
s1.CHFFluid_Management_Pathway__c = s2.Fluid_status_assessment__c + s2.Fluid_management_orders__c;       
}     
}   
}
}

 This trigger is only work correct for single record, for bulk it will not give perfect results, just check for single if it work I will help you to make it for bulk as well. let me know if any issues in it.

This was selected as the best answer