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
p.gleixnerp.gleixner 

Apex trigger creates Asset twice

Hi!


I created a apex trigger wich, among other things, calls following class when a case is created or updated (before insert, before update):

 

public class createAsset {
      
      public static void setAsset(Account acc, Case c, String[] data){
           
//defining variables for the Asset TextFields
        String assetName = data[7]+' - '+data[6];
        String gon = data[5];
        String geSystemId = data[6];
        String installCode = data[10];
        
        Contact con = [SELECT Id, Name FROM Contact WHERE Name =: data[0] limit 1];
        Release__c rel = [SELECT Id, Name FROM Release__c WHERE Name LIKE: data[8]+'%' ORDER BY Name ASC limit 1];
//Creating a new Asset with fields from Descr.
        Asset a = new Asset(AccountId = [SELECT Id FROM Account WHERE Name =: data[3] limit 1].Id,
                           Name = assetName+'_TEST04.1',
                           SF42_GON__c = gon,
                           SF42_GE_System_ID__c = geSystemId,
                           Installation_Code__c = installCode,
                           Optionen__c = data[9],
                           SF42_Release__c = rel.Id,
                           ContactId = [SELECT Id FROM Contact WHERE Name =: data[0] limit 1].Id);
      
//Update/Insert the Asset
          upsert(a);
            
   
    }
}

 

Actually that whole thing works, but the Problem is it creates not one but two similar Assets.

Why is this? Has this anything to do with the trigger? Or with the upsert method?


I hope you can help me again

 

Kind regards

Peter

sfdcfoxsfdcfox

The function's being called twice, obviously. Check your debug logs. The problem is likely in your trigger, not this class (well, besides the fact that it is not bulkified).

p.gleixnerp.gleixner

Hi!

 

thnaks for your answer. I already tried reading out the logs but all i receive when i click on a log file is "Operation did not complete. Long running operation did not complete, continuing in background"

Kinda of strange.


Yes I know its not bulkiefied yet. But I am pretty new to apex and had to figure all this out on a very basic way. Now that I am a little deeper into it and when everything works this thing is going to be bulkified ;)

 

Kind regards

Peter

€dit:

Here Is my Trigger Code:

 

trigger licenseReqToAsset on Case (before insert, before update) {
         
        String s;
        String[] split;
        String field;
        Account acc;
        Contact con;
    for(Case c:trigger.new){   
        s = c.Description.replaceAll('\'','');
        s= s.replaceAll(',',';');
        split =  s.split('\n');
        field  = '';
//Cutting the Description in appropriate pieces
      
      for(Integer i=0; i< split.Size()-1; i++){
            if(split[i].contains('My Name'))
               field=field+split[i+1]+'\n';
               if(split[i].contains('My e-mail address'))
               field=field+split[i+1]+'\n';
             if(split[i].contains('My SSO ID'))
               field=field+split[i+1]+'\n';
              if(split[i].contains('Customer name'))
               field=field+split[i+1]+'\n';
              if(split[i].contains('Customer Country'))
               field=field+split[i+1]+'\n';
              if(split[i].contains('GON (global order number)'))
               field=field+split[i+1]+'\n';
              if(split[i].contains('GE system ID'))
               field=field+split[i+1]+'\n';
              if(split[i].contains('Ordered system configuration'))
               field=field+split[i+1]+'\n';
              if(split[i].contains('Software version'))
               field=field+split[i+1]+'\n';
            if(split[i].contains('Ordered Options'))
               field=field+split[i+1]+'\n';
              if(split[i].contains('Installation Code'))
               field=field+split[i+1]+'\n';
           }
        field = field.replaceAll('\r','');
/refill Array
          split = field.split('\n');
               
//SQL Queriey to get Id, Name from the account/contact matching the Account/ContactName in the Description   
       if([SELECT Id ,Name FROM Account WHERE Name =: split[3] limit 1].isEmpty()){
                acc = new Account(Name=split[3]);
                   upsert(acc);}
                else{
                    acc = [SELECT Id ,Name FROM Account WHERE Name =: split[3] limit 1];
                    c.AccountId = acc.Id;
                }
        
       
          if([SELECT Id ,Name FROM Contact WHERE Name =: split[0] limit 1].isEmpty()){
                con = new Contact(LastName = split[0],
                                 AccountId = [SELECT Id FROM Account WHERE Name = 'ACME corp' limit 1].Id,
                                 Kontakt_Bemerkung__c='SSO ID: '+split[2]+'\n'+'GON: '+ split[5]);
                   upsert(con);}
                else{
                    con = [SELECT Id ,Name FROM Contact WHERE Name =: split[0] limit 1];
                    c.ContactId = con.Id;
                }
    
        
        Release__c rel = [SELECT Id, Name FROM Release__c WHERE Name LIKE: split[8]+'%' ORDER BY Name ASC limit 1];
          c.Release__c = rel.Id;
        

        
       
    }
    //Call for an extern class to create an Asset       
     createAsset.setAsset(acc, split);
}

sfdcfoxsfdcfox

My first thought is that you have a Workflow Field Update or some rule that is being called on Case. This would cause a recursive call to your trigger and cause the asset to be created twice. You don't apparently have anything here that stands out in terms of a recursive call; if you did, it'd be an infinite depth trigger that would error out, so this is unlikely.