function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Lukesh KarmoreLukesh Karmore 

Hello,i need below functionality in trigger

I am writing trigger " when account is updated, add all releted OpportunityLineItem to account asset releted list"
i successfully write code that working fine ,but  whenever i update account 2nd time it insert duplicate asset.
if Accounts Opp contain 4 opportunityLineItem, Let say 3 asset already added to account releted list and 1 Oli (line item) is remaining so in 2nd update only that remaining 1 is added
only i need code as answer on above functionality ,no any other ans please Thank you.

trigger CreateAnAssetWithAllReletedOppLineItemReletedTOAccount_WhenAccountIsUpdated on Account (After Update) {
set<Id> AccIds=new set<Id>();
for(account acc:Trigger.new){
    AccIds.add(acc.Id);
}
list<Opportunity> opplist=[SELECT id,AccountId FROM Opportunity WHERE AccountId In:accIds];
list<OpportunityLineItem> OliList=[SELECT pricebookentry.name,pricebookentry.product2Id,quantity,Description,unitprice FROM OpportunityLineItem WHERE OpportunityId IN :opplist];
list<asset> asslist=new list<asset>();
for(Opportunity opp:opplist){
    for(OpportunityLineItem oli:OliList){
        Asset a=new Asset();
       a.AccountId=opp.AccountId;
       a.product2Id=oli.pricebookentry.product2Id;
       a.quantity=oli.quantity;
       a.price=oli.unitprice;
       a.Description=oli.Description;
       a.name=oli.pricebookentry.name;
       asslist.add(a);
    }
}
insert asslist;
}
PriyaPriya (Salesforce Developers) 

Hi Lukesh,

In Asset object, you are not setting any field which show this assest is related to any particular lineitem and you are performing 'insert' operation hence it is adding everytime new assest for every line item which is available. 

We suggest to create relationship field in Assest for OpportunityLineItem.

We suggest to define an external ID field on Assest which will be unique and required field, and it will strore the opportunitylineItem ID. And insted of insert use below statement :- 
 

upsert <Asset/ List<Asset>> <Externald Field Name >;

 

Please mark as Best Answer so that it can help others in the future.

Regards,

Priya Ranjan

 

 

Suraj Tripathi 47Suraj Tripathi 47
Hi Lukesh Karmore,
I think for an efficient solution you should Apply Trigger on opprtunityLineItem.That would more efficient way to do that.

Trigger:
trigger AsignAccountRealtedOpprtunityInAccountAsset on OpportunityLineItem (After insert) {
    if(trigger.isAfter && Trigger.isInsert){
        AccountRelatedOPLITriggerHandler.updateAccountRelatedOpli(Trigger.new);
    }
}

Apex Class:
public class AccountRelatedOPLITriggerHandler {
    public static void updateAccountRelatedOpli(List<OpportunityLineItem> opliList){
        Set<Id> opliIdSet = new Set<Id>();
        for(OpportunityLineItem opli:opliList){
            opliIdSet.add(opli.Id); 
        }
        List<OpportunityLineItem> opliList2 = new List<OpportunityLineItem>();
        opliList2 =[Select id ,Opportunity.AccountId, Opportunity.Id, Product2Id,quantity,unitprice from OpportunityLineItem where id in :opliIdSet];
        
        List<Asset> assetList = new List<Asset>();
        For(OpportunityLineItem opli : opliList2){
            if(opli.Opportunity.AccountId != null){
                Asset ast = new Asset();
                ast.AccountId = opli.Opportunity.AccountId;
                ast.Product2Id = opli.Product2Id;
                ast.Price = opli.UnitPrice;
                ast.Description = opli.Description;
                assetList.add(ast);
            }
        }
        if(!assetList.isEmpty()){
            upsert assetList;// insert assetList;  
        }
    }
}
And if you want still to run the trigger on the Account then repeatation may occur as you do not want to.If you have any query regarding this solution kindly reply me.

If you find your Solution then mark it as the best answer. 

Thanks and Regards
Suraj Tripathi.