+ Start a Discussion
Sri 7Sri 7 

i am getting this error at running the batch class First error: Duplicate id in list: a2v8E00000037ZgQAI

global class ZuoraProductRatePlanChargeHandlerBatch implements Database.Batchable<sObject>
    global Database.QueryLocator start(Database.BatchableContext BC){
        String query = 'select id,Invoice_Display_Name__c,(select id,Name,Subscription_Product_Charge__c,Subscription_Product_Charge__r.Zuora__SubscriptionRatePlan__c,Subscription_Product_Charge__r.Zuora__SubscriptionRatePlan__r.PRPC_Invoice_Display_Name__c from Product_Users__r where Subscription_Product_Charge__r.Zuora__SubscriptionRatePlan__r.PRPC_Invoice_Display_Name__c!=null) from zqu__ProductRatePlanCharge__c where Invoice_Display_Name__c!=null';
        return database.getQueryLocator(query);
    global void execute(Database.BatchableContext Bc, List<zqu__ProductRatePlanCharge__c> prpcList){
        Set<Zuora__SubscriptionRatePlan__c> srplist = new Set<Zuora__SubscriptionRatePlan__c>();
        for(zqu__ProductRatePlanCharge__c each:prpcList){
            if(each.Product_Users__r !=null){
                List<SPG_Product_User__c> tempUsers = each.Product_Users__r;
                for(SPG_Product_User__c eachUser:tempUsers){
                                Zuora__SubscriptionRatePlan__c tempSRP = new Zuora__SubscriptionRatePlan__c();
                                tempSRP.PRPC_Invoice_Display_Name__c = each.Invoice_Display_Name__c;
        if(srplist.size() >0 ){
            List<Zuora__SubscriptionRatePlan__c> tempList = new List<Zuora__SubscriptionRatePlan__c>();
            update tempList;
    global void finish(Database.BatchableContext BC){        
Khan AnasKhan Anas (Salesforce Developers) 
Hi Sri,

Greetings to you!

You are getting the Duplicate id in list error because you have a Duplicate id in your list.
The list won't give you a problem when you add a duplicate to the list, but it will give you a problem during run-time when it actually finds duplicates during an update to the list.

A Map won't allow for a duplicate to be entered, to begin with.

Lists can hold duplicate values, but if it contains duplicate sobject IDs and you try to update you'll get the error.
1. Create a map of  <id,sobject>
2. Convert the list to Map so that the duplicate IDs are removed.
3. Update the values part of the Map. 

Please refer to the below link which might help you further with the above issue.


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas