-1

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.

3 Answers3

2

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;          
            }
        }   
    }       
}
EricSSH
  • 447
  • 2
  • 12
  • What to do if want to upload more than 5000 records using the same trigger? – Tanveer Ahmad Aug 22 '16 at 11:46
  • Help me please @EricSSH. – Tanveer Ahmad Aug 22 '16 at 12:06
  • You can do 5000 it will chunk it to 200 per context – EricSSH Aug 22 '16 at 13:40
  • @TanveerAhmad is something breaking? – EricSSH Aug 22 '16 at 17:08
  • I am able to do 5000 but i want to upload more than a million records using this trigger. What to do in that situation? – Tanveer Ahmad Aug 23 '16 at 07:37
  • You can do as many as you want, it will chunk it into 200 in each context – EricSSH Aug 23 '16 at 14:56
  • Its working for 5000 records but giving error for more than 5000. Below is the error.2016-08-25 11:28:55,018 FATAL [main] process.ProcessRunner topLevelError (Proces sRunner.java:238) - Unable to run process accountUpsert java.lang.RuntimeException: java.lang.NullPointerException at com.salesforce.dataloader.process.ProcessRunner.run(ProcessRunner.jav a:162) at com.salesforce.dataloader.process.ProcessRunner.run(ProcessRunner.jav a:100) – Tanveer Ahmad Aug 25 '16 at 06:33
  • This is the error. `2016-08-25 11:28:55,018 FATAL [main] process.ProcessRunner topLevelError (ProcessRunner.java:238) - Unable to run process accountUpsert java.lang.RuntimeException: java.lang.NullPointerException at com.salesforce.dataloader.process.ProcessRunner.run(ProcessRunner.java:162) at com.salesforce.dataloader.process.ProcessRunner.run(ProcessRunner.java:100) at com.salesforce.dataloader.process.ProcessRunner.main(ProcessRunner.java:253) Caused by: java.lang.NullPointerException at java.lang.String$CaseInsensitiveComparator.compare(Unknown Source)` – Tanveer Ahmad Aug 25 '16 at 06:45
  • `at java.lang.String$CaseInsensitiveComparator.compare(Unknown Source) at java.util.TreeMap.put(Unknown Source) at java.util.TreeSet.add(Unknown Source) at java.util.AbstractCollection.addAll(Unknown Source) at java.util.TreeSet.addAll(Unknown Source) at com.salesforce.dataloader.mapping.Mapper.(Mapper.java:87) at com.salesforce.dataloader.mapping.LoadMapper.(LoadMapper.java:51) at com.salesforce.dataloader.controller.Controller.createMapper(Controll er.java:195) at com.salesforce.dataloader.process.ProcessRunner.run(ProcessRunner.java:146) ... 2 more` – Tanveer Ahmad Aug 25 '16 at 06:48
1

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;
Erik Godard
  • 5,930
  • 6
  • 30
  • 33
saikrishna
  • 11
  • 2
  • As you can see i am using before insert in trigger, So when an opp object is created before insert, the relationship attributes like Account.Applicant_ID__c can not be accessed directly. You can access the attributes of Account using following query. Account acc = [Select LastName From Account Where id =:o.AccountId]; o.Name =acc.LastName; – Tanveer Ahmad Aug 05 '16 at 07:09
  • You must work around the problem by mapping out your data. See EricSSH's answer above. – Aaron P. Aug 05 '16 at 22:48
-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

Ole Albers
  • 8,715
  • 10
  • 73
  • 166
Kirti Rathod
  • 1
  • 1
  • 2