0

I am iterating over the values from a JSON response in an hourly job and putting those values into a map. From there I'd like to compare each map elements key to the Customer__c JobId field.

If the map's key already exists in Salesforce, then I want to remove that particular map key element from the map. When I use the remove() method with the map, it seems to be deleting all of the map elements.

How can I check for a duplicate value (Map key = Customer__c JOB Id), and insert only those map keys that don't already exist in Salesforce? Thank you in advance.

     map<string,Customer__c> rec2insert = new map <string,Customer__c>();
    map<string,Customer__c> rec2insert2 = new map <string,Customer__c>();

    List<Customer__c> conList1 = [Select Id, JOB_ID__c from Customer__c where JOB_ID__c != null];


 for(String s :scope){

        Customer__c cod = new Customer__c(); //stores fax details
        Customer__c cod2 = new Customer__c(); // to be updated
            //Second callout below
                cod = CustomerBuilder.getOutBoundRecords(s); 
                cod2 = cod;
                cod2.Name = cod.Name;
                cod2.JOB_ID__c = s;

                if(cod2.Start_Date__c != null ){    
                rec2insert.put(s,cod2);
                }     


}



for(Customer__c cc : conList1){

        if(rec2insert.size() > 0 && rec2insert.contains(cc.JOB_ID__c){ 
            rec2insert.remove(cc.JOB_ID__c);
            }
    Else{

        system.debug('NO Values for insert found');
        }

    }
    insert rec2insert.values();


   }
1

This code is almost unreadable. Please make an effort to use consistent, meaningful indentation and brace style, as well as consistent capitalization and code spacing. (A tool like Prettier can help you do this). It will be a huge benefit to you as well as to anyone else that you ask to review and interpret your code.

Below is what your code could look like if you simply used a reasonable code style and removed irrelevant, tautological elements.

The Map#remove method does in fact remove a single, specific element from the Map (Map#clear removes all entries). There is likely a different issue in your code, or a misunderstanding about what your collections contain at each step along the way.

{
    Map<String, Customer__c> rec2insert = new Map<String, Customer__c>();

    List<Customer__c> conList1 = [
        SELECT Id, JOB_ID__c 
        FROM Customer__c 
        WHERE JOB_ID__c != null
    ];


    for (String s : scope){
        Customer__c cod;

        // Second callout below
        cod = CustomerBuilder.getOutBoundRecords(s); 
        cod.JOB_ID__c = s;

        if(cod.Start_Date__c != null) {    
            rec2insert.put(s, cod);
        }
    }

    for(Customer__c cc : conList1) {
        if (rec2insert.contains(cc.JOB_ID__c) { 
            rec2insert.remove(cc.JOB_ID__c);
        }  
    }
    if (rec2insert.isEmpty()) {
        System.debug('No Values for insert found');
    }

    insert rec2insert.values();
}
| improve this answer | |
1

I may have a different understanding of what you're looking for than David above (although I do agree with his point about clean code), but if your objective is to insert only non-existing data, then couldn't you just invert your logic: if(!rec2insert.contains(cc.JOB_ID__c) {... and get the non-matching values from the map to be added to a new list that can then be inserted.

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.