【Apex】取引開始をまとめて行う方法
Apexを使用して、リードから取引先責任者への移行(取引開始)をまとめて行うクラスのサンプルを作ってみました。
ただfor文の中でSOQL使用していたりするので、ガバナ制限には気を付けなければいけません。そこは注意してください。
// 取引開始メソッド public void Convert(){ // リード状況の「取引開始済み」に該当する値を取得(MasterLabel) LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted = true LIMIT 1 ]; // 取引開始していないリードを取得 Lead[] leads = [SELECT Id FROM Lead WHERE Status != :convertStatus.MasterLabel ]; // 取得したリードの数だけ取引開始を行う for(Lead lead : leads){ Database.LeadConvert lc = new Database.LeadConvert(); //取引開始済みリードオブジェクトをNew Account account = [SELECT Id FROM Account WHERE Name = :lead.Company LIMIT 1 ]; try{ lc.setLeadId(lead.Id); lc.setConvertedStatus(convertStatus.MasterLabel); // 取引先に同じ会社名がある場合は既存のレコードを使用 // 新しい取引先は作らない if(account != null){ lc.setAccountId(account.Id); } lc.setDoNotCreateOpportunity(True); // 商談オブジェクトを作成しない Database.LeadConvertResult lcr = Database.convertLead(lc); // 取引開始 }catch(DmlException e){ System.debug('エラー:' + e); } } }
参考