Search code examples
salesforceapexsoqlapex-trigger

using SOQL Query not able to fetch Account Applicant_ID__c


I have an external id in Account named Applicant_ID__c. I am using data loader to import data into salesforce Opportunity. Below is my mapping file content.

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

The above mapping is working correctly now what i want is to populate the opportunity name from trigger. Below is my trigger content

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;
}  

}

Thanks in advance.


Solution

  • That answer you created and excepted is going to blow up if insert 101 Opportunities, but if you want to use the Applicant_ID__c you can query it out in the account query

    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;          
                }
            }   
        }       
    }