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
munna321munna321 

Help for trigger

Hi All,


 Appreciate if you could help me correct this trigger

When a custom field(Is_Serviceable__c) on the custom object (Product_Master__c) is true, then create or update the fields on Asset by the fields of custom object(Contract_Product__c).

trigger UpdateAsset on Product_Master__c (after insert, after update) {

        //List Asset to insert, update

      List<Asset> insertASList      = new List<Asset>();

      List<Asset> updateASList      = new List<Asset>();

 

      for(Product_Master__c pm:trigger.new){

      for(Contract_Product__c cp:trigger.new){

     //check for the checkbox

      if(trigger.isinsert && trigger.isafter && pm.Is_Serviceable__c==true){

     cp.add(new Contract_Product__c(Contract_Start_Date__c=AS.Contract_Start_Date__c, Contract_End_Date__c=AS.Contract_End_Date__c));

      }

     //check for the checkbox update on Product_Master__c

      if(trigger.isupdate && trigger.isafter && pm.Is_Serviceable__c==true){

     cp.add(new Contract_Product__c(Contract_Start_Date__c=AS.Contract_Start_Date__c, Contract_End_Date__c=AS.Contract_End_Date__c));

      }  

    }

      }

    insert insertASList;

    update updateASList;

 

}

 

}

trustchristrustchris

Hi Munna

 

A couple of pointers.

 

You can't use the trigger.new method to access Contract_Product__c information directly, as the trigger is firing on the Product Master object.  You will need to run a soql query to return this related information.

 

Your asset lists that are declared, are not used in your processing.  so you are trying to insert null objects with your DML statements.  in your 'for' loops, you are trying to access varaible AS, which has not been declared.

 

Try writing your trigger using the following process.

 

  • Declare a list to hold new Assets (one will do)
  • Run an soql quey to return any Contract_Product__c records that are related to the Product Master records([select name from contract_product__c where product_master__c.id in: triggernewMap.keyset()] ).  And assign records returned to a list.(do this query outside any loops to prevent hitting govenor limits)
  • Loop through the Product master records in your trigger, checking to see if the is servicable box has been checked/updated (you'll need to use trigger.isInsert/Update to do this effectively). where the field has been checked, loop through all Contract_Product records looking for a match with the Product master(use the Product Master Id to evaluate).  where you find a match, create a new asset record using the Contract Product in context and add it to the asset list.
  • Once you have looped through all Product Master records, insert the Asset list with the appropriate DML statement

Hope this information helps.  Good luck!

munna321munna321

Thanks a ton for ur reply, actually i'm very new to write any code... can you please correct the trigger code here that would be very helpful..  Thanks again.

 

 

When a custom lookup field on opportunity(Contract__c) is updated then some fields from Contract standard object should autofill the fields on opportunity.


 

trigger UpdateOpportunity on Opportunity (after insert, after update) {
 //List opportunity to insert, update
 List<opportunity> insertOPList = new List<Opportunity>();
 List<opportunity> updateOPList = new List<Opportunity>();
 for(Contract co:trigger.new){
     //check for the contract lookup on Opprtuinty
      if(trigger.isinsert && trigger.isafter && OP.Contract__c==true){
     OP.add(new Opportunity(Contract_Start_Date__c=co.StartDate, Contract_End_Date__c=co.EndDate));
      }
     //check for the contract lookup update on Opprtuinty
      if(trigger.isupdate && trigger.isafter && OP.Contract__c==true){
     OP.add(new Opportunity(Contract_Start_Date__c=co.StartDate, Contract_End_Date__c=co.EndDate));
      }   
 }
    insert insertOPList;
    update updateOPList;
    
  }

 

munna321munna321

Please help me with these two triggers... I would be really grateful to you...

 

When a custom field(Is_Serviceable__c) on the custom object (Product_Master__c) is true, then create or update the fields on Asset by the fields of custom object(Contract_Product__c).

 

trigger UpdateAsset on Product_Master__c (after insertafter update) {

        //List Asset to insert, update

      List<Asset> insertASList      = new List<Asset>();

      List<Asset> updateASList      = new List<Asset>();

 

      for(Product_Master__c pm:trigger.new){

      for(Contract_Product__c cp:trigger.new){

     //check for the checkbox

      if(trigger.isinsert && trigger.isafter && pm.Is_Serviceable__c==true){

     cp.add(new Contract_Product__c(Contract_Start_Date__c=AS.Contract_Start_Date__c, Contract_End_Date__c=AS.Contract_End_Date__c));

      }

     //check for the checkbox update on Product_Master__c

      if(trigger.isupdate && trigger.isafter && pm.Is_Serviceable__c==true){

     cp.add(new Contract_Product__c(Contract_Start_Date__c=AS.Contract_Start_Date__c, Contract_End_Date__c=AS.Contract_End_Date__c));

      }  

    }

      }

    insert insertASList;

    update updateASList;

 

}

 

}

Imran MohammedImran Mohammed

You are trying to access Contract_Start_Date__c using AS variable but you haven't declared that.

You did not add any Asset to insertAsList or updateAslist.

Can you be liittle more clear with what is contrat_product__c and Asset object ?

 

munna321munna321

I gave some sample field names...

 

Actually my trigger should be on Product_Master__c object, when a checkbox field updates on Product_Master__c (ie., Is_Serviceable=true) then a Asset is created or updated with contract_Product__c fields. 

 

Contract_Product__c is a object, line item to contract object. Product_Master__c is lookup to Contract_Product__c.

 

Thank you for the reply... please help me with the trigger...