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
Supriyo Ghosh 9Supriyo Ghosh 9 

trigger update from one object to anther object.

Hi,

I want to updateto date field based on another object value.If my Case object having a picklist field value ABCD then in policy object i wnat to update two date fields.I will match Policy no from my case object so that it will update that exact policy start date and end date. Please help
D-CoderD-Coder
What is relationhip in Case and Polocy ? Lookup ? Who is the parent and who is the child ?
Sumeet_ForceSumeet_Force
write trigger on case object and write conditional logic to match and fire DML update operation on your policy object.
Supriyo Ghosh 9Supriyo Ghosh 9
I will put policy no in case object custom field.
D-CoderD-Coder
Here you go ::
 
Trigger updatePolicyDates on Case (After Insert, After Update) {
    
	Set<Id> caseIds = new Set<Id>();
	Set<Id> policyIds = new Set<Id>();
	List<Policy__c> policyList = new List<Policy__c>();
	List<Policy__c> policyListToUpdate = new List<Policy__c>();
    List<Case> caseList = New List<Case>();

    for(Case  c : Trigger.New){
		
		caseIds.add(c.id);
		
    }

	caseList = [SELECT id, pickList__c , policy__c FROM Case WHERE id IN : caseIds];
	
	for(Case c : caseList){
		
		policyIds.add(c.policy__c);
	}
	
	policyList = [SELECT id, start_date__c , end_date__c FROM Policy__c WHERE id IN : policyIds];
	
	Map<id, Policy__c> caseToPolicyMap = new Map<id, Policy__c>();
	
	for(Case c : caseList){
		
		for(Policy__c p : policyList){
			
			if(c.policy__c == p.id){
				
				caseToPolicyMap.put(c.id,p);
			}
		}
	}
	
	for(Case c : caseList){
		
		If(c.pickList__c == 'ABCD'){
		
			Policy__c p = caseToPolicyMap.get(c.id);
			
			if(p != null){
				
				p.start_date__c = 'Set your start date here';
				p.end_date__c = 'Set your end date here';
				
				policyListToUpdate.add(p);
			}
			
			
        }
    }
	
	if(policyListToUpdate != null && policyListToUpdate.size()>0){
		
		update policyListToUpdate;
	}
    
}
If this answers your query please mark this question as a solved so that it can be filtered out from unsolved questions.