You need to sign in to do that
Don't have an account?
Rishant Kumar
Hi guys, I am write a trigger from opportunity line itme to find the total number of unique product count please help
trigger findingoppcount on OpportunityLineItem (after insert) {
List<Account> acc = new List<Account>();
Set<Id> oppId = new Set<Id>();
Map<Id,List<OpportunityLineItem>> Account_opplineitems = new Map<Id,List<OpportunityLineItem>> ();
for( OpportunityLineItem oppLine : Trigger.New ){
oppId.add( oppLine.OpportunityId );
system.debug('oppid'+oppId);
}
for( Opportunity opp : [ SELECT Id, AccountId,
( SELECT Id, Product2Id FROM OpportunityLineItems )
FROM Opportunity WHERE Id IN: oppId ] ){
for(OpportunityLineItem oli_obj :opp.OpportunityLineItems){
if(Account_opplineitems.containsKey(opp.AccountId) && Account_opplineitems.get(opp.AccountId) != null) {
List<OpportunityLineItem> lst_terr = Account_opplineitems.get(opp.AccountId);
lst_terr.add(oli_obj);
Account_opplineitems.put(opp.AccountId,lst_terr);
}
else {
Account_opplineitems.put(opp.AccountId, new List<OpportunityLineItem> {oli_obj});
}
}
}
system.debug('Account_opplineitems'+Account_opplineitems);
}
List<Account> acc = new List<Account>();
Set<Id> oppId = new Set<Id>();
Map<Id,List<OpportunityLineItem>> Account_opplineitems = new Map<Id,List<OpportunityLineItem>> ();
for( OpportunityLineItem oppLine : Trigger.New ){
oppId.add( oppLine.OpportunityId );
system.debug('oppid'+oppId);
}
for( Opportunity opp : [ SELECT Id, AccountId,
( SELECT Id, Product2Id FROM OpportunityLineItems )
FROM Opportunity WHERE Id IN: oppId ] ){
for(OpportunityLineItem oli_obj :opp.OpportunityLineItems){
if(Account_opplineitems.containsKey(opp.AccountId) && Account_opplineitems.get(opp.AccountId) != null) {
List<OpportunityLineItem> lst_terr = Account_opplineitems.get(opp.AccountId);
lst_terr.add(oli_obj);
Account_opplineitems.put(opp.AccountId,lst_terr);
}
else {
Account_opplineitems.put(opp.AccountId, new List<OpportunityLineItem> {oli_obj});
}
}
}
system.debug('Account_opplineitems'+Account_opplineitems);
}
trigger findingoppcount on OpportunityLineItem (after insert) {
try{
List<Account> acc = new List<Account>();
Set<Id> oppId = new Set<Id>();
Map<Id,List<OpportunityLineItem>> Account_opplineitems = new Map<Id,List<OpportunityLineItem>> ();
for( OpportunityLineItem oppLine : Trigger.New ){
oppId.add( oppLine.OpportunityId );
system.debug('oppid'+oppId);
}
for( Opportunity opp : [ SELECT Id, AccountId,
( SELECT Id, Product2Id FROM OpportunityLineItems )
FROM Opportunity WHERE Id IN: oppId And AccountId !=Null ] ){
//Add a null check for AccoundID not be Null
//And check opportunity has some OpportunityLineItems
if(opp.OpportunityLineItems.size()>0){
for(OpportunityLineItem oli_obj :opp.OpportunityLineItems){
if(Account_opplineitems.containsKey(opp.AccountId) && Account_opplineitems.get(opp.AccountId) != null) {
List<OpportunityLineItem> lst_terr = Account_opplineitems.get(opp.AccountId);
//flag to identify unique products by product2Id
boolean isUnique = true;
for(OpportunityLineItem oli :lst_terr){
if(oli.Product2Id == oli_obj.Product2Id){
isUnique = false;
}
}
if(isUnique == true){
lst_terr.add(oli_obj);
Account_opplineitems.put(opp.AccountId,lst_terr);
}
}
else {
Account_opplineitems.put(opp.AccountId, new List<OpportunityLineItem>());
}
}
}
}
for( Id varId : Account_opplineitems.keySet()){
system.debug('AccountId----->'+varId+' And Number of unique product----------->'+Account_opplineitems.get(varId).size());
}
}
catch(Exception ex){
system.debug('*****Error*****'+ex.getMessage()+'****Line Number ******'+ex.getLineNumber());
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Suraj Tripathi
sir i want to store a value it in a field
i make a field in account obj name count__c
Here is updated code:
trigger findingoppcount on OpportunityLineItem (after insert) {
try{
List<Account> acc = new List<Account>();
Set<Id> oppId = new Set<Id>();
Map<Id,List<OpportunityLineItem>> Account_opplineitems = new Map<Id,List<OpportunityLineItem>> ();
for( OpportunityLineItem oppLine : Trigger.New ){
oppId.add( oppLine.OpportunityId );
system.debug('oppid'+oppId);
}
for( Opportunity opp : [ SELECT Id, AccountId,
( SELECT Id, Product2Id FROM OpportunityLineItems )
FROM Opportunity WHERE Id IN: oppId And AccountId !=Null ] ){
//Add a null check for AccoundID not be Null
//And check opportunity has some OpportunityLineItems
if(opp.OpportunityLineItems.size()>0){
for(OpportunityLineItem oli_obj :opp.OpportunityLineItems){
if(Account_opplineitems.containsKey(opp.AccountId) && Account_opplineitems.get(opp.AccountId) != null) {
List<OpportunityLineItem> lst_terr = Account_opplineitems.get(opp.AccountId);
//flag to identify unique products by product2Id
boolean isUnique = true;
for(OpportunityLineItem oli :lst_terr){
if(oli.Product2Id == oli_obj.Product2Id){
isUnique = false;
}
}
if(isUnique == true){
lst_terr.add(oli_obj);
Account_opplineitems.put(opp.AccountId,lst_terr);
}
}
else {
Account_opplineitems.put(opp.AccountId, new List<OpportunityLineItem>());
}
}
}
}
List<Account> updateAccountList = [SELECT Id, count__c FROM Account WHERE Id IN :Account_opplineitems.keySet()];
for( Account accObj : updateAccountList){
if(Account_opplineitems.containsKey(accObj.id)){
accObj.count__c = Account_opplineitems.get(accObj.id).size();
}
}
system.debug('updateAccountList----->'+updateAccountList);
// update count field on Account object
update updateAccountList;
}
catch(Exception ex){
system.debug('*****Error*****'+ex.getMessage()+'****Line Number ******'+ex.getLineNumber());
}
}
I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.
Thanks and Regards,
Suraj Tripathi