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 update related record on edit of parent record

I have code that creates a new Contract_Line__c record whenever I change the status of my parent object, Asset, to 'Active'.

 

How do I make sure the Contract_Line__c is updated whenever I edit my Asset thereafter?

 

(As an aside, all of my Contract_Line__c fields are updated by formula fields related to the parent Asset - but this doesn't take care of updating the record.  I need it to actually update the record so I can have a second trigger fire (from the Contract_Line__c record)).

 

Thanks for any help!

 

Here is my current code, which only takes care of creating new Contract_Line__c records (and not creating duplicates):

 

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;
        //this needs to "update" the record so that it also "updates" the related Contract Header
        //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); 
     }
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
trigger Asset on Asset (after update)
{
    Map <Id, Asset> oldmap = new Map <Id, Asset>();
    Map <Id, Asset> newmap = new Map <Id, Asset>();
    List<Contract_Line__c> newConList = new List<Contract_Line__c>();

System.debug('------ Asset First Run-----------'+Constants.Asset_First_Run);

    if(Constants.ASSET_FIRST_RUN)  
    {
        Map<Id, Contract_Line__c> conLineMap = new Map<Id, Contract_Line__c>();

        for(Contract_Line__c cl : [SELECT Id, Name, Asset__c, Date_Last_Updated__c FROM Contract_Line__c WHERE Asset__c IN :Trigger.newMap.keySet()])
        {
            conLineMap.put(cl.Asset__c, cl);
        }

        for(Asset a: Trigger.new) 
        {
            if(conLineMap.get(a.Id) <> null) // asset have a Contract Line record.
            {
                Contract_Line__c con = conLineMap.get(a.Id);

                con.Date_Last_Updated__c = System.now();
                newConList.add(con);
            }
            else                             // Asset do not have a Contract Line record.
            {
                Contract_Line__c con = new Contract_Line__c();

                con.Asset__c = a.Id;
                con.Date_Last_Updated__c = System.now();
                newConList.add(con);
            }
        }
        Constants.ASSET_FIRST_RUN = False;
        upsert newConList;
    }
}

 I am checking the value for Constants.Asset_First_Run to be false to run the code...

 

now i guess this will work.

All Answers

Naidu PothiniNaidu Pothini
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>();

    if(!Constants.ASSET_FIRST_RUN)
    {
        Map<Id, Contract_Line__c> conLineMap = new Map<Id, Contract_Line__c>();

        for(Contract_Line__c cl : [SELECT Id, Name, Asset__c, to_be_update_Field FROM Contract_Line__c WHERE Asset__c IN :Trigger.newMap.keySet())])
        {
            conLineMap.put(cl.Asset__c, cl);
        }

        for(Asset a: Trigger.new) 
        {
            if(conLineMap.get(a.Id) <> null) // asset have a Contract Line record.
            {
                Contract_Line__c con = conLineMap.get(a.Id);

                con.to_be_Updated_Field = 'Assign something here';
                newConList.add(con);
            }
            else                             // Asset do not have a Contract Line record.
            {
                Contract_Line__c con = new Contract_Line__c();

                con.Asset__c = a.Id;
                con.to_be_Updated_Field = 'Assign something here';
                newConList.add(con);
            }
        }

        upsert newConList;
    }
}

 try this.

cbrocbro

Error: Compile Error: Variable does not exist: newConList at line 35 column 16

 

Hello Naidu, 

 

Thanks very much.  I added a Date/Time field to update.

 

I am now getting the following error when I try to save, however: Error: Compile Error: Variable does not exist: newConList at line 35 column 16

 

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>();

    if(!Constants.ASSET_FIRST_RUN)
    {
        Map<Id, Contract_Line__c> conLineMap = new Map<Id, Contract_Line__c>();

        for(Contract_Line__c cl : [SELECT Id, Name, Asset__c, Date_Last_Updated__c FROM Contract_Line__c WHERE Asset__c IN :Trigger.newMap.keySet()])
        {
            conLineMap.put(cl.Asset__c, cl);
        }

        for(Asset a: Trigger.new) 
        {
            if(conLineMap.get(a.Id) <> null) // asset have a Contract Line record.
            {
                Contract_Line__c con = conLineMap.get(a.Id);

                con.Date_Last_Updated__c = System.now();
                newConList.add(con);
            }
            else                             // Asset do not have a Contract Line record.
            {
                Contract_Line__c con = new Contract_Line__c();

                con.Asset__c = a.Id;
                con.Date_Last_Updated__c = System.now();
                newConList.add(con);
            }
        }

        upsert newConList;
    }
}

 

Naidu PothiniNaidu Pothini
trigger Asset on Asset (after update)
{
    Map <Id, Asset> oldmap = new Map <Id, Asset>();
    Map <Id, Asset> newmap = new Map <Id, Asset>();
    List<Contract_Line__c> newConList = new List<Contract_Line__c>();

    if(!Constants.ASSET_FIRST_RUN)
    {
        Map<Id, Contract_Line__c> conLineMap = new Map<Id, Contract_Line__c>();

        for(Contract_Line__c cl : [SELECT Id, Name, Asset__c, Date_Last_Updated__c FROM Contract_Line__c WHERE Asset__c IN :Trigger.newMap.keySet()])
        {
            conLineMap.put(cl.Asset__c, cl);
        }

        for(Asset a: Trigger.new) 
        {
            if(conLineMap.get(a.Id) <> null) // asset have a Contract Line record.
            {
                Contract_Line__c con = conLineMap.get(a.Id);

                con.Date_Last_Updated__c = System.now();
                newConList.add(con);
            }
            else                             // Asset do not have a Contract Line record.
            {
                Contract_Line__c con = new Contract_Line__c();

                con.Asset__c = a.Id;
                con.Date_Last_Updated__c = System.now();
                newConList.add(con);
            }
        }

        upsert newConList;
    }
}

 try this

cbrocbro

Hmm.  

 

Well it's compiling, but the trigger is no longer working.

Naidu PothiniNaidu Pothini

Did you try with debug statements in the code above?

 

try to display the newConList before upserting the records. If possible can you post the debug log.

 

 

 

cbrocbro

Here is what I have now.

 

trigger Asset on Asset (after update)
{
    Map <Id, Asset> oldmap = new Map <Id, Asset>();
    Map <Id, Asset> newmap = new Map <Id, Asset>();
    List<Contract_Line__c> newConList = new List<Contract_Line__c>();

    //System.debug('Chris in trigger')
    if(!Constants.ASSET_FIRST_RUN)
    {
    
        Map<Id, Contract_Line__c> conLineMap = new Map<Id, Contract_Line__c>();
    
        for(Contract_Line__c cl : [SELECT Id, Name, Asset__c, Date_Last_Updated__c FROM Contract_Line__c WHERE Asset__c IN :Trigger.newMap.keySet()])
        {
            conLineMap.put(cl.Asset__c, cl);
        }

        for(Asset a: Trigger.new) 
        {
            if(conLineMap.get(a.Id) <> null) // asset have a Contract Line record.
            {
                Contract_Line__c con = conLineMap.get(a.Id);

                con.Date_Last_Updated__c = System.now();
                newConList.add(con);
            }
            else                             // Asset do not have a Contract Line record.
            {
                Contract_Line__c con = new Contract_Line__c();

                con.Asset__c = a.Id;
                con.Date_Last_Updated__c = System.now();
                newConList.add(con);
            }
        }
 
 
  if(newConList.size()>0)
    {
    //System.debug('Chris has values to insert = '+ newConList.size());
    try
     {
        upsert newConList;
     }
     catch (System.Dmlexception e)  
     {
        system.debug (e); 
     }
    }
         
     




    }
}

 

 

 DEBUG is not even touching the first statement.

 

27.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
14:58:45.041 (41410000)|EXECUTION_STARTED
14:58:45.041 (41453000)|CODE_UNIT_STARTED|[EXTERNAL]|Validation:Asset:02iV0000002hcQM
14:58:45.041 (41471000)|VALIDATION_RULE|03dV0000000Cu6j|AssetStatusLockedonInactive
14:58:45.041 (41654000)|VALIDATION_FORMULA|AND(ISCHANGED(Status),

ISPICKVAL(PRIORVALUE(Status), "Inactive"),

NOT(ISPICKVAL(Status, "Inactive")))|Status=Active
14:58:45.041 (41663000)|VALIDATION_PASS
14:58:45.041 (41672000)|CODE_UNIT_FINISHED|Validation:Asset:02iV0000002hcQM
14:58:45.041 (41681000)|EXECUTION_FINISHED
14:58:45.078 (78199000)|EXECUTION_STARTED
14:58:45.078 (78215000)|CODE_UNIT_STARTED|[EXTERNAL]|01qV0000000Cp6Y|Asset on Asset trigger event AfterUpdate for [02iV0000002hcQM]
14:58:45.079 (79620000)|SYSTEM_CONSTRUCTOR_ENTRY|[5]|<init>()
14:58:45.079 (79652000)|SYSTEM_CONSTRUCTOR_EXIT|[5]|<init>()
14:58:45.082 (82129000)|METHOD_ENTRY|[1]|01pV0000000DZH4|Constants.Constants()
14:58:45.082 (82171000)|METHOD_EXIT|[1]|Constants
14:58:45.142 (82196000)|CUMULATIVE_LIMIT_USAGE
14:58:45.142|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of code statements: 6 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

14:58:45.142|CUMULATIVE_LIMIT_USAGE_END

14:58:45.082 (82236000)|CODE_UNIT_FINISHED|Asset on Asset trigger event AfterUpdate for [02iV0000002hcQM]
14:58:45.082 (82245000)|EXECUTION_FINISHED
14:58:45.087 (87530000)|EXECUTION_STARTED
14:58:45.087 (87537000)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:Asset
14:58:45.111 (111321000)|WF_RULE_EVAL_BEGIN|Assignment
14:58:45.111 (111342000)|WF_RULE_EVAL_BEGIN|Response
14:58:45.111 (111350000)|WF_RULE_EVAL_BEGIN|Workflow
14:58:45.111 (111375000)|WF_CRITERIA_BEGIN|[Asset: 30 Day Deposit 02iV0000002hcQM]|Asset - Active|01Q80000000FC1C|ON_CREATE_OR_TRIGGERING_UPDATE
14:58:45.118 (118085000)|WF_RULE_FILTER|[Asset : Status equals Active]
14:58:45.118 (118100000)|WF_RULE_EVAL_VALUE|Active
14:58:45.118 (118105000)|WF_CRITERIA_END|true
14:58:45.119 (119026000)|WF_CRITERIA_BEGIN|[Asset: 30 Day Deposit 02iV0000002hcQM]|Asset - Inactive|01Q80000000FC1H|ON_CREATE_OR_TRIGGERING_UPDATE
14:58:45.119 (119063000)|WF_RULE_FILTER|[Asset : Status equals Inactive]
14:58:45.119 (119072000)|WF_RULE_EVAL_VALUE|Active
14:58:45.119 (119075000)|WF_CRITERIA_END|false
14:58:45.119 (119091000)|WF_SPOOL_ACTION_BEGIN|Workflow
14:58:45.119 (119391000)|WF_FIELD_UPDATE|[Asset: 30 Day Deposit 02iV0000002hcQM]|Field:Asset: Active Date|Value:Wed Mar 06 00:00:00 GMT 2013|Id=04Y80000000Czd8|CurrentRule:Asset - Active (Id=01Q80000000FC1C)
14:58:45.119 (119438000)|WF_ACTION| Field Update: 2;
14:58:45.119 (119450000)|WF_RULE_EVAL_BEGIN|Escalation
14:58:45.119 (119455000)|WF_RULE_EVAL_END
14:58:45.132 (132369000)|CODE_UNIT_STARTED|[EXTERNAL]|01qV0000000Cp6Y|Asset on Asset trigger event AfterUpdate for [02iV0000002hcQM]
14:58:45.132 (132622000)|SYSTEM_CONSTRUCTOR_ENTRY|[5]|<init>()
14:58:45.132 (132633000)|SYSTEM_CONSTRUCTOR_EXIT|[5]|<init>()
14:58:45.192 (132659000)|CUMULATIVE_LIMIT_USAGE
14:58:45.192|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of code statements: 10 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

14:58:45.192|CUMULATIVE_LIMIT_USAGE_END

14:58:45.132 (132701000)|CODE_UNIT_FINISHED|Asset on Asset trigger event AfterUpdate for [02iV0000002hcQM]
14:58:45.133 (133748000)|WF_ACTIONS_END| Field Update: 2;
14:58:45.133 (133756000)|CODE_UNIT_FINISHED|Workflow:Asset
14:58:45.133 (133763000)|EXECUTION_FINISHED

 

Naidu PothiniNaidu Pothini
trigger Asset on Asset (after update)
{
    Map <Id, Asset> oldmap = new Map <Id, Asset>();
    Map <Id, Asset> newmap = new Map <Id, Asset>();
    List<Contract_Line__c> newConList = new List<Contract_Line__c>();

System.debug('------ Asset First Run-----------'+Constants.Asset_First_Run); if(!Constants.ASSET_FIRST_RUN) // do you need it?? If not remove this { Map<Id, Contract_Line__c> conLineMap = new Map<Id, Contract_Line__c>(); for(Contract_Line__c cl : [SELECT Id, Name, Asset__c, Date_Last_Updated__c FROM Contract_Line__c WHERE Asset__c IN :Trigger.newMap.keySet()]) { conLineMap.put(cl.Asset__c, cl); } for(Asset a: Trigger.new) { if(conLineMap.get(a.Id) <> null) // asset have a Contract Line record. { Contract_Line__c con = conLineMap.get(a.Id); con.Date_Last_Updated__c = System.now(); newConList.add(con); } else // Asset do not have a Contract Line record. { Contract_Line__c con = new Contract_Line__c(); con.Asset__c = a.Id; con.Date_Last_Updated__c = System.now(); newConList.add(con); } } Constants.ASSET_FIRST_RUN = False; upsert newConList; } }

 can you post debug log now?

cbrocbro

Hey Naidu, 

 

I do need the code in red, b/c I need there to only be a 1:1 relationship between each Asset and each Contract_Line__c record.

 

I have 2 possible scenarios:

1.  It is a new record and so it needs to insert a new Contract_Line__c

2.  It is not a new record, and there is an existing Contract_Line__c record associated with the Asset - in which case it should update the Date_Last_Updated__c field.

 

Thanks very much for all of your help - I very much appreciate it.

 

Here is the new debug log.  (I have inserted the code from above)

 

This is only scenario 1 - when creating a new Contract_Line__c from the Asset.  

 

27.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
15:16:04.029 (29556000)|EXECUTION_STARTED
15:16:04.029 (29612000)|CODE_UNIT_STARTED|[EXTERNAL]|Validation:Asset:02iV0000002hcQR
15:16:04.029 (29628000)|VALIDATION_RULE|03dV0000000Cu6j|AssetStatusLockedonInactive
15:16:04.029 (29950000)|VALIDATION_FORMULA|AND(ISCHANGED(Status),

ISPICKVAL(PRIORVALUE(Status), "Inactive"),

NOT(ISPICKVAL(Status, "Inactive")))|Status=Active
15:16:04.029 (29959000)|VALIDATION_PASS
15:16:04.029 (29964000)|CODE_UNIT_FINISHED|Validation:Asset:02iV0000002hcQR
15:16:04.029 (29972000)|EXECUTION_FINISHED
15:16:04.080 (80138000)|EXECUTION_STARTED
15:16:04.080 (80159000)|CODE_UNIT_STARTED|[EXTERNAL]|01qV0000000Cp6Y|Asset on Asset trigger event AfterUpdate for [02iV0000002hcQR]
15:16:04.081 (81495000)|SYSTEM_CONSTRUCTOR_ENTRY|[5]|<init>()
15:16:04.081 (81529000)|SYSTEM_CONSTRUCTOR_EXIT|[5]|<init>()
15:16:04.084 (84287000)|METHOD_ENTRY|[1]|01pV0000000DZH4|Constants.Constants()
15:16:04.084 (84328000)|METHOD_EXIT|[1]|Constants
15:16:04.084 (84360000)|SYSTEM_METHOD_ENTRY|[7]|String.valueOf(Object)
15:16:04.084 (84387000)|SYSTEM_METHOD_EXIT|[7]|String.valueOf(Object)
15:16:04.084 (84410000)|SYSTEM_METHOD_ENTRY|[7]|System.debug(ANY)
15:16:04.084 (84422000)|USER_DEBUG|[7]|DEBUG|------ Asset First Run-----------true
15:16:04.084 (84428000)|SYSTEM_METHOD_EXIT|[7]|System.debug(ANY)
15:16:04.118 (84457000)|CUMULATIVE_LIMIT_USAGE
15:16:04.118|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of code statements: 7 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

15:16:04.118|CUMULATIVE_LIMIT_USAGE_END

15:16:04.084 (84519000)|CODE_UNIT_FINISHED|Asset on Asset trigger event AfterUpdate for [02iV0000002hcQR]
15:16:04.084 (84529000)|EXECUTION_FINISHED
15:16:04.092 (92689000)|EXECUTION_STARTED
15:16:04.092 (92699000)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:Asset
15:16:04.117 (117627000)|WF_RULE_EVAL_BEGIN|Assignment
15:16:04.117 (117666000)|WF_RULE_EVAL_BEGIN|Response
15:16:04.117 (117684000)|WF_RULE_EVAL_BEGIN|Workflow
15:16:04.117 (117716000)|WF_CRITERIA_BEGIN|[Asset: 30 Day Deposit 02iV0000002hcQR]|Asset - Active|01Q80000000FC1C|ON_CREATE_OR_TRIGGERING_UPDATE
15:16:04.127 (127970000)|WF_RULE_FILTER|[Asset : Status equals Active]
15:16:04.128 (128000000)|WF_RULE_EVAL_VALUE|Active
15:16:04.128 (128009000)|WF_CRITERIA_END|true
15:16:04.128 (128650000)|WF_CRITERIA_BEGIN|[Asset: 30 Day Deposit 02iV0000002hcQR]|Asset - Inactive|01Q80000000FC1H|ON_CREATE_OR_TRIGGERING_UPDATE
15:16:04.128 (128704000)|WF_RULE_FILTER|[Asset : Status equals Inactive]
15:16:04.128 (128725000)|WF_RULE_EVAL_VALUE|Active
15:16:04.128 (128732000)|WF_CRITERIA_END|false
15:16:04.128 (128758000)|WF_SPOOL_ACTION_BEGIN|Workflow
15:16:04.129 (129149000)|WF_FIELD_UPDATE|[Asset: 30 Day Deposit 02iV0000002hcQR]|Field:Asset: Active Date|Value:Wed Mar 06 00:00:00 GMT 2013|Id=04Y80000000Czd8|CurrentRule:Asset - Active (Id=01Q80000000FC1C)
15:16:04.129 (129217000)|WF_ACTION| Field Update: 2;
15:16:04.129 (129237000)|WF_RULE_EVAL_BEGIN|Escalation
15:16:04.129 (129245000)|WF_RULE_EVAL_END
15:16:04.143 (143388000)|CODE_UNIT_STARTED|[EXTERNAL]|01qV0000000Cp6Y|Asset on Asset trigger event AfterUpdate for [02iV0000002hcQR]
15:16:04.143 (143742000)|SYSTEM_CONSTRUCTOR_ENTRY|[5]|<init>()
15:16:04.143 (143757000)|SYSTEM_CONSTRUCTOR_EXIT|[5]|<init>()
15:16:04.143 (143788000)|SYSTEM_METHOD_ENTRY|[7]|String.valueOf(Object)
15:16:04.143 (143808000)|SYSTEM_METHOD_EXIT|[7]|String.valueOf(Object)
15:16:04.143 (143833000)|SYSTEM_METHOD_ENTRY|[7]|System.debug(ANY)
15:16:04.143 (143851000)|USER_DEBUG|[7]|DEBUG|------ Asset First Run-----------true
15:16:04.143 (143863000)|SYSTEM_METHOD_EXIT|[7]|System.debug(ANY)
15:16:04.177 (143894000)|CUMULATIVE_LIMIT_USAGE
15:16:04.177|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of code statements: 12 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

15:16:04.177|CUMULATIVE_LIMIT_USAGE_END

15:16:04.143 (143953000)|CODE_UNIT_FINISHED|Asset on Asset trigger event AfterUpdate for [02iV0000002hcQR]
15:16:04.144 (144635000)|WF_ACTIONS_END| Field Update: 2;
15:16:04.144 (144642000)|CODE_UNIT_FINISHED|Workflow:Asset
15:16:04.144 (144650000)|EXECUTION_FINISHED

 

 

 

Naidu PothiniNaidu Pothini
trigger Asset on Asset (after update)
{
    Map <Id, Asset> oldmap = new Map <Id, Asset>();
    Map <Id, Asset> newmap = new Map <Id, Asset>();
    List<Contract_Line__c> newConList = new List<Contract_Line__c>();

System.debug('------ Asset First Run-----------'+Constants.Asset_First_Run);

    if(Constants.ASSET_FIRST_RUN)  
    {
        Map<Id, Contract_Line__c> conLineMap = new Map<Id, Contract_Line__c>();

        for(Contract_Line__c cl : [SELECT Id, Name, Asset__c, Date_Last_Updated__c FROM Contract_Line__c WHERE Asset__c IN :Trigger.newMap.keySet()])
        {
            conLineMap.put(cl.Asset__c, cl);
        }

        for(Asset a: Trigger.new) 
        {
            if(conLineMap.get(a.Id) <> null) // asset have a Contract Line record.
            {
                Contract_Line__c con = conLineMap.get(a.Id);

                con.Date_Last_Updated__c = System.now();
                newConList.add(con);
            }
            else                             // Asset do not have a Contract Line record.
            {
                Contract_Line__c con = new Contract_Line__c();

                con.Asset__c = a.Id;
                con.Date_Last_Updated__c = System.now();
                newConList.add(con);
            }
        }
        Constants.ASSET_FIRST_RUN = False;
        upsert newConList;
    }
}

 I am checking the value for Constants.Asset_First_Run to be false to run the code...

 

now i guess this will work.

This was selected as the best answer
cbrocbro

Hey Naidu!

 

It worked!  Thanks again for all of your help!

 

Chris

 

Here is my final code.  

 

trigger Asset on Asset (after update)
{
    Map <Id, Asset> oldmap = new Map <Id, Asset>();
    Map <Id, Asset> newmap = new Map <Id, Asset>();
    List<Contract_Line__c> newConList = new List<Contract_Line__c>();

System.debug('------ Asset First Run-----------'+Constants.Asset_First_Run);

    if(Constants.ASSET_FIRST_RUN)  
    {
        Map<Id, Contract_Line__c> conLineMap = new Map<Id, Contract_Line__c>();

        for(Contract_Line__c cl : [SELECT Id, Name, Asset__c, Date_Last_Updated__c FROM Contract_Line__c WHERE Asset__c IN :Trigger.newMap.keySet()])
        {
            conLineMap.put(cl.Asset__c, cl);
        }

        for(Asset a: Trigger.new) 
        {
            if(conLineMap.get(a.Id) <> null) // asset have a Contract Line record.
            {
                Contract_Line__c con = conLineMap.get(a.Id);

                con.Date_Last_Updated__c = System.now();
                newConList.add(con);
            }
            else                             // Asset do not have a Contract Line record.
            {
                Contract_Line__c con = new Contract_Line__c();

                con.Asset__c = a.Id;
                con.Date_Last_Updated__c = System.now();
                newConList.add(con);
            }
        }
        Constants.ASSET_FIRST_RUN = False;
        if(newConList.size()>0)
    {
    //System.debug('Chris has values to insert = '+ newConList.size());
    try
     {
        upsert newConList;
     }
     catch (System.Dmlexception e)  
     {
        system.debug (e); 
     }
}    }
}

 

cbrocbro

FYI, I used the same code, just tweaked a bit to do the same thing for Contract to Contract_Header__c changes!

 

Thanks again, Naidu.  You've been a great help.  See tweaked code.

 

:)

 

trigger Contract on Contract (after update) 
{
    Map <Id, Contract> oldmap = new Map <Id, Contract>();
    Map <Id, Contract> newmap = new Map <Id, Contract>();
    List <Contract_Header__c> newConHeadList = new List <Contract_Header__c> ();
 
 System.debug('------ Contract First Run-----------'+Constants.Asset_First_Run);

    if(Constants.CONTRACT_FIRST_RUN)  
    {
        Map<Id, Contract_Header__c> conHeaderMap = new Map<Id, Contract_Header__c>();

        for(Contract_Header__c co : [SELECT Id, Name, Contract__c, Most_Recent_Update__c FROM Contract_Header__c WHERE Contract__c IN :Trigger.newMap.keySet()])
        {
            conHeaderMap.put(co.Contract__c, co);
        }

        for(Contract c: Trigger.new) 
        {
            if(conHeaderMap.get(c.Id) <> null) // asset have a Contract Line record.
            {
                Contract_Header__c con = conHeaderMap.get(c.Id);

                con.Most_Recent_Update__c = System.now();
                newConHeadList.add(con);
            }
            else                             // Asset do not have a Contract Line record.
            {
                Contract_Header__c con = new Contract_Header__c();

                con.Contract__c = c.Id;
                con.Most_Recent_Update__c = System.now();
                newConHeadList.add(con);
            }
        }
        Constants.CONTRACT_FIRST_RUN = False;
        if(newConHeadList.size()>0)
    {
    //System.debug('Chris has values to insert = '+ newConList.size());
    try
     {
        upsert newConHeadList;
     }
     catch (System.Dmlexception e)  
     {
        system.debug (e); 
     }
}    }
}