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
anillllanillll 

Test Coverage

trigger promoteOrdertoAsset on Item_Order__c (after insert, after update) {
 2	     
 3	     public List<Asset> listOfAssets = new List<Asset>();
 4	     public List<Asset> listOfUpdateAssets= new List<Asset>();
 5	     Schema.DescribeSObjectResult objDescribe =  Asset.SObjectType.getDescribe();
 6	     List<Status__c> status = [select id, Name from Status__C];
 7	      Map<Id,String> statusMap = new Map<Id, String>();
 8	     for (Status__c stat : status)
 9	     {
 10	         statusMap.put(stat.Id, stat.Name);
 11	     }
 12	     
 13	     List<OrderToAssetMap__c> conversionMap = OrderToAssetMap__c.getall().values();
 14	     Map<String,String> ignoreList = new Map<String, String>();
 15	     Map<String,String> mapList = new Map<String, String>();
 16	     for (OrderToAssetMap__c mapField : conversionMap )
 17	     {
 18	         if(mapField.Ignore__c= true)
 19	         {
 20	             ignoreList.put(mapField.Asset_Field__c,mapField.Asset_Field__c);
 21	         }
 22	     }
 23	    
 24	     for (OrderToAssetMap__c mapField : conversionMap )
 25	     {
 26	         System.debug(' mapField.Ignore__c is: ' + mapField.Ignore__c);
 27	         System.debug(' mapField.Asset_Field__c is: ' + mapField.Asset_Field__c);
 28	         System.debug('mapField.Order_Field__c is: ' +mapField.Order_Field__c);
 29	         if(mapField.Asset_Field__c!=null)
 30	         {
 31	             mapList.put(mapField.Asset_Field__c,mapField.Order_Field__c);
 32	         }
 33	     }
 34	     System.debug('mapList is: ' + mapList );
 35	     List<String> itemIds = new List<String>();
 36	     List<String> parentItemIds = new List<String>();
 37	     for(Item_Order__c ord : Trigger.new )
 38	     {
 39	         itemIds.add(ord.Item_ID__c);
 40	     }
 41	     for(Item_Order__c ord : Trigger.new )
 42	     {
 43	         if(ord.Parent_Item__c != null)
 44	         {
 45	             parentItemIds.add(ord.Parent_Item_Id__c);
 46	         }
 47	     }
 48	     System.debug('parentItemIds: ' + parentItemIds);
 49	     List<Asset> updateAssetList = [select Id, Item_Id__c from Asset where Item_id__c in : itemIds];
 50	     List<Asset> parentAssetList = [select Id, Item_Id__c from Asset where Item_id__c in : parentItemIds];
 51	     System.debug('parentAssetList : ' + parentAssetList );
 52	     Map<String, Asset> updateAssetMap = new Map<String, Asset>();
 53	     Map<String, Asset> parentAssetMap = new Map<String, Asset>();
 54	     for(Asset asset : updateAssetList )
 55	     {
 56	        updateAssetMap.put(asset.Item_Id__c, asset);
 57	     }
 58	     for(Asset asset : parentAssetList )
 59	     {
 60	        parentAssetMap.put(asset.Item_id__c, asset);
 61	     }
 62	     for(Item_Order__c ord : Trigger.new )
 63	     {
 64	         System.debug('Order Id: ' + ord.Account__C);
 65	         if(statusMap.get(ord.Status__c) != null && statusMap.get(ord.Status__c) == 'Published' && updateAssetMap.get(ord.Item_Id__C) == null)
 66	         {
 67	             System.debug('Creating Asset for Order: ' + ord);
 68	             Asset asset = new Asset();
 69	             asset.AccountId = ord.Account__c;
 70	             if(ord.Parent_Item__c != null)
 71	             {
 72	                 asset.Asset__c = parentAssetMap.get(ord.Parent_Item_Id__c).Id;
 73	             }
 74	             asset.InstallDate = System.today();
 75	              for (String field : objDescribe.fields.getMap().keySet())
 76	                 {
 77	                     if(ignoreList.get(field)== null && mapList.get(field)== null)
 78	                     {
 79	                         if(ord.get(field) != null)
 80	                         {
 81	                             asset.put(field , ord.get(field));
 82	                             System.debug('Asset is: '+ asset);
 83	                         }
 84	                     }
 85	                     if(mapList.get(field)!= null)
 86	                     {
 87	                         String orderField = mapList.get(field);
 88	                         asset.put(field , ord.get(orderField ));
 89	                         System.debug('orderField is: '+ orderField );
 90	                         System.debug(' orderField Asset is: '+ asset);
 91	                     }
 92	                 }
 93	             listOfAssets.add(asset);
 94	         }
 95	         else if (updateAssetMap.get(ord.Item_Id__C) != null && (ord.Action__c =='Change' ||ord.Action__c =='Renew') && ord.Action_Type__c != 'Cancel')
 96	         {
 97	             Asset asset = new Asset(Id=updateAssetMap.get(ord.Item_Id__C).Id);
 98	              for (String field : objDescribe.fields.getMap().keySet())
 99	                 {
 100	                     if(ignoreList.get(field)== null)
 101	                     {
 102	                         asset.put(field , ord.get(field));
 103	                         System.debug('Asset is: '+ asset);
 104	                     }
 105	                 }
 106	                 listOfUpdateAssets.add(asset);
 107	         }
 108	         else if (updateAssetMap.get(ord.Item_Id__C) != null && ord.Action__c =='Cancel'  && ord.Action_Type__c != 'Cancel')
 109	         {
 110	             Asset asset = new Asset(Id=updateAssetMap.get(ord.Item_Id__C).Id);
 111	              asset.Status = 'Cancel';
 112	                 listOfUpdateAssets.add(asset);
 113	         }
 114	        
 115	         
 116	     }
 117	    
 118	     System.debug('listOfAssets: '+ listOfAssets);
 119	     if(listOfAssets!=null)
 120	     {
 121	      insert listOfAssets;
 122	      }
 123	      if(listOfUpdateAssets != null)
 124	      {
 125	          update listOfUpdateAssets;
 126	      }
 127	  }

 Line no:68-112 are not covering

 

can u pls help in these

 

hitesh90hitesh90

Hi anil,

 

Put system.debug above the line no: 65 for check the value of statusMap.get(ord.Status__c) and updateAssetMap.get(ord.Item_Id__c)

i think it is getting null value because of this your class is not covered.

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thanks,
Hitesh Patel
SFDC Certified Developer & Administrator