<apex:page standardController="Account" extensions="myCls" lightningStylesheets="true">
<apex:form id="form">
<apex:actionFunction name="doClickBtn" action="{!doClick}" rerender="form"/>
<apex:messages id="msg"/>
<apex:pageBlock >
<apex:pageBlockButtons location="bottom">
<apex:commandButton id="btn" onClick="return doClickBtn();" action="{!execution}" value="运行" disabled="{!isRunFlag}" styleClass="slds-vf-button_brand" />
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
public with sharing class myCls {
public Boolean isRunFlag {get;set;}
public Id dealerId{get;set;}
public myCls(ApexPages.StandardController stdController) {
dealerId = stdController.getRecord().Id;
isRunFlag = false;
}
/**
* 実行ボタン押下
*/
public PageReference execution(){
try {
myBatch batch = new myBatch (dealerId);
Database.executeBatch(batch, 200);
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, '已运行!'));
isRunFlag = true;
return null;
} catch (Exception ex) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, '运行失败,请联系系统管理员。错误信息:'+ex.getMessage()));
return null;
}
}
public PageReference doClick(){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, '正在运行中,请稍等。。。'));
isRunFlag = true;
return null;
}
}
public with sharing class permissionResetBatch implements Database.Batchable<sObject>, Database.Stateful{
Id accId;
public AccountPermissionResetBatch(Id accId) {
this.accId = accId;
}
public Database.Querylocator start(Database.BatchableContext BC){
String soql = '';
soql += ' select';
soql += ' AccountAgency__c,';
soql += ' AccountUser__c';
soql += ' from';
soql += ' XXXX__c';
if(String.isNotBlank(this.accId)){
soql += ' where';
soql += ' AccountAgency__c = :accId';
}
system.debug('======soql====='+soql);
return Database.getQueryLocator(soql);
}
public void execute(Database.BatchableContext BC, List<XXXX__c> scope){
set<Id> dealerIdList = new set<Id>();
for(XXXX__c pcar :scope){
dealerIdList.add(pcar.AccountAgency__c);
}
/*
// 代理店ロール取得
List<User> agencyUserList = [SELECT Id, Name, AccountId, UserRoleId FROM User WHERE AccountId = : DealerId];
Set<Id> agentRoleSet = new Set<Id>();
for (User u : agencyUserList) {
agentRoleSet.add(u.UserRoleId);
}
*/
system.debug('===this.accId ==='+this.accId );
system.debug('===dealerIdList==='+dealerIdList.size());
Set<Id> agentRoleSet = new Set<Id>();
// 代理店ロール取得
List<UserRole> urList = [SELECT Name,ParentRoleId,PortalRole,PortalAccountId,PortalType
FROM UserRole
WHERE PortalAccountId IN :dealerIdList];
for (UserRole ur : urList) {
agentRoleSet.add(ur.ParentRoleId);
}
List<Group> groupList = [SELECT ID, RelatedId
FROM Group
WHERE RelatedId IN :agentRoleSet];
Map<Id,Id> accIdGroupIdMap = new Map<Id,Id>();
for(UserRole u :urList){
for(Group g :groupList){
if(u.ParentRoleId == g.RelatedId){
accIdGroupIdMap.put(u.PortalAccountId,g.Id);
}
}
}
system.debug('==accIdGroupIdMap===='+accIdGroupIdMap);
List<Accountshare> accShareList = new List<Accountshare>();
// 顧客レコード共有
Accountshare accshare;
for (XXXX__c pacr : scope) {
accshare = new Accountshare();
accshare.AccountId = pacr.AccountUser__c;
accshare.AccountAccessLevel = 'Edit';
accshare.ContactAccessLevel = 'None';
accShare.OpportunityAccessLevel = 'None';
accshare.RowCause = 'Manual';
accshare.UserOrGroupId = accIdGroupIdMap.get(pacr.AccountAgency__c);
accShareList.add(accshare);
}
system.debug('===========accShareList.size()==='+accShareList.size());
if (!accShareList.isEmpty()) {
insert accShareList;
}
}
public void finish(Database.BatchableContext BC){
}
}