+ Start a Discussion
Keri-An Richards 9Keri-An Richards 9 

Update Lines from Contract Activaton

Hi all,
Hoping I can beg some help again with another trigger.  I need to update a list of custom objects (Contract Lines) when a contract is activated.
Appreciate any help I can get.
Keri-An
Best Answer chosen by Keri-An Richards 9
Abilash Kosigi 8Abilash Kosigi 8

Hi Kerl-An,

Assuming your contract lines have a lookup on ContractNumber field of Contract object, I have written the following piece of code, which is working fine.
You may need to customize a bit to suit your other requirements. However, the basic requirement that you have mentioned in the post can be fulfilled with the following piece of code. CheckRecurisveTrigger class needs to be created to avoid recursive trigger.

Trigger:

 trigger updateContractLine on Contract(after insert, after update) {

if(CheckRecursiveTrigger.runOnce())
{
 for (Contract con: Trigger.new)
 {
  Contract k = [select Id,  ContractNumber,status from Contract where Id IN : trigger.new];
  String contnumber=k.ContractNumber ;
  List<Contract_Line__c>  clc = [Select Id, Contract_Line_Status__c from Contract_Line__c where
   Contract__r.ContractNumber = :contnumber];


  if (k.status == 'Activated')
  {
   for (Contract_Line__c c: clc)
   {
     c.Contract_line_status__c = 'Active';
    }
   update clc;
  }
 }
}
}



CheckRecursiveTrigger Class:

public class CheckRecursiveTrigger {
    private static boolean run = true;
    public static boolean runOnce(){
        if (run)
        {
            run=false;
            return true;
        }
        else
        {
            return false;
        }
    }

}

If this suits your requirements, please do not forget to mark as the solution :).

All Answers

Keri-An Richards 9Keri-An Richards 9
Sorry - one additional detail - the status on the lines is slightly different to that on the contract - so also need to map those.
Abilash Kosigi 8Abilash Kosigi 8
May I request few more details about your requirement.

If I understood it correctly, if Status field of Contract is changed to 'Activated', you need to update a list custom objects. In this case, the custome object name is Contract Lines.

So, what would like to update on this custom object. You want to update a field?

Please let me know, to give you a right reply
Keri-An Richards 9Keri-An Richards 9
Thanks Abilash
I thought i might not have been clear enough.
I need a trigger so that when the Activate button is clicked (std Salesforce button) on a Contract (thereby updating it's status to Activated), I need to update the status on the Contract Lines custom object.
Status on Contracts is Draft, Activated and Expired
Status on Contract Lines is Pending, Active and Inactive
There can be many Contract Lines on a Contract (similar set up to Opportunity and Opportunity Line Items)
Is that enough information?
Abilash Kosigi 8Abilash Kosigi 8

Hi Kerl-An,

Assuming your contract lines have a lookup on ContractNumber field of Contract object, I have written the following piece of code, which is working fine.
You may need to customize a bit to suit your other requirements. However, the basic requirement that you have mentioned in the post can be fulfilled with the following piece of code. CheckRecurisveTrigger class needs to be created to avoid recursive trigger.

Trigger:

 trigger updateContractLine on Contract(after insert, after update) {

if(CheckRecursiveTrigger.runOnce())
{
 for (Contract con: Trigger.new)
 {
  Contract k = [select Id,  ContractNumber,status from Contract where Id IN : trigger.new];
  String contnumber=k.ContractNumber ;
  List<Contract_Line__c>  clc = [Select Id, Contract_Line_Status__c from Contract_Line__c where
   Contract__r.ContractNumber = :contnumber];


  if (k.status == 'Activated')
  {
   for (Contract_Line__c c: clc)
   {
     c.Contract_line_status__c = 'Active';
    }
   update clc;
  }
 }
}
}



CheckRecursiveTrigger Class:

public class CheckRecursiveTrigger {
    private static boolean run = true;
    public static boolean runOnce(){
        if (run)
        {
            run=false;
            return true;
        }
        else
        {
            return false;
        }
    }

}

If this suits your requirements, please do not forget to mark as the solution :).

This was selected as the best answer
Keri-An Richards 9Keri-An Richards 9
Thanks Abilash - that got it pretty close - I updated object and field names and it looks as though it's working superbly!
Keri-An Richards 9Keri-An Richards 9
@Abilash or anyone who can help

How can I change this to update the contract lines once the Contract is Expired?

I have a workflow that has a time based field update that amends the Contract status to Expired on the day it finishes.  I also need to mark the contract lines as Inactive when that happens.

Thanks
Keri-An Richards 9Keri-An Richards 9
Never mind - I think I cracked it!