0

I have a requirement where we will be using dataloader to upsert a number of records, and do not want to import records with matching Case_Id__c. I will be handling the update through flows since I am a novice at best at Apex. What I believe the code should be doing is creating a list of the records being imported, comparing to what we have in the database for duplicate Case_ID__c, then removing those Case_ID__c from the list before the insert. I believe there is something I'm missing at the getting existing Case_ID__c portion, but your support is appreciated and codes below:

trigger checkDuplicateCareMetxDataTrigger on CareMetx_Data__c (before insert) {

// get triggering CMX data case IDs
Set <String> CMXcaseID = new Set<String>();
for(CareMetx_Data__c newCMXData:Trigger.new){
    CMXcaseID.add(newCMXData.Case_ID__c);
}
// get existing CMX data case IDs
List<CareMetx_Data__c> caseIDs = [SELECT Case_ID__c FROM CareMetx_Data__c WHERE Case_ID__c =: CMXcaseID];
Set<String> existingCaseIDs = new Set<String>();
    for(CareMetx_Data__c c: caseIDs){
        existingCaseIDs.add(c.Case_ID__c);
    }
    System.debug('Existing case IDs: ' + existingCaseIDs);
// get existing CMX data case IDs from triggering records and remove
for(CareMetx_Data__c insertCMXcaseID: [SELECT Case_ID__c FROM CareMetx_Data__c WHERE Case_ID__c !=: existingCaseIDs]){
    CMXcaseID.remove(insertCMXcaseID.Case_ID__c);
}
System.debug('Case ID before trigger: ' + CMXcaseID);

}

@isTest

public class duplicateCareMetxDataTest {

static testMethod void validatecheckDuplicateCareMetxData() {
    
   // **NEW CODE BLOCK INSERTS NEW CAREMETX RECORD**
   
   CareMetx_Data__c newCMX = new CareMetx_Data__c(Case_ID__c = '0000000');
   System.debug('Case ID before insert: ' + newCMX.Case_ID__c);

   // insert CareMetx data
   insert newCMX;

   // retrieve the new CareMetx data
   newCMX = [SELECT Case_ID__c FROM CareMetx_Data__c WHERE Id =:newCMX.Id];
   System.debug('CareMetx case ID after trigger fired: ' + newCMX.Case_ID__c);
    
   // **NEW CODE BLOCK INSERTS EXISTING CAREMETX RECORD**
   
   CareMetx_Data__c existCMX = new CareMetx_Data__c(Case_ID__c = '6279270');
   System.debug('Case ID before insert: ' + existCMX.Case_ID__c);

   // insert CareMetx data
   insert existCMX;
  
   // retrieve the new CareMetx data
   existCMX = [SELECT Case_ID__c FROM CareMetx_Data__c WHERE Id =:existCMX.Id];
   System.debug('CareMetx case ID after trigger fired: ' + existCMX.Case_ID__c); 

   // **NEW CODE BLOCK UPDATES EXISTING CAREMETX RECORD**
   
   CareMetx_Data__c upCMX = new CareMetx_Data__c(Case_ID__c = '6279270');
   System.debug('Case ID before update: ' + upCMX.Case_ID__c);

   // insert CareMetx data
   insert upCMX;
  
   // retrieve the new CareMetx data
   upCMX = [SELECT Case_ID__c FROM CareMetx_Data__c WHERE Id =:upCMX.Id];
   System.debug('CareMetx case ID after trigger fired: ' + upCMX.Case_ID__c); 
    
   // update CareMetx data
   update upCMX;
}

}

2
  • If case ID is set as an external ID, you can use the data loader Upsert function to automatically insert or update based on the value. No code required!
    – sfdcfox
    Jan 25, 2023 at 22:19
  • There I go again making something so simple into something too complicated for me. Thanks for your support @sfdcfox!
    – Kotaro Sai
    Jan 25, 2023 at 23:03

1 Answer 1

0

I think you need to change your query to the following because CMXcaseID is a set:

List<CareMetx_Data__c> caseIDs = [SELECT Case_ID__c FROM CareMetx_Data__c WHERE Case_ID__c IN : CMXcaseID];

4
  • I appreciate your feedback Aana! I changed it to the below code but still looks like it isn't returning any values: // get existing CMX data case IDs Set <String> existingCMXcaseIDs = new Set<String>(); List <CareMetx_Data__c> existingCMXdatas = [SELECT Case_ID__c FROM CareMetx_Data__c WHERE Case_ID__c IN: CMXcaseIDs];{ for(CareMetx_Data__c existingCMXdata:existingCMXdatas) existingCMXcaseIDs.add(existingCMXdata.Case_ID__c);
    – Kotaro Sai
    Jan 25, 2023 at 22:59
  • Could it be that there are not any matches? Maybe add a System.debug(CMXcaseID) before selecting the matching case id's? Jan 25, 2023 at 23:21
  • The 6279270 Case_ID__c is a case ID that is in our sandbox, however it is not returning the value in the code block that gets existing case IDs unfortunately.
    – Kotaro Sai
    Jan 25, 2023 at 23:39
  • Salesforce ID's are either a 15-character case-senistive string, or an 18-character case-insensitive string. So, this can't actually be an ID in the ID field on the CASE record. Take a look at the following help page to find the ID. help.salesforce.com/s/articleView?id=000385008&type=1 Jan 26, 2023 at 16:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .