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
3C3C 

Trigger to insert custom obj record based on opportunity

I have the following trigger, but it's not working as intended. There are no errors in the code.. any ideas?
 
trigger createCancellationReport on Opportunity (after update) {

    List<Win_Loss_Report__c> reportstoinsert = new List<Win_Loss_Report__c>();

    for (Opportunity opp : Trigger.new) {

        // Create cancellation report only for cancellation oppys
        if (opp.StageName == 'Closed Won' & opp.Cancellation_Reason__c != null) {
            Win_Loss_Report__c cr = new Win_Loss_Report__c();
            cr.Opportunity__c   = opp.id;
            cr.Name = 'Cancellation Report';
            cr.RecordTypeID = '012J00000009NC9';
            reportstoinsert.add(cr); // bulk process
        } 
    } 
}

 
Best Answer chosen by 3C
Vishnu VaishnavVishnu Vaishnav
HI,

Here is modified code :

trigger createCancellationReport on Opportunity(after update) {
    List < Win_Loss_Report__c > reportstoinsert = new List < Win_Loss_Report__c > ();
    for (Opportunity opp: Trigger.new) {
        // Create cancellation report only for cancellation oppys
        if (opp.StageName == 'Closed Won' & opp.Cancellation_Reason__c != null) {
            Win_Loss_Report__c cr = new Win_Loss_Report__c();
            cr.Opportunity__c = opp.id;
            cr.Name = 'Cancellation Report';
            cr.RecordTypeID = '012J00000009NC9';
            reportstoinsert.add(cr); // bulk process
        }
    }
    if(reportstoinsert.size() > 0){
      insert reportstoinsert;
    }
}
:::======================================================================:::
Qusetion Solved ? then mark as best answer to make helpful to others .....

All Answers

Vishnu VaishnavVishnu Vaishnav
HI,

Here is modified code :

trigger createCancellationReport on Opportunity(after update) {
    List < Win_Loss_Report__c > reportstoinsert = new List < Win_Loss_Report__c > ();
    for (Opportunity opp: Trigger.new) {
        // Create cancellation report only for cancellation oppys
        if (opp.StageName == 'Closed Won' & opp.Cancellation_Reason__c != null) {
            Win_Loss_Report__c cr = new Win_Loss_Report__c();
            cr.Opportunity__c = opp.id;
            cr.Name = 'Cancellation Report';
            cr.RecordTypeID = '012J00000009NC9';
            reportstoinsert.add(cr); // bulk process
        }
    }
    if(reportstoinsert.size() > 0){
      insert reportstoinsert;
    }
}
:::======================================================================:::
Qusetion Solved ? then mark as best answer to make helpful to others .....
This was selected as the best answer
3C3C
Thanks!