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
Mahmoud Coudsi 1Mahmoud Coudsi 1 

Autofill X look-up field that is built on Y object with the corresponding X record.

The problem:
 
I'm not able to autofill the program look-up field that is built on the appointments (custom-object) with the corresponding program (custom-object) record.
 
Background:
 
X is a medical clinic that runs weight loss programs for obesity patients. X offers three types of programs; VLCD (Very Low Calorie Diet), Stepwise, Maintenance. Each of the programs length is 6 month.
 
Patients normally schedule appointments either weekly, bi-monthly, or monthly for six month period.
 
Objective:
 
My goal to write a code that tie every appointment record to the unique correspondent program for the patient. A program may have zero or many appointments. (This is what I'm trying to resolve here)
 
What I've built:
 
I have built three objects:
 
1. Patients (Accounts; standard-object)
 
2.Programs (Custom object; each record represents a program that is unique for the patient)
* There is a master-detail relationship between the patients object and program. Patients may have zero or many programs.
 
3.Appointments: (Custom object; each record represents an appointment that the patient has made).
* I've built a look-up relationship between the programs object and the appointments object. I want to tie every appointment to a unique program. A program may have zero or many appointments. (This is what I'm trying to resolve here)
 
I've tried these two methods (Process builder and workflow automation rules, and they both failed):
*I was told by SF support that this only can be done through APEX code since its "objects are falling under Dynamic Relationship"

 
I tried to do it through creating workflow automation rules and process builder by setting this condition and immidate actions below. When formula evaluates to true:
 
AND(Program__r.Program_start_date__c < appointments__r.
scheduled_time__c,
appointments__r.scheduled_time__c < Program__r.Program_end_date__c)
 
Fire Workflow action -- Update look-up field: Program name: appointemnts__r.patient__r.Program__c
 
Results: Appointments_r.Program__c remained NULL.

*I was told by SF support that this only can be done through APEX code since its "objects are falling under Dynamic Relationship"

Can someone please help me automate this look-up field update through APEX ? Clear guidance would be appericated since I have zero experince with APEX coding.


P.S:

All the fields (scheduled date, programs..etc) metioned above are autofiled either through APIs calls from our other internal systems or by being calculated fileds. 
sandeep reddy 37sandeep reddy 37
programe is dynamic field beacuase user need to change field update work in processbuilder but it always same look up value updates  every person have diffrent Appointments and diffrent programe like VLCD,Stepwise, Maintenance. so in this case go to apex programe otherwise use process builder so use process builder resolve your problrms