SFDC二重押下防止


<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){

    }

}

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值