-1

TriggerHandler class:

 public static void onBeforeUpdate(Map<Id, Apttus__APTS_Agreement__c> oldMap, Map<Id, Apttus__APTS_Agreement__c> newMap){

   List<Apttus__APTS_Agreement__c> ApptusAgreementList = 
        [Select Id, Name, APT_Contract_Number__c, Apttus__Status__c, Apttus__Status_Category__c  FROM Apttus__APTS_Agreement__c
         WHERE Apttus__Status_Category__c = 'In Effect' and Apttus__Status__c = 'Activated' ];

   List<Contract> contractsToBeUpdatedList = new List<Contract>();

    for(Apttus__APTS_Agreement__c ApptusAgreement : ApptusAgreementList){
        Contract contractsToBeUpdated = New Contract();
      //  contractsToBeUpdated.Id = ApptusAgreement.Id; 
        contractsToBeUpdated.Name = ApptusAgreement.Name;
     // contractsToBeUpdated.ContractNumber = ApptusAgreement.APT_Contract_Number__c; // Apptus ContractNumber is Text while in Contract it is auto number
       contractsToBeUpdated.Status = ApptusAgreement.Apttus__Status__c;
       contractsToBeUpdated.Status_Category__c  = ApptusAgreement.Apttus__Status_Category__c;

        contractsToBeUpdatedList.add(contractsToBeUpdated);

    }

    upsert contractsToBeUpdatedList; }

Trigger :

  trigger APT_AgreementSyncTrigger on Apttus__APTS_Agreement__c (after 
 delete, after insert, after undelete, after update, before delete, before 
  insert, before update) {
if(trigger.isBefore) {
    if(trigger.isInsert) {

    } else if(trigger.isUpdate) {
        APT_AgreementSyncTriggerHandler.onBeforeUpdate(trigger.oldMap, trigger.newMap);
    } else if(trigger.isDelete) {

    }

} else if(trigger.isAfter) {
    if(trigger.isInsert) {

    } else if(trigger.isUpdate) {

    } else if(trigger.isDelete) {

    } else if(trigger.isUndelete) {

    }
}}
2
  • Trigger does not have compile error but when I have update the record to meet the condition, It has an error an rigger does not work. Sep 5 '18 at 10:52
  • This is my error: Error: Invalid Data. Review all error messages below to correct your data. Apex trigger APT_AgreementSyncTrigger caused an unexpected exception, contact your administrator: APT_AgreementSyncTrigger: execution of BeforeUpdate caused by: System.DmlException: Upsert failed. First exception on row 0; first error: FAILED_ACTIVATION, Choose a valid contract status and save your changes. Ask your admin for details.: [Status]: Class.APT_AgreementSyncTriggerHandler.onBeforeUpdate: line 49, column 1, This is the line 49, upsert contractsToBeUpdatedList; Sep 5 '18 at 10:55
1

You are doing DML operation on before update.You cant do any DML operation on before update. I think your trigger has to be written in "after update"

Just call this Line of code like this:

 if(trigger.isUpdate || trigger.isAfter){
 APT_AgreementSyncTriggerHandler.onBeforeUpdate(trigger.oldMap, 
 trigger.newMap);
 }

Please try to use OR condition your trigger.Instead of making else if ladder

Mistakes:

  • You are directly querying the "ApptusAgreementList", but You are passing trigger.oldmap and newmapvalues.But Where you are using it your class "APT_AgreementSyncTriggerHandler"?!!!!.
    • DML operation on Before Update Event

Please check salesforce trailhead trigger basics:

https://trailhead.salesforce.com/en/modules/apex_triggers


Please use this trigger for your reference:

Trigger:

You haven't given brief description about your trigger.But still I had written few things,I hope it will helpful to you

      trigger APT_AgreementSyncTrigger on Apttus__APTS_Agreement__c (after 
     Update) {
     APT_AgreementSyncTriggerHandler.onAFterUpdate(trigger.new);
     }

class:

   public static void APT_AgreementSyncTriggerHandler(List <
   Apttus__APTS_Agreement__c > ApptusAgreementList) {
   set < id > apptusId = new set < id > ();
    List < Contract > contractsToBeUpdatedList = new List < Contract > ();
      for (Apttus__APTS_Agreement__c ac: ApptusAgreementList) {
       apptusId.add(ac.id);
      }
   List < Apttus__APTS_Agreement__c > ApptusAgreementList = [Select Id, 
  Name, APT_Contract_Number__c, Apttus__Status__c,
    Apttus__Status_Category__c FROM Apttus__APTS_Agreement__c
       WHERE Apttus__Status_Category__c = 'In Effect'
     and Apttus__Status__c =
     'Activated'
    and id in: apptusId
 ];

     if (!ApptusAgreementList.isEmpty()) {
   for (Apttus__APTS_Agreement__c ApptusAgreement: ApptusAgreementList) {
      Contract contractsToBeUpdated = New Contract();
    //  contractsToBeUpdated.Id = ApptusAgreement.Id;   //You need to link Contract and aggrement  
   contractsToBeUpdated.Name = ApptusAgreement.Name;
    contractsToBeUpdated.Status = ApptusAgreement.Apttus__Status__c;
     contractsToBeUpdated.Status_Category__c =
        ApptusAgreement.Apttus__Status_Category__c;
    contractsToBeUpdatedList.add(contractsToBeUpdated);
   }
 }
  if (!contractsToBeUpdatedList.isEmpty()) {
   update ontractsToBeUpdatedList;
 }
 }
5
  • Accdg to you, after update, so this should be in? Is it correct? if(trigger.isUpdate || trigger.isAfter){ APT_AgreementSyncTriggerHandler.onAfterUpdate(trigger.oldMap, trigger.newMap); } Sep 5 '18 at 11:13
  • I just Updated my answer. "OnafterUpdate"its a method name...you can any specific name Sep 5 '18 at 11:18
  • The Edit was successful however, there is clone Salesforce contract record was not created. What I have missed here? Sep 5 '18 at 11:22
  • How to make the assigning of values to clone dynamic? Sep 7 '18 at 2:52
  • How to make the assigning of values to clone record dynamic? Sep 7 '18 at 4:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.