You need to sign in to do that
Don't have an account?
Sandy Shaw
Incompatible types: Asset, OpportunityLineItem
Hi,
I am writing a cross object trigger.
This is my first map: Map<Id,List<Asset>> acc_asset_map = new Map<Id,List<Asset>>();
for(Id k:account_id){
List<Asset> asset = new List<Asset>();
asset = [SELECT Product2Id FROM Asset WHERE AccountId = :k];
acc_asset_map.put(k,asset);
}
And this is the second map: Map<Id,List<OpportunityLineItem>> opp_prod_map = new Map<Id,List<OpportunityLineItem>>();
for (Id i:opp_id){
List<OpportunityLineItem> oppLineItem = new List<OpportunityLineItem>();
oppLineItem = [SELECT Product2Id FROM OpportunityLineItem WHERE OpportunityId = :i];
opp_prod_map.put(i,oppLineItem);
}
I want to compare the value of my two maps. So I have a for loop go trough a list of all my AccountId. Then I get all the OpportunityId under each AccountId. With each OpportunityId, I want to check if there is any same Product2Id under my Opportunity and my Asset. Though both of the value I put in my map are 'Product2Id', they are get from different Object types. One is from Asset, and the other is from OpportunityLineItem.
So am I on the reight direction if I want to check whether the account asset and the opporutnity under that account have the same product?
If so, is there anyway to fix this bug?
Thanks in advance!
I am writing a cross object trigger.
This is my first map: Map<Id,List<Asset>> acc_asset_map = new Map<Id,List<Asset>>();
for(Id k:account_id){
List<Asset> asset = new List<Asset>();
asset = [SELECT Product2Id FROM Asset WHERE AccountId = :k];
acc_asset_map.put(k,asset);
}
And this is the second map: Map<Id,List<OpportunityLineItem>> opp_prod_map = new Map<Id,List<OpportunityLineItem>>();
for (Id i:opp_id){
List<OpportunityLineItem> oppLineItem = new List<OpportunityLineItem>();
oppLineItem = [SELECT Product2Id FROM OpportunityLineItem WHERE OpportunityId = :i];
opp_prod_map.put(i,oppLineItem);
}
I want to compare the value of my two maps. So I have a for loop go trough a list of all my AccountId. Then I get all the OpportunityId under each AccountId. With each OpportunityId, I want to check if there is any same Product2Id under my Opportunity and my Asset. Though both of the value I put in my map are 'Product2Id', they are get from different Object types. One is from Asset, and the other is from OpportunityLineItem.
So am I on the reight direction if I want to check whether the account asset and the opporutnity under that account have the same product?
If so, is there anyway to fix this bug?
Thanks in advance!
Hi Sandy,
Below is code snippet which will work for you :) Please let me know in case of any issue.
Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
All Answers
First thing I would like to say is, you should avoid using SOQL query in FOR LOOP. Above code is using 2 SOQL query in FOR LOOP.
Regarding code, per my understanding, you want to check whether Asset and OpportunityLineItem contains the same product or not. As both objects have Product2Id, if it points to same product, it will return same value.
Here is the sample code which might help you. I have added few comments, you make changes as per your requirement:
-Thanks,
TK
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Hi Sandy,
Below is code snippet which will work for you :) Please let me know in case of any issue.
Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
Thanks for correcting the code. I missed that it is a Map of List. :)
-Thanks,
TK
Hi Tarun,
I understand :) No worry
Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
Thanks for the code snipet. But I have a question, for the opp_prod_map, every time I only put the (oli.Product2Id, oppLineItem_Recd) into the map, where the Product2Id is the key and the list is the value. Then how could "opp_prod_map.containsKey(oli.OpportunityId)" check whether this opportunitylineItem has been visited or not.