I am facing problem in marking checkbox true. Below is my cases

Case1:(It is correct and working)

I need records which have duplicate names and do not have any opportunity or contact associated with them. If the names are same and they have any of the Opportunities or the contacts linked then mark them as duplicates. E.g; if there are 2 such records then mark any 1 of them as duplicate.

Case 2:(Wrong result)

Suppose, there are a total of 3 duplicate records(with the same names) out of which 1 record is having either opportunity or contact or even both associated with it and remaining 2 do not have any such relationship, then these should be marked as duplicate.

But, in my case it is marking one checkbox(duplicate__c) as true

List<Account> actLst = new List<Account>();
Set<String> userNames = new Set<String>();

List<Account> accList = [SELECT Name,Id  , Prospect_Code__c  , duplicate__C  FROM Account where Id Not
                           In (Select AccountId from Contact) And Id Not In (Select AccountId from Opportunity)];                        

for(Account account_i : accList)
     if(!userNames.contains(account_i.Name)) {

    } else {
        account_i.duplicate__c = true;
    system.debug('actLst >> ' + actLst);
    update actLst;

  • I don't really understand, what your problem is. Could you please rephrase your question? What is your task? What do you want to accomplish? And please do not use any abbreviations like "Cont" or "opp" in fluid text. That doesn't help anybody who wants to help you. People simply turn away, when they read something like that on SO. – Johannes Jun 25 at 12:34
  • I edited the problem please revert back if you find any solutions @Johannes – Palash Jun 25 at 12:54
  • Hi Palash, to be clear on your failing scenario: You have three accounts with the same name. One of those three accounts has a contact and / or an opportunity linked. The other two accounts do not have any contacts nor opportunities linked. And you would like to mark these two accounts as a duplicate by setting the accounts' field "duplicate__c" to true. Is that right? – Johannes Jun 25 at 13:11
  • Correct. But in my case it is returning 1 record(duplicate__c)= true. So i need tworecord to be true – Palash Jun 25 at 13:14
  • Ok. I'll just tell you what your SOQL-Statement does. Maybe you'll understand, why just one of those two records gets marked / updated: You fetch all accounts, that do not have a contact linked, and that do not have an opportunity linked. – Johannes Jun 25 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.