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
ColbridgeColbridge 

How to generate a string with a list of skipped record ids

How to generate a string with a list of skipped record ids.
Here I am skipping the records for which there is no matching count__c. 
I need to concatinate a string (skippedRecords) with ids of those skipped records. How would I achive this?
 
List<Parent__c> parentList = [SELECT Id, Name,downloads__c  FROM Parent__c 
    WHERE count__c IN :countList];

List<Data__c > datatoupdate = new List<Data__c >();

String skippedRecords = '';

    for(Data__c dRecords : dList) { 
        for(Parent__c parentRecords : parentList) {
            if(dRecords.count__c  == parentRecords.count__c) {
		        dRecords.downloads__c  = parentRecords.downloads__c ;
		        datatoupdate.add(dRecords );
            }
    } 
} 
update  datatoupdate;

 
Best Answer chosen by Colbridge
ColbridgeColbridge
Hi Sai, Thanks, but it didn't work. I ended up with this. Not sure if its the ideal, but it worked:
 
String skippedRecords = '';
Integer checkedParentIds = 0;
Boolean idMatch = false;
Integer totalParentIds = parentList.size();

for(Data__c dRecords : dList) {
    idMatch = false;
    totalParentIds = parentList.size();
    checkedParentIds = 0;
    for(Parent__c parentRecs : parentList) {
        checkedParentIds++;
        if(dRecords.count__c  == parentRecs.count__c) {
            dRecords.downloads__c  = parentRecords.downloads__c;
            ddRecsToInsert.add(dRecords);
            idMatch = true;
            break;
        } else if ((checkedParentIds == totalParentIds) && (idMatch == false)) {
            skippedRecords += dRecords.count__c + '\n';
        }
    } 
}

 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Colbridge,

Please find the below piece of code to add the records which are getting skipped by adding to the string.
 
List<Data__c > datatoupdate = new List<Data__c >();
String skippedRecords = '';
for(Data__c dRecords : dList) { 
    for(Parent__c parentRecords : parentList) {
        if(dRecords.count__c  == parentRecords.count__c) {
            dRecords.downloads__c  = parentRecords.downloads__c ;
            datatoupdate.add(dRecords );
        }else{
            if(skippedRecords = ''){
                skippedRecords = dRecords.Id;
            }else{
                skippedRecords = ','+skippedRecords+dRecords.Id;
            }
            
        }
} 
}
update  datatoupdate;

if this solution helps, Please mark it as best answer.

Thanks,
ColbridgeColbridge
Hi Sai, Thanks, but it didn't work. I ended up with this. Not sure if its the ideal, but it worked:
 
String skippedRecords = '';
Integer checkedParentIds = 0;
Boolean idMatch = false;
Integer totalParentIds = parentList.size();

for(Data__c dRecords : dList) {
    idMatch = false;
    totalParentIds = parentList.size();
    checkedParentIds = 0;
    for(Parent__c parentRecs : parentList) {
        checkedParentIds++;
        if(dRecords.count__c  == parentRecs.count__c) {
            dRecords.downloads__c  = parentRecords.downloads__c;
            ddRecsToInsert.add(dRecords);
            idMatch = true;
            break;
        } else if ((checkedParentIds == totalParentIds) && (idMatch == false)) {
            skippedRecords += dRecords.count__c + '\n';
        }
    } 
}

 
This was selected as the best answer