
Hi,
Can you please write a test class for this trigger, I don't know how to write the test class logic, Please help me out...
Trigger CreateorUpdateAsset on Contract_Product__c (before insert, before update) {
//List for insert and update
List<Asset> insertASList = new List<Asset>();
List<Asset> updateASList = new List<Asset>();
// To map the newly created asset back to the contract products
Map<Id,Id> assetIDMap =new Map<Id,Id>();
for(integer i=0;i<trigger.new.size();i++){
Contract_Product__c cp= trigger.new[i];
//Check for the Product_Master__c.Is_Serviceable__c is checked from the formula field
if((Trigger.isInsert && Trigger.isBefore && cp.Product_Master_Is_Serviceable_Fr__c=='True') || (Trigger.isUpdate && Trigger.isBefore && cp.Product_Master_Is_Serviceable_Fr__c=='True'&& cp.Asset__c==null)){
//create a new record on Asset
Asset newAsset = new Asset();
newAsset.Name= cp.Product_Name__c;
newAsset.Quantity=cp.Quantity__c;
newAsset.Contract_Product__c=
cp.id;newAsset.AccountId= cp.Account_Name__c;
insertAslist.add(newAsset);
}
//If it is an update - and if the old value is true - do nothing - else create AS
if(Trigger.isUpdate && Trigger.isBefore && cp.Product_Master_Is_Serviceable_Fr__c=='True'&&
trigger.old[i].Product_Master_Is_Serviceable_Fr__c=='False'){
//create a new record on Asset
Asset newAsset = new Asset(id =cp.Asset__c);
newAsset.Name= cp.Product_Name__c;
newAsset.Quantity=cp.Quantity__c;
newAsset.Contract_Product__c=
cp.id;newAsset.AccountId= cp.Account_Name__c;
updateASList.add(newAsset);
}
}
//Inserting the newly created asset
if(insertASList.size()>0) insert insertASList;
//Updating the existing asset information
if(updateASList.size()>0) update updateASList;
//Load into assetIDMap
for(Asset a: insertASList){
assetIDMap.put(a.Contract_Product__c,
a.id);}
//go over the list of contract products and update the newly created asset values
for(integer i=0;i<trigger.new.size();i++){
Contract_Product__c cp= trigger.new[i];
if(assetIDMap.get(
cp.id)!=null){cp.Asset__c=assetIDMap.get(
cp.id );}
}
}
Post this question in the developer board, where you may get and better and quick response
http://boards.developerforce.com/t5/Apex-Code-Development/bd-p/apex