How to generate a s...
 
Notifications
Clear all

[Solved] How to generate a string with a list of skipped record ids

2 Posts
1 Users
0 Likes
357 Views
Posts: 205
 CWL
Admin
Issue starter
(@cwl)
Member
Joined: 11 years ago

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?

I am updating the child object field based on the value of another field in its parent record. So for each child record, I am looking for a matching parent record, if found updates the parent field, if not skips the child record by not adding to the collection. No I want to track the ids of those skipped records, which I want to keep adding to the string skippedRecords, so when the loop exits, I get a list of ids of those skipped records. Hope this is clear enough. 

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;

 

Issue Tags
1 Reply
Posts: 205
 CWL
Admin
Issue starter
(@cwl)
Member
Joined: 11 years ago

Ended up creating this:

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';
        }
    } 
}
Reply
Share: