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
sam_Adminsam_Admin 

Creating Asset record when case is closed

Am trying to create an Asset when a case is closed but asset is not being created when i close it , here is my code not sure what's wrong here

 

trigger CaseAsset on Case (after insert) {
List<Asset> ast = new List<Asset>();
   
    for (case c : Trigger.new) {
      
    if(trigger.isinsert && (c.status == 'closed' && c.Type == 'TELECOM MOBILE_(Connectivity, SW, HW)'))
    
      {
         ast.add(new asset(   
        
        Assigned_Number__c = c.Asser_Phone_Number__c,
        Asset_Type__c = c.Asset_Type__c,
        Name = c.Asset_Type__c ));
        
      }
        
    }
    insert ast;
}

  

Best Answer chosen by Admin (Salesforce Developers) 
VPrakashVPrakash

 

use after update event, this should work

 

 

trigger CaseAsset on Case (after update) {
List<Asset> ast = new List<Asset>();
   
    for (case c : Trigger.new) {
     case oldc = trigger.oldmap.get(c.id);
   if((c.status != oldc.status__c) && (c.status == 'closed' && c.Type == 'TELECOM MOBILE_(Connectivity, SW, HW)'))
    
      {
         ast.add(new asset(   
        
        Assigned_Number__c = c.Asser_Phone_Number__c,
        Asset_Type__c = c.Asset_Type__c,
        Name = c.Asset_Type__c ));
        
      }
        
    }

if(ast.szie()>0)
    insert ast;
}

All Answers

VPrakashVPrakash

 

use after update event, this should work

 

 

trigger CaseAsset on Case (after update) {
List<Asset> ast = new List<Asset>();
   
    for (case c : Trigger.new) {
     case oldc = trigger.oldmap.get(c.id);
   if((c.status != oldc.status__c) && (c.status == 'closed' && c.Type == 'TELECOM MOBILE_(Connectivity, SW, HW)'))
    
      {
         ast.add(new asset(   
        
        Assigned_Number__c = c.Asser_Phone_Number__c,
        Asset_Type__c = c.Asset_Type__c,
        Name = c.Asset_Type__c ));
        
      }
        
    }

if(ast.szie()>0)
    insert ast;
}

This was selected as the best answer
sam_Adminsam_Admin

Ths for reply when i try to close the case i get this error

 

 

Apex trigger CaseAsset caused an unexpected exception, contact your administrator: CaseAsset: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: AccountId, ContactId (asset must have account and/or contact parent(s)): [AccountId, ContactId]: Trigger.CaseAsset: line 20, column 1

VPrakashVPrakash

Account and contact relations are required to create an asset, you must assign the contactid and accountid to the asset like 

 

  ast.add(new asset(   
        
        Assigned_Number__c = c.Asser_Phone_Number__c,
        Asset_Type__c = c.Asset_Type__c,
        Name = c.Asset_Type__c ),contact =c.contact,account=c.account);

sam_Adminsam_Admin

Still receving the same error msg when i try to close the case :(

VPrakashVPrakash

check the relationship names from case-->account and asset-->account (do it for contact as well) and assign those correctly.... list

 

may be like this 

 

accountid = c.accountid, contactid = c.contactid 

sam_Adminsam_Admin

Gr8 it did worked but the problem is 2 assets are getting created of same type when i close the case , why it is getting inserted twice

VPrakashVPrakash

Can you post your modified code?

sam_Adminsam_Admin

trigger CaseAsset on Case (after update) {
List<Asset> ast = new List<Asset>();
   
    for (case c : Trigger.new) {
     case oldc = trigger.oldmap.get(c.id);
   if((c.status != oldc.status) && (c.status == 'closed' && c.Type == 'TELECOM MOBILE_(Connectivity, SW, HW)'
   && c.SubType__c == 'New Line of Service'))
    
      {
         ast.add(new asset(   
        
        Assigned_Number__c = c.Asser_Phone_Number__c,
        Asset_Type__c = c.Asset_Type__c,
        Name = c.Asset_Type__c ,Contactid = c.contactid,accountid = c.accountid));
        
      }
        
    }

    if(ast.size()>0)
    insert ast;
}

sam_Adminsam_Admin

Do you know why this is getting inserted twice?

VPrakashVPrakash

I am not sure why it is getting inserted twice. Are there any other triggers or workflow field updates on the object?

 

Probably two assets are inserted as childs to both account and contact. Try assigning the asset as child to only Account or contact  and see if it works  Use following

 

trigger CaseAsset on Case (after update) {
List<Asset> ast = new List<Asset>();
   
    for (case c : Trigger.new) {
     case oldc = trigger.oldmap.get(c.id);
   if((c.status != oldc.status) && (c.status == 'closed' && c.Type == 'TELECOM MOBILE_(Connectivity, SW, HW)'
   && c.SubType__c == 'New Line of Service'))
    
      {
         asset newasset = new asset(); 
        
        newasset .Assigned_Number__c = c.Asser_Phone_Number__c;

        newasset .Asset_Type__c = c.Asset_Type__c;
        newasset .Name = c.Asset_Type__c;


newasset.accountid = c.accountid));
        

ast.add(newasset);
      }
        
    }

    if(ast.size()>0)
    insert ast;
}

sam_Adminsam_Admin

There are triggers on case object but nothing is related to asset , i tried assigning asset as child to account first and then no assets got created when i tried for contact then again 2 assets being inserted , not sure wts wrong here

VPrakashVPrakash

Are these two new assets are identical?

 

Can you post the records? 

sam_Adminsam_Admin

Yes they both are same these are fields in asset which is being populated from case

 

Asset Name: Iphone

Asset Type: Iphone
Assigned Number: (666) 556-5656
VPrakashVPrakash

Try changing the event from 'after' to 'before' and also check the debug logs to find why two records are insterted. 

sam_Adminsam_Admin

I tried for even before update and i get 2 assets but when am checking my debug log i see that the trigger is getting executed twice and not sure why it getting 2times executed

sam_Adminsam_Admin

Thx for the link..am kinda new to coding this example has both class and trigger and not sure how to execute this in only my trigger .

 

TIA!

sam_Adminsam_Admin

I got it i created field and updated this field through workflow and used the same field in trigger now only one record is being created .