function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdc G 9sfdc G 9 

Below class is batch apex i have stuck on this error ? can any one can help me .

Hi all,

global class batchprocessdesc implements Database.Batchable<sObject>
{

    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'select id from Account';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Account> accounts)
    {
        list<Entitlement> ents = [select AccountId, id, Product_Family__c, EndDate from Entitlement where AccountId IN :accounts ORDER BY AccountId,Product_Family__c,EndDate DESC];
        //Map<Id, Entitlement> pfmap= new Map<Id, Entitlement>(ents);
       
        map<Product_Family__c,EndDate> PFMAP = new map<Product_Family__c,EndDate>(ents); // Error: Compile Error: Invalid type: EndDate at line 15 column 74

        for (Entitlement objent: ents)
        {
          
           pfmap.put('Product_Family__c','EndDate');

        }
        
    } 
    global void finish(Database.BatchableContext BC)
    {
    }
}
Alexander TsitsuraAlexander Tsitsura
Hi Deepak,

In Apex you not have "EndDate" data type, maybe you want to use Date or DateTime type.

Thanks,
Alex
Rohit K SethiRohit K Sethi
Hi Deepak,

There is type as "End Date" that is why it raise an error.
So should use below code :
 
map<Product_Family__c,Date> PFMAP = new map<Product_Family__c,Date>();
for (Entitlement objent: ents)
{
   pfmap.put(ents.Product_Family__c,ents.EndDate);
}

Note :

I thing you are little bit confused from this syntax :
       Map<Id, Entitlement> pfmap= new Map<Id, Entitlement>(ents);
in Map creation time if we pass list as argument then it can only be ID and Object Type. We can't use any other field as value.
map<Product_Family__c,EndDate> PFMAP = new map<Product_Family__c,EndDate>(ents).

Thanks. 
Amit Chaudhary 8Amit Chaudhary 8
try to update your test class like below
global class batchprocessdesc implements Database.Batchable<sObject>
{

    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'select id from Account';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Account> accounts)
    {
		Set<ID> setAccID = new Set<ID>();
		for(Account acc : accounts )
		{
			setAccID.add(acc.id);
		}		
	
        list<Entitlement> ents = [select AccountId, id, Product_Family__c, EndDate from Entitlement where AccountId IN :setAccID ORDER BY AccountId,Product_Family__c,EndDate DESC];

       
        map<Product_Family__c,Date> pfmap = new map<Product_Family__c,Date>(); 
        for (Entitlement objent: ents)
        {
           pfmap.put(objent.Product_Family__c , objent.EndDate );
        }
        
    }
	
    global void finish(Database.BatchableContext BC)
    {
    }
	
}

Let us know if this will help you

Thanks
Amit Chaudhary
 
sfdc G 9sfdc G 9
Hi Amit ,

I tried this but its throwing error,


thanks , Deep