0

リードの会社カスタム フィールドと同じ名前のアカウントが存在する場合に、現在のリードを既存のアカウントにリンクするルックアップ フィールドを自動入力する、リードに Salesforce トリガーを作成しようとしています。

これは私のコードです:

trigger Link_Lead_To_Account on Lead (before insert ) {

 Set<String> whatIDs = new Set<String>();
 MAP<id,String> accountMap= new MAP<id,String>();

 // save the leads that have been triggered
    for (Lead l : Trigger.new) { 
     whatIDs.add(l.id);      
    }

List<Lead> leads = [SELECT Id,Company FROM Lead where ID=:whatIDs ];

// loop through the triggered leads, if the account.name == to lead.company then link the found account to the lead
 for (Integer i = 0; i <Trigger.new.size(); i++)
{
// System.Debug('++++++++++++++'+Trigger.new[i].company+Trigger.new[i].id);
   if(accountMap.get(Trigger.new[i].company)!=null)
   { 
       for(Account ac :[Select name,id from Account])
       {
           if(Trigger.new[i].Company==ac.Name)
           { 
               Trigger.new[i].Account__c=  ac.id;
                break;
           }
       }

   }
//  System.Debug('Trigger.new[i].Account__c::::'+Trigger.new[i].Account__c);
//  System.Debug('Trigger.new[i].company:::::'+Trigger.new[i].company);
//  System.Debug('Trigger.new[i].ID:::::'+Trigger.new[i].ID);

}
update leads;   

}

しかし、それはまったく機能しません。次のエラーがスローされます。

Review all error messages below to correct your data.
Apex trigger Link_Lead_To_Account caused an unexpected exception, contact your administrator: Link_Lead_To_Account: execution of AfterInsert caused by: System.StringException: Invalid id: TestAccount2: External entry point

Company フィールドは ID である必要がありますが、ID を書き込んでも変更は行われません。

4

1 に答える 1

0

私はそれを修正することができました。これは、 newLeads.Values() がコンストラクターで挿入イベント前の Trigger.new() 値に取り込まれている作業クラスです。

public void LinkLeadToAccount() {

Set<String> companies = new Set<String>();
for (Lead l: newLeads.values()) {
    if (l.Company != null) companies.add(l.Company);
}

if (companies.size() > 0) {

    // Pick most recent Account where more than one with same name
    Map<String, Id> accountNameToId = new Map<String, Id>();
    for (Account a : [
            select Name, Id
            from Account
            where Name in :companies
            order by CreatedDate
            ]) {
        accountNameToId.put(a.Name, a.Id);
    }

    if (accountNameToId.size() > 0) {
        Lead[] updates = new Lead[] {};
        for (Lead l: newLeads.values()) {
            if (l.Company != null) {
                Id accountId = accountNameToId.get(l.Company);
                if (accountId != null) {
                    updates.add(new Lead(Id = l.Id, Account__c = accountId));
                }
            }
        }
        System.debug(' leads_to_update : ' + updates.size() + '   leads_to_update : ' +  updates);        
        update updates;
    }
}

}

于 2016-01-14T10:37:12.293 に答える