You need to sign in to do that
Don't have an account?
Nitin sharma 425
Updating opp record working from User Interface but same code giving trouble when updating with data loaderr
Hi All,
I have a countofCEO__c field on the Account Object and Account has couple of Opportunties with Title__c field .After an opp is updated and Title__c field which had some value ealier is left blank the the field countofCEO__c field on the account object should subtract 1 from it and update account Object.....The below given code is working fine then I update an opp from the user interface and leave Title__c field blank then 1 is subttracted the field on the Account object .
However,when I try to do same thing from the Data loader the program is not working as desired.
The below given line is the culprit.When I remove Opp.title__c and only keep the line "if(opp.title__c!=trigger.oldmap.get(opp.Id).title__c)
and try to update couple of opp's through the data loader then it works fine.If I add if(opp.title__c==null) in addition to th above line then it does not work....I need to check for the Value in the Title__c field so if its null then then I need to update account object..
So what is wrong with the null value line when trying to update opps through Data Loader.?
if(opp.title__c==null && opp.title__c!=trigger.oldmap.get(opp.Id).title__c)
{
Trigger CountOfOppwithCEOTitle on Opportunity (After Update)
{
Set<id>Accids=new set<id>();
Map<id,opportunity>mapofOppIdtoOppRecords=new map<id,opportunity>();
for(opportunity opp:trigger.new)
{
if(opp.title__c==null && opp.title__c!=trigger.oldmap.get(opp.Id).title__c)
{
system.debug('I am in the initial for loop');
mapofOppIdtoOppRecords.put(opp.id,opp);
Accids.add(opp.accountid);
}
}
Map<id,Account>MapofAccountIdToAccount=new map<id,account>([select id,countofCEO__c from account where id in:Accids]);
List<opportunity>Opp=[Select id,title__c,Accountid from Opportunity where id in:mapofOppIdtoOppRecords.keyset()];
//select id,countofCEO__c,from account
//where id in:mapofAccIdtoOppRecords.keyset()];
list<account>acclist=new list<account>();
for(Opportunity Opprecords:opp)
{
system.debug('I am in for loop');
if(MapofAccountIdToAccount.containskey(opprecords.accountid)&& Opprecords.title__c==null)
{
system.debug('The debug value is');
Account Acc=MapofAccountIdToAccount.get(Opprecords.Accountid);
Acc.countofCEO__c=acc.countofCEO__c-1;
acclist.add(acc);
}
}
try
{
if(!acclist.isEmpty()&& acclist.size()>0)
{
Map<id,Account>MapofAccountIdToOpp=new map<id,account>();
MapofAccountIdToOpp.putall(acclist);
update MapofAccountIdToOpp.values();
}
}
catch(DmlException de)
{
}
}
I have a countofCEO__c field on the Account Object and Account has couple of Opportunties with Title__c field .After an opp is updated and Title__c field which had some value ealier is left blank the the field countofCEO__c field on the account object should subtract 1 from it and update account Object.....The below given code is working fine then I update an opp from the user interface and leave Title__c field blank then 1 is subttracted the field on the Account object .
However,when I try to do same thing from the Data loader the program is not working as desired.
The below given line is the culprit.When I remove Opp.title__c and only keep the line "if(opp.title__c!=trigger.oldmap.get(opp.Id).title__c)
and try to update couple of opp's through the data loader then it works fine.If I add if(opp.title__c==null) in addition to th above line then it does not work....I need to check for the Value in the Title__c field so if its null then then I need to update account object..
So what is wrong with the null value line when trying to update opps through Data Loader.?
if(opp.title__c==null && opp.title__c!=trigger.oldmap.get(opp.Id).title__c)
{
Trigger CountOfOppwithCEOTitle on Opportunity (After Update)
{
Set<id>Accids=new set<id>();
Map<id,opportunity>mapofOppIdtoOppRecords=new map<id,opportunity>();
for(opportunity opp:trigger.new)
{
if(opp.title__c==null && opp.title__c!=trigger.oldmap.get(opp.Id).title__c)
{
system.debug('I am in the initial for loop');
mapofOppIdtoOppRecords.put(opp.id,opp);
Accids.add(opp.accountid);
}
}
Map<id,Account>MapofAccountIdToAccount=new map<id,account>([select id,countofCEO__c from account where id in:Accids]);
List<opportunity>Opp=[Select id,title__c,Accountid from Opportunity where id in:mapofOppIdtoOppRecords.keyset()];
//select id,countofCEO__c,from account
//where id in:mapofAccIdtoOppRecords.keyset()];
list<account>acclist=new list<account>();
for(Opportunity Opprecords:opp)
{
system.debug('I am in for loop');
if(MapofAccountIdToAccount.containskey(opprecords.accountid)&& Opprecords.title__c==null)
{
system.debug('The debug value is');
Account Acc=MapofAccountIdToAccount.get(Opprecords.Accountid);
Acc.countofCEO__c=acc.countofCEO__c-1;
acclist.add(acc);
}
}
try
{
if(!acclist.isEmpty()&& acclist.size()>0)
{
Map<id,Account>MapofAccountIdToOpp=new map<id,account>();
MapofAccountIdToOpp.putall(acclist);
update MapofAccountIdToOpp.values();
}
}
catch(DmlException de)
{
}
}
Greetings!
This could be becuase of the number of records that you are trying to process here.So,I would suggest you to capture the debug logs and based on the error you can optimize the code.
Also,please make sure that you bulkify the trigger code to avoid issues while handling huge data.
https://force-base.com/2016/02/03/how-to-bulkify-trigger-in-salesforce-step-by-step-guide/
Please mark it as best answer if it helps you to fix the issue.
Thank you!
Regards,
Shirisha Pathuri
The code is bulkified so I guess that is not an issue.I was only trying to update 4 records at a time using data loader.
As per the Debug logs,control is not going inside If statemen.Hence,not able to capture Opportunity and Accountid and
if(opp.title__c==null && opp.title__c!=trigger.oldmap.get(opp.Id).title__c)
{
system.debug('I am in the initial for loop');
mapofOppIdtoOppRecords.put(opp.id,opp);
Accids.add(opp.accountid);
}
and then it executes the following lines since the the body of the If staement was skipped therefore,below lines returned zero records and that is
happening because of condition checking for null values.
Map<id,Account>MapofAccountIdToAccount=new map<id,account>([select id,countofCEO__c from account where id in:Accids]);
List<opportunity>Opp=[Select id,title__c,Accountid from Opportunity where id in:mapofOppIdtoOppRecords.keyset()];
Some changes as to the code.
1. You do not to query same Opportunity again which is already in context.
2. Try using opp.title__c == ' ' as well, since upating a record field to blank (NO VALUE) via data loader sets that field value to blank instead of NULL.
Please refer below code.
Thanks,
Ashvin
I am able to solve that issue with the Data Loader....
.Firstly,whenever we are dealing with records with the blank field value we need to change settings for the null value in the data loader.
Secondly,The code that you provided seems to be updating account records once for each opportunity.In my case I was checking for mutiple opp's per account and there can be mutiple account with mutiple opps .I have solved both the issues.