0

Am having a scenario where Assets is being created using orchestration definition Auto task and assets linked to the billing account of the customer.

In the Asset Trigger,

For afterInsert: Retrieve Network priority, End Customer from OrderItem.vlocity_cmt__JSONAttribute__c and insert to Asset. Network_priority__c, Asset. End_Customer_c respectively

For AfterUpdate: If the Network Prioritization, End Customer Type , Carelevel are same as old values respectively then leave the same else Retrieve Network Prioritization, End Customer Type and Carelevel from OrderItem.vlocity_cmt__JSONAttribute__c and insert to Asset. Network_priority__c, Asset. End_Customer_c respectively

Asset -> Order and Order line item as lookup relationship.

Can you please share the code for this on trigger handler.

Sample Code i created

List<OrderItem> OrderItemswithAssets = [select id,Order.Access_Type__c,ProductCode__c,vlocity_cmt__JSONAttribute__c, vlocity_cmt__Product2Id__r.vlocity_cmt__ObjectTypeId__r.name from OrderItem where id in : orderitemIds];   
    
List<Asset> assetListToInsert = newObjects;
    
for(OrderItem Oitm:OrderItemswithAssets) {
    objecttype= '';
    networkPriortisation  = '';
    endCustomerType = '';
    Asset assetToBeInserted = new Asset();
    assetListToInsert = new List<Asset>();
    system.debug('Order.Access_Type__c ' + Oitm.Order.Access_Type__c);
        
    if(Oitm.Order.Access_Type__c=='SOGEA') {
        objecttype = Oitm.vlocity_cmt__Product2Id__r.vlocity_cmt__ObjectTypeId__r.name;
        system.debug('order ITem objecttype ' + objecttype);
            
        if (objecttype.contains('TTB Broadband Object')) {
            OiUtilAttribute OiAttr = new OiUtilAttribute(Oitm); // To parse JSON
            system.debug('JSON Attr' + Oitm.vlocity_cmt__JSONAttribute__c);
            networkPriortisation = OiAttr.parseJSONRunTimeAttributes('ATTCAT-TTB-BROADBAND-CH',Oitm.vlocity_cmt__JSONAttribute__c,'ATT-TTWS-NETWORK-PRIORITISATION');
            endCustomerType = OiAttr.parseJSONRunTimeAttributes('ATTCAT-TTB-BROADBAND-CH',Oitm.vlocity_cmt__JSONAttribute__c,'ATT-TTWS-END-CUSTOMER-TYPE');
            //system.debug('networkPriortisation -->' + networkPriortisation);
            //system.debug('endCustomerType -->' + endCustomerType );
            assetToBeInserted.NetworkPrioritization__c = networkPriortisation;
            assetToBeInserted.End_Customer_Type__c = endCustomerType ;
            assetListToInsert.add(assetToBeInserted);
        }
    }
    if (assetListToInsert.size() > 0) {
        UPDATE assetListToInsert;
    }
}

1 Answer 1

0

In your Trigger you can check if it is Insert or Update

if(Trigger.isInsert){
//your code
}
if(Trigger.isUpdate){
//your code
}

I have noticed that your create Statement and insert Statement of the list is inside your Loop. That is not good and cause Errors due to Limits. I changed your code a bit, but I do not know what your problem ist. Maybe you can share an errormessage.

List<OrderItem> OrderItemswithAssets = [select id,Order.Access_Type__c,ProductCode__c,vlocity_cmt__JSONAttribute__c, vlocity_cmt__Product2Id__r.vlocity_cmt__ObjectTypeId__r.name from OrderItem where id in : orderitemIds];   

List<Asset> assetListToInsert = new List<Asset>();;

for(OrderItem Oitm:OrderItemswithAssets) {
objecttype= '';
networkPriortisation  = '';
endCustomerType = '';
Asset assetToBeInserted = new Asset();
system.debug('Order.Access_Type__c ' + Oitm.Order.Access_Type__c);
    
if(Oitm.Order.Access_Type__c=='SOGEA') {
    objecttype = Oitm.vlocity_cmt__Product2Id__r.vlocity_cmt__ObjectTypeId__r.name;
    system.debug('order ITem objecttype ' + objecttype);
        
    if (objecttype.contains('TTB Broadband Object')) {
        OiUtilAttribute OiAttr = new OiUtilAttribute(Oitm); // To parse JSON
        system.debug('JSON Attr' + Oitm.vlocity_cmt__JSONAttribute__c);
        networkPriortisation = OiAttr.parseJSONRunTimeAttributes('ATTCAT-TTB-BROADBAND-CH',Oitm.vlocity_cmt__JSONAttribute__c,'ATT-TTWS-NETWORK-PRIORITISATION');
        endCustomerType = OiAttr.parseJSONRunTimeAttributes('ATTCAT-TTB-BROADBAND-CH',Oitm.vlocity_cmt__JSONAttribute__c,'ATT-TTWS-END-CUSTOMER-TYPE');
        //system.debug('networkPriortisation -->' + networkPriortisation);
        //system.debug('endCustomerType -->' + endCustomerType );
        assetToBeInserted.NetworkPrioritization__c = networkPriortisation;
        assetToBeInserted.End_Customer_Type__c = endCustomerType ;
        assetListToInsert.add(assetToBeInserted);
    }
}
}
UPDATE assetListToInsert;
1
  • 1
    N.B. final if statement is not required. DML on empty lists does not burn limits
    – cropredy
    Jul 20, 2022 at 22:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .