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
Prasanth RPrasanth R 

i created two objects such that patient(parent),caselist(child).caleslist is related to patient with lookup relationship.Moreover,i created the insurance field(checkbox) in patient and category field(picklist) in caselist.

scenario:If the category is covid,insurance should true,
i try the below code it running without error but the insurance field is not active..kindly someone help me.below i added the codes and screenshot.

kindly someone give the correct code.
User-added imageUser-added imageUser-added image
PriyaPriya (Salesforce Developers) 

Hi Prashant,

Can you please provide your code instead of screenshot of it.

Regards,

Priya Ranjan

Prasanth RPrasanth R
trigger Insurance on Case_List__c (after insert,after update)
{
    List<Apex_Patient__c> ptlt = new List<Apex_Patient__c>();
    Map<Id,Case_List__c> ctlt = new Map<Id,Case_List__c>();
    for(Case_List__c objIns:Trigger.new){
        ctlt.put(objIns.id,objIns);
    }
    Map<Id,Apex_Patient__c> newpt = new Map<Id,Apex_Patient__c>([Select Id,Name,Insurance__c from Apex_Patient__c WHERE
                                                                 Id IN:ctlt.keySet()]);
    for(Apex_Patient__c obj: newpt.values())
    {
        if(obj.id==ctlt.get(obj.id).Patient__c && ctlt.get(obj.id).Category__c=='Covid')
        {
            obj.Insurance__c =True;
            ptlt.add(obj);
        }
    }
    update ptlt;
}
 
CharuDuttCharuDutt
Hi Prasanth
Try Below Trigger
trigger Insurance on Case_List__c (after insert,after update)
{
	List<Apex_Patient__c> ptlt = new List<Apex_Patient__c>();
	set<id> lstPatientId = new set<Id>();
	for(Case_List__c cl:trigger.new){
		if(cl.Patient__c != null){
		lstPatientId.add(cl.Patient__c);
		}
	}
	list<Apex_Patient__c> aP = [Select Id,Name,Insurance__c,Category__c from Apex_Patient__c WHERE
                                                                 Id IN:lstPatientId];
	for(Apex_Patient__c obj: aP){
        if(obj.Category__c == 'Covid'){
            obj.Insurance__c = true;
            ptlt.add(obj);
        }
    }														 
    update ptlt;
}
Please Mark It as Best Answer If It Helps
Thank You!
Prasanth RPrasanth R
@charDutt..not working,it's error
CharuDuttCharuDutt
What Error is coming
Prasanth RPrasanth R
User-added image
CharuDuttCharuDutt
Hii Prasanth
Tyr Below Code
trigger Insurance on Case_List__c (after insert,after update)
{
	List<Apex_Patient__c> ptlt = new List<Apex_Patient__c>();
	set<id> lstPatientId = new set<Id>();
	for(Case_List__c cl:trigger.new){
		if(cl.Patient__c != null && cl.Category__c == 'Covid'){
		lstPatientId.add(cl.Patient__c);
		}
	}
	list<Apex_Patient__c> aP = [Select Id,Name,Insurance__c from Apex_Patient__c WHERE
                                                                 Id IN:lstPatientId];
	for(Apex_Patient__c obj: aP){
        
            obj.Insurance__c = true;
            ptlt.add(obj);
  
    }														 
    update ptlt;
}
Please Mark It as Best Answer If It Helps
Thank You!