I am working on a batch class and I was having some code to push into scratch org to test. But it is giving me duplicate variable error while pushing the code. After this, I tried to copy the code and directly pasted into scratch org and saved it without any error. Below is the code for the Batch Class, I had highlighted the line which is having the variable declared with the same name. But the second one is in a different scope. The code is working perfectly in scratch org but I am not able to push it using the SALESFORCE CLI
global with sharing class ThisIsBatchClass implements Database.Batchable<SObject>{
global Database.querylocator start(Database.BatchableContext BC){
return Database.getQueryLocator('SELECT Id FROM Account LIMIT 10);
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
Map<Id,sObject> newVersionMap = new Map<Id,sObject>();
sObject[] newDistis = new sObject[]{};//Here I have declared the variable
for(Account p : (Account[]) scope ){
Pagereference pdf = Page.PDFController;
pdf.getParameters().put('Id',p.Id);
pdf.setRedirect(true);
if(!Test.isRunningTest()){
Blob pdfData = pdf.getContentasPdf();
sObject newVersion = createObject('ContentVersion');
newVersion.put('Title', 'Account_Summary_'+String.valueOf(System.Today()));
newVersion.put('Description','Invoice for '+p.Name);
newVersion.put('PathOnClient', 'Account_Summary_'+String.valueOf(System.Today())+'.pdf');
newVersion.put('VersionData', pdfData);
newVersion.put('Origin', 'H');
newVersionMap.put(p.Id, newVersion);
}
}
insert newVersionMap.values();
if(!newVersionMap.isEmpty()){
sObject[] newDistis = new sObject[]{};//Now this variable is declare again, which is in scope of If statement
for(Id k: newVersionMap.keyset()){
sObject c = newVersionMap.get(k);
sObject newDisti = createObject('ContentDistribution');
newDisti.put('ContentVersionId',(Id)c.get('Id'));
newDisti.put('Name',(String)c.get('Title'));
newDisti.put('RelatedRecordId',k);
newDisti.put('PreferencesAllowPDFDownload',true);
newDisti.put('PreferencesAllowOriginalDownload',false);
newDisti.put('PreferencesAllowOriginalDownload',false);
newDisti.put('PreferencesLinkLatestVersion',true);
newDisti.put('PreferencesAllowViewInBrowser',true);
newDistis.add(newDisti);
}
insert newDistis;
}
}
global void finish(Database.BatchableContext BC){
}
public static sObject createObject(String typeName) {
Schema.SObjectType targetType = Schema.getGlobalDescribe().get(typeName);
if (targetType == null) {
return null;
}
return targetType.newSObject();
}
}