【问题标题】:using SOQL Query not able to fetch Account Applicant_ID__c使用 SOQL 查询无法获取帐户申请者_ID__c
【发布时间】:2016-08-02 09:46:53
【问题描述】:

我的帐户中有一个名为申请人_ID__c 的外部 ID。我正在使用数据加载器将数据导入 salesforce Opportunity。下面是我的映射文件内容。

Date\ Cancelled=Date_Cancelled__c
Date\ Denied=Date_Denied__c
Date\ Decisioned=Date_Decisioned__c
App\ Status=Application_Status__c
Date\ Submitted=Application_Submitted__c
Closing\ Date=CloseDate
Application\ Source=Application_Source__c
Application\ Type=Application_Type__c
Application\ Sub-Type=Application_Sub_Type__c
App\ ID=App_ID__c
Property=Property_Name__r\:Property_Code__c
Applicant\ ID=Account\:Applicant_ID__c
Record\ Type\ ID=RecordTypeId

上面的映射现在可以正常工作了,我想要的是从触发器中填充机会名称。 下面是我的触发内容

trigger MapStatusToStageBeforeOpportunintyCreation on Opportunity (before insert, before update) {

for (Opportunity o : Trigger.New){


Account acc = [Select LastName From Account Where Applicant_ID__c =:Account:Applicant_ID__c];

o.Name =acc.LastName;
}  

}

提前致谢。

【问题讨论】:

    标签: salesforce apex soql apex-trigger


    【解决方案1】:

    如果插入 101 个机会,您创建和排除的答案将会爆炸,但如果您想使用 Applicant_ID__c,您可以在帐户查询中查询它

    trigger MapStatusToStageBeforeOpportunintyCreation on Opportunity (before insert, before update) 
    {
        Set<ID> acctIDS = new Set<ID>();
    
        for (Opportunity o : Trigger.new)
        {  
            if(o.AccountId != null)
            {
                acctIDS.add(o.AccountID);
            }                
        } 
    
        Map<ID, Account> acctMap = new Map<ID, Account>([Select LastName, Applicant_ID__c  From Account Where ID =: acctIDS]); 
    
        for(Opportunity o : Trigger.new)
        {
            for(ID acctID :acctMap.keySet())
            {
                if(o.AccountID == acctID)
                {
                    o.Lastname = acctMap.get(acctID).LastName;          
                }
            }   
        }       
    }
    

    【讨论】:

    • 如果要使用同一个触发器上传超过5000条记录怎么办?
    • 请帮助我@EricSSH。
    • 你可以做 5000 个,它会在每个上下文中将其分块为 200 个
    • @TanveerAhmad 出了什么问题?
    • 我可以做 5000 条,但我想使用这个触发器上传超过一百万条记录。在这种情况下该怎么办?
    【解决方案2】:

    你查询错了 首先,永远不要在 for 循环中查询

    List'<'Opportuniy opplist = new list'<'Opportunity'>'();<Br/>
    // Remove the single quotes <br/>
    for (Opportunity o : Trigger.New){<Br/>
    o.OpportunityApplicentID = o.Account.Applicant_ID__c;<Br/>
    o.Name =acc.LastName;<Br/>
    opplist.add(o);<Br/>
    }
    update opplist;
    

    【讨论】:

    • 如您所见,我使用的是 before insert in trigger,因此当在 insert 之前创建 opp 对象时,无法直接访问 Account.Applicant_ID__c 等关系属性。您可以使用以下查询访问 Account 的属性。 Account acc = [Select LastName From Account Where id =:o.AccountId]; o.Name =acc.LastName;
    • 您必须通过绘制数据来解决该问题。请参阅上面 EricSSH 的答案。
    【解决方案3】:

    +不要使用Applicant_ID__c =:Account:Applicant_ID__c this.。只需使用Applicant_ID__c =:Account.Applicant_ID__c。不要使用冒号。使用点运算符

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      • 2022-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多