You need to sign in to do that
Don't have an account?
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;
}
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
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;
}
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
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);
Still receving the same error msg when i try to close the case :(
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
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
Can you post your modified code?
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;
}
Do you know why this is getting inserted twice?
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;
}
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
Are these two new assets are identical?
Can you post the records?
Yes they both are same these are fields in asset which is being populated from case
Asset Name: Iphone
Try changing the event from 'after' to 'before' and also check the debug logs to find why two records are insterted.
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
Read this topic to avoid recurrsive triggers --> http://www.salesforce.com/docs/developer/cookbook/Content/apex_controlling_recursive_triggers.htm and also read this-->http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_static.htm
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!
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 .