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
cbrocbro 

Need to add Line Number to this object when created. (cannot be one that already exists).

I need to add a number to the Contract_Line__c record that is created in the trigger below.

 

It cannot be the same number (cl.Line_No__c) as another Contract_Line__c record that is associated with the same Contract_Header__c record that this new one that is created is associated with.

 

It needs to start with 1 - and then go from there.

 

Example:

 

Each Contract_Line__c record created is also associated with a Contract_Header__c record - which is inherited from the Asset record.

 

If I create one Contract_Line__c record, and it has Contract_Line__c.Line_No__c = '1' - and it's associated with Contract_Header__c record 'XXX1'

 

The next Contract_Line__c record that is created (and associated with Contract_Header__c record 'XXX1') should then have a Contract_Line__c.Line_No__c = '2'

 

and so on...

 

Further:

When a Contract_Line__c record then has an End Date (Contract_Line__c.End_Date__c = Today or before), it should be removed from this sequence - and all other Contract_Line__c.Line_No__c should be updated with new numbers starting at the number 1.

 

Can I do that?

 

Does that make sense?

 

Thanks for any help.

 

trigger Asset on Asset (after update) {

    List <Contract_Line__c> contractLines = new List <Contract_Line__c> ();
    Map <Id, Asset> oldmap = new Map <Id, Asset>();
    Map <Id, Asset> newmap = new Map <Id, Asset>();

    //System.debug('Chris in trigger');
    if(Constants.ASSET_FIRST_RUN == False){ }else 
    for(Asset a: Trigger.new) 
    {
    //System.debug('Chris in asset loop')
    if ((Trigger.oldMap.get(a.Id).Status != Trigger.newMap.get(a.Id).Status) && Trigger.newMap.get(a.Id).Status == 'Active')
        { 
        //System.debug('Chris in status changed');
        Contract_Line__c cl = new Contract_Line__c ();
        cl.Asset__c = a.Id;
        cl.Line_No__c = 
        //need to add new number for the Line Number (cl.Line__c) on Contract Line
        //but make sure it's not the same number as one that was already existing for this Contract_Header__c
        
        //System.debug('Chris creating new contract line and adding to list');
        contractLines.add(cl);
        }
        Constants.ASSET_FIRST_RUN = False;      
    }

    if(contractLines.size()>0)
    {
    //System.debug('Chris has values to insert = '+ contractLines.size());
    try
     {
        insert contractLines;
     }
     catch (System.Dmlexception e)  
     {
        system.debug (e); 
     }
    }
}

 

 

 

 

 

Cory CowgillCory Cowgill

Question: Why don't you make this field an Auto-Number field? Seems like Auto-Number would be better suited for this use case based on limited information below. This would auto-increment the field in a format you choose.

 

Have you looked at using Auto-Number Field?

cbrocbro

I thought about that, but the only problem is that each Contract_Header__c record that these Contract_Line__c records are associated with are unique.

 

Each Contract Line needs to start with 1, instead of just becoming the next line in a sequence of Contract_Line__c records.

 

:)


Is there another way to do this with the AutoNumber field?