Triggers 如何在帐户联系人角色中为没有重复项的帐户创建主联系人

Triggers 如何在帐户联系人角色中为没有重复项的帐户创建主联系人,triggers,apex,apex-trigger,Triggers,Apex,Apex Trigger,每当我尝试使用dataloader为单个帐户插入新的多个联系人时,应在帐户联系人角色对象中插入一个主联系人 如果有多个接触,我们应该考虑第一次接触,将被认为是第一次接触,而不应该插入,只有一个主接触应该在接触角色对象中。在下面的代码中,我尝试过,没有为帐户创建主联系人 trigger AccountContactRole on Contact(After insert,After Update){ list<AccountContactRole> acr=new list&l

每当我尝试使用dataloader为单个帐户插入新的多个联系人时,应在帐户联系人角色对象中插入一个主联系人

如果有多个接触,我们应该考虑第一次接触,将被认为是第一次接触,而不应该插入,只有一个主接触应该在接触角色对象中。在下面的代码中,我尝试过,没有为帐户创建主联系人

trigger AccountContactRole on Contact(After insert,After Update){
    list<AccountContactRole> acr=new list<AccountContactRole>();
    set<ID> getid=new set<ID>();
    Contact[] clist=trigger.new;
        for(Contact Con : clist) {
            if(con.AccountId!=Null) {
                getid.add(Con.AccountId);
            }
        }
        List<AccountContactRole> ac = [Select Id,AccountId,ContactId,IsPrimary from AccountContactRole Where AccountId in :getid and IsPrimary =True];
        set<ID>accountid=new set<Id>();
        for(AccountContactRole acv:ac) {
            accountid.add(acv.AccountId);
        }
        System.debug('Records on List are '+ac +' And Records are '+ac);
        for(Contact Cont: clist) {
            AccountcontactRole C = new AccountcontactRole();
            System.debug('Cont current value is'+Cont);     

            if(ac.isempty()) {
                if(getid.contains(Cont.AccountId)==false){
                C.contactId=Cont.Id;
                C.Role='Decision Maker';
                C.AccountId=Cont.AccountId;        
                C.Isprimary=true;
                acr.add(C);
            }
        }
    }

    Insert acr; 
在联系人上触发AccountContactRole(插入后,更新后){
list acr=新列表();
set getid=new set();
联系人[]clist=trigger.new;
用于(联系人:clist){
如果(con.AccountId!=Null){
getid.add(Con.AccountId);
}
}
List ac=[从AccountContactRole中选择Id、AccountId、ContactId、IsPrimary,其中AccountId位于:getid和IsPrimary=True];
setaccountid=新集合();
对于(AccountContactRole acv:ac){
accountid.add(acv.accountid);
}
系统调试('列表上的记录为'+ac+',记录为'+ac+);
用于(联系方式:clist){
AccountcontactRole C=新AccountcontactRole();
系统调试('Cont当前值为'+Cont');
if(ac.isempty()){
if(getid.contains(Cont.AccountId)==false){
C.联系人Id=联系人Id;
C.角色&决策者;
C.AccountId=Cont.AccountId;
C.Isprimary=true;
acr.添加(C);
}
}
}
插入acr;