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
K_devK_dev 

I want to convert a custom object to opportunity and line item please help in modifying it.


trigger CreateopportunityonConverted on calllist__c (after insert, after update) {
list<string> callids=new list<String>();
for(callist__c o: trigger.new){
if(o.status__c == converted && o.converted_to_Opp == false){
callids.add(o.id);
}
}
if(!callids.IsEmpty())


calllist object fields to be converted into opportunity and opportunity line items.There is a account and opportunity lookup.
While opportunity creation make close date to today and stage to new.There is a secondary owner field which must be filled with calllist record owner in convertion.
{
calllist[] OLI = [Select UnitPrice, Quantity,OpportunityId, PricebookEntry.Product2Id, PricebookEntry.Product2.Name, Description, Converted_to_opp__c
From OpportunityLineItem
where OpportunityId IN:oppIds and Converted_to_opp__c = false];
Asset[] ast = new Asset[]{};
Asset a = new Asset();
for(OpportunityLineItem ol: OLI){
a = new Asset();
a.AccountId = Trigger.NewMap.get(ol.OpportunityId).AccountId;
a.Product2Id = ol.PricebookEntry.Product2Id;
a.Quantity = ol.Quantity;
a.Price = ol.UnitPrice;
a.PurchaseDate = Trigger.NewMap.get(ol.OpportunityId).CloseDate;

a.Status = 'Purchased';
a.Description = ol.Description;
a.Name = ol.PricebookEntry.Product2.Name;
ast.add(a);
ol.Converted_to_Assets__c = true;
}

update OLI;
insert ast;

}

 

Thanks in advance