使用SOQL查询无法获取Account Applicant_ID__c

时间:2016-08-02 09:46:54

标签: salesforce apex soql apex-trigger

我在帐户中有一个名为Applicant_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;
}  

}

提前致谢。

3 个答案:

答案 0 :(得分:2)

如果插入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;          
            }
        }   
    }       
}

答案 1 :(得分:1)

你回答错了 首先,你应该永远不要查询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;

答案 2 :(得分:-1)

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