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
Eunice Patajo 9Eunice Patajo 9 

Apex Trigger not working on custom objects

Hi everyone,

I have two objects: Segment Brief Tracking and Segment Category Mapping. I've created a trigger that is supposed to get the Segment Category Mapping ID that corresponds to the Segment Name from Segment Brief Tracking - mapped to Segment Name field on Segment Category Mapping.

I'm not getting an error but the Segment Category Mapping ID field is not being populated at all.

I've only started dabbling into Apex triggers so it'd be great to have an expert's opinion on how this trigger could be written better/why it's not working.

Thank you!
 
trigger SegmentBriefTracking_AddSegmentID on Segment_Brief_Tracking__c (before insert, before update) {

//Create a Set of all the Segment Names to be Uploaded

Set<String> SegmentNames = new Set<String>();

//Variable declaration

for (Segment_Brief_Tracking__c segbrieftrack : Trigger.new)
if(segbrieftrack.Segment_Name_For_Upload__c!=''){
SegmentNames.add(segbrieftrack.Segment_Name_For_Upload__c);}

//Map Segment Name for Upload to Segment Taxonomy Name

Map<String,Segment_Category_Mapping__c> mastersheet = new Map<String,Segment_Category_Mapping__c>([Select Id, Segment_Name__c FROM Segment_Category_Mapping__c WHERE Segment_Name__c IN :SegmentNames]);

//Add Segment Category Mapping ID to Segment Brief Tracking Record

for (Segment_Brief_Tracking__c segbrieftrack : Trigger.new)
if(segbrieftrack.Segment_Category_Mapping_ID__c!=null && segbrieftrack.Segment_Category_Mapping_ID__c!=''){
segbrieftrack.Segment_Category_Mapping_ID__c=mastersheet.get(segbrieftrack.Segment_Name_For_Upload__c).Id;}
}

 
Best Answer chosen by Eunice Patajo 9
LBKLBK
Hi Eunice,

I am not sure how I missed this, but the IF condition in line 27 seems to be a contradicting statement.

I think you are supposed to update segbrieftrack.Segment_Category_Mapping_ID__c, if it is NULL or BLANK. Not the other way.

According to your condition, it will update segbrieftrack.Segment_Category_Mapping_ID__c only if it is already filled (NOT NULL and NOT BLANK).

Does this make sense?

All Answers

LBKLBK
Hi Eunice,

Line 15 in your code  may have some issues. Direct assignment to MAP object works only on Id, not on Name field.

Try this code.
trigger SegmentBriefTracking_AddSegmentID on Segment_Brief_Tracking__c (before insert, before update) {

	//Create a Set of all the Segment Names to be Uploaded

	Set<String> SegmentNames = new Set<String>();

	//Variable declaration

	for (Segment_Brief_Tracking__c segbrieftrack : Trigger.new){
		if(segbrieftrack.Segment_Name_For_Upload__c!=''){
			SegmentNames.add(segbrieftrack.Segment_Name_For_Upload__c);
		}
	}
	//Map Segment Name for Upload to Segment Taxonomy Name

	Map<String,Segment_Category_Mapping__c> mastersheet = new Map<String,Segment_Category_Mapping__c>();
	
	List<Segment_Category_Mapping__c> lstSegmentCM = [Select Id, Segment_Name__c FROM Segment_Category_Mapping__c WHERE Segment_Name__c IN :SegmentNames];
	
	for(Segment_Category_Mapping__c objSegmentCM : lstSegmentCM){
		mastersheet.put(objSegmentCM.Segment_Name__c, objSegmentCM);
	}

	//Add Segment Category Mapping ID to Segment Brief Tracking Record

	for (Segment_Brief_Tracking__c segbrieftrack : Trigger.new)
	if(segbrieftrack.Segment_Category_Mapping_ID__c!=null && segbrieftrack.Segment_Category_Mapping_ID__c!=''){
	segbrieftrack.Segment_Category_Mapping_ID__c=mastersheet.get(segbrieftrack.Segment_Name_For_Upload__c).Id;}
}

 
Eunice Patajo 9Eunice Patajo 9
Hi LBK,

Thanks for your reply!

The trigger's still not working though - Segment Category Mapping ID is still not being populated.
LBKLBK
Hi Eunice,

I am not sure how I missed this, but the IF condition in line 27 seems to be a contradicting statement.

I think you are supposed to update segbrieftrack.Segment_Category_Mapping_ID__c, if it is NULL or BLANK. Not the other way.

According to your condition, it will update segbrieftrack.Segment_Category_Mapping_ID__c only if it is already filled (NOT NULL and NOT BLANK).

Does this make sense?
This was selected as the best answer
Eunice Patajo 9Eunice Patajo 9
Hi LBK,

Thank you so much! It works now!

Do you have any tips on how to write the Apex Class to test this trigger? It's currently at 0% Code Coverage so I'd need to write a class to be able to deploy it.