+ Start a Discussion
Nick WhiteNick White 

How to copy one Boolean field, into another liked object via a Trigger

I have a Junction Object = Linkage__c
When field Confidential_Link__c [Checkbox:Boolean] is updated I wish for the trigger to update another Checkbox field in a linked Object, TripReport__c.
I am not an experienced Apex programmer, so trying to keep it simple and have the following.

trigger Update_TR_ConfFlag on Linkage__c (after insert, after update){
for(Linkage__c c : Trigger.new)
TripReport__c.Confidential_Flg__c = c.Confidential_Link__c;
}

...but I get: "Error: Compile Error: Expression cannot be assigned at line -1 column -1"

Can anyone help?
Best Answer chosen by Nick White
Dhanya NDhanya N
Hi Nick,

I hope there is a relationship from   Linkage__c to TripReport__c.
If so, refer the code:
trigger Update_TR_ConfFlag on Linkage__c (after insert, after update){

map<Id, Linkage__c> mapTripReport = new map<Id, Linkage__c>();
list<TripReport__c> lstTripReport = new list<TripReport__c>();
for(Linkage__c objLinkage : Trigger.new) {
	
		//Store TripReport__c Id and Linkage in a map
		mapTripReport.put(objLinkage.TripReport__c, objLinkage);
	}
	
	for(TripReport__c objTripReport : [Select Id, Confidential_Flg__c From TripReport__c Where Id IN: mapTripReport.keySet()]) {
		
		objTripReport.Confidential_Flg__c = mapTripReport.get(objTripReport.Id).Confidential_Link__c;
		lstTripReport.add(objTripReport);
	}
	
	if(!lstTripReport.isEmpty())
		update lstTripReport;
}
Thanks,
Dhanya

All Answers

Dhanya NDhanya N
Hi Nick,

I hope there is a relationship from   Linkage__c to TripReport__c.
If so, refer the code:
trigger Update_TR_ConfFlag on Linkage__c (after insert, after update){

map<Id, Linkage__c> mapTripReport = new map<Id, Linkage__c>();
list<TripReport__c> lstTripReport = new list<TripReport__c>();
for(Linkage__c objLinkage : Trigger.new) {
	
		//Store TripReport__c Id and Linkage in a map
		mapTripReport.put(objLinkage.TripReport__c, objLinkage);
	}
	
	for(TripReport__c objTripReport : [Select Id, Confidential_Flg__c From TripReport__c Where Id IN: mapTripReport.keySet()]) {
		
		objTripReport.Confidential_Flg__c = mapTripReport.get(objTripReport.Id).Confidential_Link__c;
		lstTripReport.add(objTripReport);
	}
	
	if(!lstTripReport.isEmpty())
		update lstTripReport;
}
Thanks,
Dhanya
This was selected as the best answer
Nick WhiteNick White
Hi Danya, This works a treat.
Many thanks.