• sami31
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 3
    Replies
HI,
Address & Call (Child obj) have a lokup relation and i would like to udpate parent address field with value in Call_Date field of the second call on that address, we use Call_Date to determine the order of the call. I tried this with a batch job and queried all calls ordered by Call_Date in start method and did the update logic in execute(). But it is updating only few address records ignoring most of them. I think its because records returned by query are divided into batches of 200 records and they are executed independently (order is not guaranteed). Due to this 2 calls (on the same address) got split into different batches and they are processed as single call in the execute(), so the address is not updated with the Call_Date of 2nd call. 

How can we correct this? Is there a better solution to achieve this requirement.

Thanks.
  • April 11, 2018
  • Like
  • 0
Hi,
There are 2 objects Account &Address and the relationship is master detial. In address there is a checkboox called primary and most of the account will have 1 address which is marked as primary. I want to know the accounts which don't have a primary address at all. For example there are 2 accounts A & B. Account A has 5 addresses and none of them have primary checked and Account B has 5 address in which 1 is primary and the other 4 non primary. So when a run a report saying primary=false i am getting both accounts A(5 records for each non primary address) & B(4 records for each non primary address) instead of only account A. This is because B has 4 addresses which are not primary and report is pulling them.
Is there any different way or query where i can get only account wiht no primary address which means by our example only account A should come out but not acount B.
Thanks.
HI I created a batch apex but my test class is not covering the entire code, not sure where i am going wrong. Looks like query in start method is not returning any test records which i created. Please shed some light on where i am going wrong.

Batch class:
global class updatePrimaryZipTerritory implements Database.Batchable<sobject>{
    public String query='select id,Zip_vod__c,INDV_CL_Territory__c,Country_vod__c from Address_vod__c where Primary_vod__c=TRUE AND Country_vod__c=\'us\'';
    global Database.QueryLocator start(Database.BatchableContext BC){
        List<Address_vod__c> ad=[select id,Zip_vod__c,INDV_CL_Territory__c,Country_vod__c from Address_vod__c where Primary_vod__c=TRUE AND Country_vod__c='us'];
        System.debug('****'+ad.size());
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<sobject> scope){
        List<Address_vod__c> addrList=(List<Address_vod__c>)scope;
        System.debug('Query List'+addrList);
        List<Address_vod__c> updateTerr=new List<Address_vod__c>();
        Set<String> zipIds=new Set<String>();
        for(Address_vod__c ad:addrList){
            if(ad.Zip_vod__c!='')
            zipIds.add(ad.Zip_vod__c);
        }
        System.debug('Zip Codes'+zipIds+'Size'+zipIds.size());
           Map<String,String> zipTerr=new Map<String,String>();
        for(INDV_Zip_To_Territory__c zip:[select Name,INDV_Territory_Id__c from INDV_Zip_To_Territory__c where Name in: zipIds]){
                        zipTerr.put(zip.Name,zip.INDV_Territory_Id__c);
        }
        System.debug('Map of Zip Terr'+zipTerr+'Size'+zipTerr.size());
        if(!zipTerr.isEmpty()){
        for(Address_vod__c add:addrList){
            if(zipTerr.containsKey(add.Zip_vod__c)){
                if(String.isBlank(add.INDV_CL_Territory__c) || zipTerr.get(add.Zip_vod__c)!=add.INDV_CL_Territory__c){
                    add.INDV_CL_Territory__c=zipTerr.get(add.Zip_vod__c);
                    updateTerr.add(add);
                }
            }                
        }
        }    
        System.debug('Update Territory List'+updateTerr+updateTerr.size());
        if(!updateTerr.isEmpty()){
            update updateTerr;
        }
    }
    global void finish(Database.BatchableContext BC){
   
    }
}

Test Class:
@isTest
private class updatePrimaryZipTerritoryTest {
   static testmethod void updateTerritoryTestMethod(){
        List<INDV_Zip_To_Territory__c> zip_terr=new List<INDV_Zip_To_Territory__c>();
        List<Address_vod__c> addrList=new List<Address_vod__c>();
           Account acc=new Account(lastName='Test Batch');
        insert acc;
       List<String> zip=new List<String>{'00680', '00681', '00682', '00683', '00684', '00685', '00686', '00687', '00688', '00689'};
        for(integer i=0;i<10;i++){
            addrList.add(new Address_vod__c(Account_vod__c=acc.id,Name='test'+i,Primary_vod__c=TRUE,Country_vod__c='us',city_vod__c='test',State_vod__c='VA',Zip_vod__c=zip[i]));
            zip_terr.add(new INDV_Zip_To_Territory__c(Name=zip[i],INDV_Territory_Id__c='29999999'));
        }
        insert addrList;
         insert zip_terr;
        
        system.debug('Before Address+++'+addrList);    
        Test.startTest();
        updatePrimaryZipTerritory cb=new updatePrimaryZipTerritory();
        Id batchId = Database.executeBatch(cb);
        Test.stopTest();

        system.debug('After Address+++'+addrList);
        System.assertEquals(7,[select count() from Address_vod__c where INDV_CL_Territory__c!='']);
    }
}

Thanks
 
Hi,
There are 2 objects Account &Address and the relationship is master detial. In address there is a checkboox called primary and most of the account will have 1 address which is marked as primary. I want to know the accounts which don't have a primary address at all. For example there are 2 accounts A & B. Account A has 5 addresses and none of them have primary checked and Account B has 5 address in which 1 is primary and the other 4 non primary. So when a run a report saying primary=false i am getting both accounts A(5 records for each non primary address) & B(4 records for each non primary address) instead of only account A. This is because B has 4 addresses which are not primary and report is pulling them.
Is there any different way or query where i can get only account wiht no primary address which means by our example only account A should come out but not acount B.
Thanks.
HI I created a batch apex but my test class is not covering the entire code, not sure where i am going wrong. Looks like query in start method is not returning any test records which i created. Please shed some light on where i am going wrong.

Batch class:
global class updatePrimaryZipTerritory implements Database.Batchable<sobject>{
    public String query='select id,Zip_vod__c,INDV_CL_Territory__c,Country_vod__c from Address_vod__c where Primary_vod__c=TRUE AND Country_vod__c=\'us\'';
    global Database.QueryLocator start(Database.BatchableContext BC){
        List<Address_vod__c> ad=[select id,Zip_vod__c,INDV_CL_Territory__c,Country_vod__c from Address_vod__c where Primary_vod__c=TRUE AND Country_vod__c='us'];
        System.debug('****'+ad.size());
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<sobject> scope){
        List<Address_vod__c> addrList=(List<Address_vod__c>)scope;
        System.debug('Query List'+addrList);
        List<Address_vod__c> updateTerr=new List<Address_vod__c>();
        Set<String> zipIds=new Set<String>();
        for(Address_vod__c ad:addrList){
            if(ad.Zip_vod__c!='')
            zipIds.add(ad.Zip_vod__c);
        }
        System.debug('Zip Codes'+zipIds+'Size'+zipIds.size());
           Map<String,String> zipTerr=new Map<String,String>();
        for(INDV_Zip_To_Territory__c zip:[select Name,INDV_Territory_Id__c from INDV_Zip_To_Territory__c where Name in: zipIds]){
                        zipTerr.put(zip.Name,zip.INDV_Territory_Id__c);
        }
        System.debug('Map of Zip Terr'+zipTerr+'Size'+zipTerr.size());
        if(!zipTerr.isEmpty()){
        for(Address_vod__c add:addrList){
            if(zipTerr.containsKey(add.Zip_vod__c)){
                if(String.isBlank(add.INDV_CL_Territory__c) || zipTerr.get(add.Zip_vod__c)!=add.INDV_CL_Territory__c){
                    add.INDV_CL_Territory__c=zipTerr.get(add.Zip_vod__c);
                    updateTerr.add(add);
                }
            }                
        }
        }    
        System.debug('Update Territory List'+updateTerr+updateTerr.size());
        if(!updateTerr.isEmpty()){
            update updateTerr;
        }
    }
    global void finish(Database.BatchableContext BC){
   
    }
}

Test Class:
@isTest
private class updatePrimaryZipTerritoryTest {
   static testmethod void updateTerritoryTestMethod(){
        List<INDV_Zip_To_Territory__c> zip_terr=new List<INDV_Zip_To_Territory__c>();
        List<Address_vod__c> addrList=new List<Address_vod__c>();
           Account acc=new Account(lastName='Test Batch');
        insert acc;
       List<String> zip=new List<String>{'00680', '00681', '00682', '00683', '00684', '00685', '00686', '00687', '00688', '00689'};
        for(integer i=0;i<10;i++){
            addrList.add(new Address_vod__c(Account_vod__c=acc.id,Name='test'+i,Primary_vod__c=TRUE,Country_vod__c='us',city_vod__c='test',State_vod__c='VA',Zip_vod__c=zip[i]));
            zip_terr.add(new INDV_Zip_To_Territory__c(Name=zip[i],INDV_Territory_Id__c='29999999'));
        }
        insert addrList;
         insert zip_terr;
        
        system.debug('Before Address+++'+addrList);    
        Test.startTest();
        updatePrimaryZipTerritory cb=new updatePrimaryZipTerritory();
        Id batchId = Database.executeBatch(cb);
        Test.stopTest();

        system.debug('After Address+++'+addrList);
        System.assertEquals(7,[select count() from Address_vod__c where INDV_CL_Territory__c!='']);
    }
}

Thanks