Reputation: 51
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.
Upvotes: -1
Views: 772
Reputation: 447
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;
}
}
}
}
Upvotes: 2
Reputation: 1
+Instead of using Applicant_ID__c =:Account:Applicant_ID__c this.
.Just use Applicant_ID__c =:Account.Applicant_ID__c
.Don't use colon .Use dot operator
Upvotes: -1
Reputation: 11
You are querying it wrong First, you should Never Query in for loop
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;
Upvotes: 1