You need to sign in to do that
Don't have an account?
Vigneshwaran Loganathan
Need help for following Trigger
I have given with a situation, In Opportunity whenever the Status Goes 'Closed won' need to write Trigger like, Opportunity line item fields ( Name, Productname, price, Quantity, Account ) should be copied to ASSET ( Which is in product Object ) as new RECORD. Asset having three fiels Asset name ( Product name ), Account and Contact. Here How to get CONTACT ID from Opportunity.
I gave Trigger below, its working and I can save records in opportunity but datas not copied to ASSET ( Which is in Product Object )
trigger Demo on Opportunity (after insert, after update)
{
List<opportunitylineitem> opp1 = new List <opportunitylineitem>();
Map<string,string> product = new map<string,string>();
List<product2> product1 = new List<product2>();
List<product2> updateproduct1 = new List<product2>();
//List<Product2> products = new List<Product2>();
List<Asset> ast = new List<Asset>();
for(Opportunity opp:Trigger.new)
{
if(Trigger.isUpdate){
Opportunity oldOpp = trigger.oldMap.get(opp.Id);
if(opp.StageName == 'Closed Won' && opp.StageName != oldOpp.StageName)
{
opp1 = [select id,name,ProductCode,Quantity from opportunitylineitem where Opportunityid = :opp.id];
//product2 products = [select id, Name from Product2 where id=:opp1.id];
for(opportunitylineitem op : opp1)
{
List<string> productcode = new List<string>();
string value =' ';
productcode.add(op.productcode);
product.put(op.productcode,op.id);
product1 = [select id,name from Product2 where productcode in : productcode];
Asset a= new Asset();
for(Product2 prodnew:product1)
{
string ss1 = prodnew.productcode;
if(product.containskey(ss1))
{
value = product.get(ss1);
// system.debug(logginglevel.ERROR,'%%%% Value: ' + value);
}
}
a.Name = value;
a.Quantity = op.Quantity;
// system.debug(logginglevel.ERROR,'%%%% Asset Name/Quantity: ' + a.Name + ' ' + a.Quantity);
ast.add(a);
}
}
}
}
insert ast;
}
I gave Trigger below, its working and I can save records in opportunity but datas not copied to ASSET ( Which is in Product Object )
trigger Demo on Opportunity (after insert, after update)
{
List<opportunitylineitem> opp1 = new List <opportunitylineitem>();
Map<string,string> product = new map<string,string>();
List<product2> product1 = new List<product2>();
List<product2> updateproduct1 = new List<product2>();
//List<Product2> products = new List<Product2>();
List<Asset> ast = new List<Asset>();
for(Opportunity opp:Trigger.new)
{
if(Trigger.isUpdate){
Opportunity oldOpp = trigger.oldMap.get(opp.Id);
if(opp.StageName == 'Closed Won' && opp.StageName != oldOpp.StageName)
{
opp1 = [select id,name,ProductCode,Quantity from opportunitylineitem where Opportunityid = :opp.id];
//product2 products = [select id, Name from Product2 where id=:opp1.id];
for(opportunitylineitem op : opp1)
{
List<string> productcode = new List<string>();
string value =' ';
productcode.add(op.productcode);
product.put(op.productcode,op.id);
product1 = [select id,name from Product2 where productcode in : productcode];
Asset a= new Asset();
for(Product2 prodnew:product1)
{
string ss1 = prodnew.productcode;
if(product.containskey(ss1))
{
value = product.get(ss1);
// system.debug(logginglevel.ERROR,'%%%% Value: ' + value);
}
}
a.Name = value;
a.Quantity = op.Quantity;
// system.debug(logginglevel.ERROR,'%%%% Asset Name/Quantity: ' + a.Name + ' ' + a.Quantity);
ast.add(a);
}
}
}
}
insert ast;
}
Ashishyadav
because for creating Asset you need to madatory fields Name and Account or contact, and I see neither you have passed account or contact.Pass atleast one. you can easily pass Account