You need to sign in to do that
Don't have an account?
KR_Force
Batch Class Help?
Below is skipping the if and else if condition line 25 and 29. and updating the zero records. any help will be much appriciated?
Note::Product_Subscriptions__c is a multi pick list field
Note::Product_Subscriptions__c is a multi pick list field
- global class BatchAccountProductSubscriptions implements Database.Batchable<sobject>, Database.Stateful
- {
- public String query;
- global String error;
- global database.queryLocator start(Database.BatchableContext BC)
- { String query = 'select AccountId, Id, Product_Family__c FROM Asset where Product_Family__c!=null limit 200';
- return database.getQueryLocator(query);
- }
- global void execute(Database.BatchableContext BC, List<sObject> scope)
- {
- List<asset> assetList =(List<Asset>)scope;
- set<id>acIDs=new set<Id>();
- Map<Id,Set<String>> actPsubs = new Map<Id,Set<String>>();
- Map<id,string> accProdFamily = new Map<id,string>();
- Map<id,Asset> aidAssetMap = new Map<id,Asset>();
- List<Account> updateAccountList = new List<Account>();
- boolean isUpdate = false;
- try
- { System.Debug('assetList ****'+assetList);
- for(Asset a :assetList){aidAssetMap.put(a.AccountId,a);}
- list<Account> act =[select Id, Product_Subscriptions__c FROM account where id in:aidAssetMap.keyset()];
- for(Account ac:act){
- string prodstring ='';
- if(!ac.Product_Subscriptions__c.contains(aidAssetMap.get(ac.id).Product_Family__c) && (ac.Product_Subscriptions__c<>''|| ac.Product_Subscriptions__c!=null ))
- { System.Debug('Product_Subscriptions__c Match not Found ');
- prodstring=ac.Product_Subscriptions__c+';'+aidAssetMap.get(ac.id).Product_Family__c;
- }
- else if(!ac.Product_Subscriptions__c.contains(aidAssetMap.get(ac.id).Product_Family__c) && (ac.Product_Subscriptions__c=='' || ac.Product_Subscriptions__c==null))
- { System.Debug('Product_Subscriptions__c Match Found ');
- prodstring=aidAssetMap.get(ac.id).Product_Family__c;
- }
- updateAccountList.add(new Account(Id = ac.id, Product_Subscriptions__c = prodstring));
- }
- System.Debug('updateAccountList ****'+updateAccountList);
- if(updateAccountList.size() != 0){
- Database.SaveResult[] srList = Database.update(updateAccountList, false);
- for(Database.SaveResult sr : srList)
- {
- for(Database.Error e : sr.getErrors())
- error += '\n ' + e.getMessage();
- }
- }
- }
- catch(Exception e)
- {
- error += e.getMessage();
- }
- }
- global void finish(Database.BatchableContext BC)
- {
- AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email from AsyncApexJob where Id =: BC.getJobId()];
- Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
- String[] toEmailAddresses = new String[] {'XXXXXXXXXXXXXXXXXXXXX'};
- mail.setToAddresses(toEmailAddresses);
- mail.setSubject('BatchJob Status' + a.Status);
- mail.setHTMLBody
- ('Total No of Batch Apex job processed ' + a.TotalJobItems +
- ' batches with '+ a.NumberOfErrors + ' failures.');
- Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
- }
- }
{
string prodstring ='';
if((ac.Product_Subscriptions__c!=null && ac.Product_Subscriptions__c !='' ))
{
string productSub=ac.Product_Subscriptions__c;
if(!productSub.contains(aidAssetMap.get(ac.id).Product_Family__c) )
{.
System.Debug('Product_Subscriptions__c Match not Found ');
prodstring=productSub+';'+aidAssetMap.get(ac.id).Product_Family__c;
}
else
{
prodstring=aidAssetMap.get(ac.id).Product_Family__c;
}
}
else
{ System.Debug('Product_Subscriptions__c Match Found ');
prodstring=aidAssetMap.get(ac.id).Product_Family__c;
}
updateAccountList.add(new Account(Id = ac.id, Product_Subscriptions__c = prodstring));
}
I didn't paid attention to the first if statement it should be &&
Try it now.
All Answers
limit is just for testing purpose. i dont want the batch class execute for entire table fo size 600K.
answering to you next question: Yes im checking the null on account side not on Asset field(Product_Subscriptions__c is an Account field and Product_Family__c is on Asset)
String query = 'select AccountId, Account.Product_Subscriptions__c, Id, Product_Family__c FROM Asset where Product_Family__c!=null limit 200';
In your execute method now you can do a compare during the for loop repeat and dont really need maps. Also I am not sure if I understand your contains logic..
i dont want to add the duplicate values so im just checking if the value is already selected in multi pick or not. if it is already selected for that particular record then i dont want add it.
without map how are going to set the valu from asset to Account field? if its possiable without map then can you please share code snippet?
you need to use the map to put the accountid/Asset,
and this the code where you need to replace 23/34.
for(Account ac:act)
{
string prodstring ='';
if((ac.Product_Subscriptions__c!=null ||ac.Product_Subscriptions__c !='' )&& !ac.Product_Subscriptions__c.contains(aidAssetMap.get(ac.id).Product_Family__c) )
{ System.Debug('Product_Subscriptions__c Match not Found ');
prodstring=ac.Product_Subscriptions__c+';'+aidAssetMap.get(ac.id).Product_Family__c;
}
else if( ( ac.Product_Subscriptions__c==null || ac.Product_Subscriptions__c=='' ) )
{ System.Debug('Product_Subscriptions__c Match Found ');
prodstring=aidAssetMap.get(ac.id).Product_Family__c;
}
updateAccountList.add(new Account(Id = ac.id, Product_Subscriptions__c = prodstring));
}
you was checking if ac.Product_Subscriptions__c==null || ac.Product_Subscriptions__c=='' so no need to the other condition
Try it ,it should work
Good Luck
Unfortunately that didnt help , its still skipping the if condition.
the reson for checking does not contain in IF condition is to make to sure the .Product_Subscriptions__c(Multi picklist field) field does not have the same value to avoid duplicates in pick list.
System.NullPointerException: Attempt to de-reference a null object at line 25.
any suggestions ?
for(Account ac:act)
{
string prodstring ='';
if((ac.Product_Subscriptions__c!=null ||ac.Product_Subscriptions__c !='' ))
{
if(!ac.Product_Subscriptions__c.contains(aidAssetMap.get(ac.id).Product_Family__c) )
{.
System.Debug('Product_Subscriptions__c Match not Found ');
prodstring=ac.Product_Subscriptions__c+';'+aidAssetMap.get(ac.id).Product_Family__c;
}
}
else
{ System.Debug('Product_Subscriptions__c Match Found ');
prodstring=aidAssetMap.get(ac.id).Product_Family__c;
}
updateAccountList.add(new Account(Id = ac.id, Product_Subscriptions__c = prodstring));
}
for(Account ac:act)
{
string prodstring ='';
if((ac.Product_Subscriptions__c!=null ||ac.Product_Subscriptions__c !='' ))
{
if(!ac.Product_Subscriptions__c.contains(aidAssetMap.get(ac.id).Product_Family__c) )
{.
System.Debug('Product_Subscriptions__c Match not Found ');
prodstring=ac.Product_Subscriptions__c+';'+aidAssetMap.get(ac.id).Product_Family__c;
}
else
{
prodstring=aidAssetMap.get(ac.id).Product_Family__c;
}
}
else
{ System.Debug('Product_Subscriptions__c Match Found ');
prodstring=aidAssetMap.get(ac.id).Product_Family__c;
}
updateAccountList.add(new Account(Id = ac.id, Product_Subscriptions__c = prodstring));
}
it should work
System.NullPointerException: Attempt to de-reference a null object at this line "if(!ac.Product_Subscriptions__c.contains(aidAssetMap.get(ac.id).Product_Family__c) )"
{
string prodstring ='';
if((ac.Product_Subscriptions__c!=null && ac.Product_Subscriptions__c !='' ))
{
string productSub=ac.Product_Subscriptions__c;
if(!productSub.contains(aidAssetMap.get(ac.id).Product_Family__c) )
{.
System.Debug('Product_Subscriptions__c Match not Found ');
prodstring=productSub+';'+aidAssetMap.get(ac.id).Product_Family__c;
}
else
{
prodstring=aidAssetMap.get(ac.id).Product_Family__c;
}
}
else
{ System.Debug('Product_Subscriptions__c Match Found ');
prodstring=aidAssetMap.get(ac.id).Product_Family__c;
}
updateAccountList.add(new Account(Id = ac.id, Product_Subscriptions__c = prodstring));
}
I didn't paid attention to the first if statement it should be &&
Try it now.