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
Sudhir_MeruSudhir_Meru 

Update is not happening in Trigger

Hi, 

  I wrote a trigger below to update opportuntiy from custom object based on condition but is not updating please help

 Code below highlighted in bold is not updating

trigger Temp_Asset2_Opp on Temp_Assets__c (after insert )
{
    List<OpportunityLineItem> OppLineItems = new List<OpportunityLineItem>();
   
    List<Temp_Assets__c> CurrentAssetOpp = [Select Id, Name, Asset_Id__c, Serial_Number__c, AccountId__c, Product__c, Service_Start_Date__c,
                                              Service_End_Date__c ,Install_Date__c ,Reseller__c,Distributor__c ,
                                              Incumbent_Reseller__c,Education__c,Expiry_Date__c,Existing_Opportunity__c,
                                              New_Opportunity__c,Expiry_Term__c,Bundle_Support__c, X5_Year_SKU_Code__c, X3_Year_SKU_Code__c,
                                              X1_Year_SKU_Code__c, Monthly_SKU_Code__c, X5_year_SKU__c ,X3_year_SKU__c, X1_year_SKU__c,
                                              Total_in_Months__c, Support_Only_5_Year_SKU__c, Support_Only_3_Year_SKU__c, Support_Only_1_Year_SKU__c,
                                              Support_Only_Monthly_SKU__c, DaysRemaining_Months__c
                                              From Temp_Assets__c
                                              Where CreatedById = :userinfo.getUserId() Limit 1];
   
    List<Temp_Assets__c> CurrentAssetOppLine = [Select Id, Name, Asset_Id__c, Serial_Number__c, AccountId__c, Product__c, Service_Start_Date__c,
                                                Service_End_Date__c ,Install_Date__c ,Reseller__c,Distributor__c ,
                                                Incumbent_Reseller__c,Education__c,Expiry_Date__c,Existing_Opportunity__c,
                                                New_Opportunity__c,Expiry_Term__c,Bundle_Support__c, X5_Year_SKU_Code__c, X3_Year_SKU_Code__c,
                                                X1_Year_SKU_Code__c, Monthly_SKU_Code__c, X5_year_SKU__c ,X3_year_SKU__c, X1_year_SKU__c,
                                                Total_in_Months__c, Support_Only_5_Year_SKU__c, Support_Only_3_Year_SKU__c, Support_Only_1_Year_SKU__c,
                                                Support_Only_Monthly_SKU__c, DaysRemaining_Months__c
                                                From Temp_Assets__c
                                                Where CreatedById = :userinfo.getUserId()];

    Pricebook2 prBook= [select id from Pricebook2 where Name=: 'NAM Price Book'];
    RecordType renewal_RT = [Select id, Name, sObjectType from RecordType where sObjectType= 'Opportunity' and Name = 'Renewal Opportunity' ];
   
   Opportunity Opp = new Opportunity();
  
   for ( Temp_Assets__c TA : CurrentAssetOpp )
   {   
     // If New Opportunity is Created 
    if ( TA.Existing_Opportunity__c == NULL && TA.New_Opportunity__c != NULL )   
     {
            // Insert Opportunity with all mandatory fields       
             Opp.Name      = TA.New_Opportunity__c;            
             Opp.Type      = 'Existing Customer';
             Opp.AccountId = TA.AccountId__c;
             Opp.CloseDate =  system.today().addDays(+30); // default Sysdate + 30
             Opp.Government_Contract__c = 'None';
             Opp.Renewal_Opportunity__c = 'Yes';
             Opp.StageName = 'Renewal';
             //Opp.Lost_Reason__c = 'Other'; // not required
             Opp.Primary_Competitor__c = 'No Competitor';
             Opp.ForecastCategoryName = 'Pipeline';
             Opp.LeadSource = 'Renewal';
             Opp.Primary_Reseller__c    = TA.Reseller__c;
             Opp.Primary_Distributor__c = TA.Distributor__c;
             Opp.Renewal_Incumbant_Reseller__c = TA.Incumbent_Reseller__c;
             Opp.Renewal_K_12__c   =  TA.Education__c;
             //Assigning the new Opportunity to the Renewal Record Type
             opp.recordtypeId = renewal_RT.id;
            
          Insert Opp;
      }
    
     
    if ( TA.Existing_Opportunity__c == NULL && TA.New_Opportunity__c != NULL )   
     {
        List<Opportunity> OpptUp = [select Id from Opportunity where id =  :TA.Existing_Opportunity__c ];       
       
         for ( Opportunity OppUpd : OpptUp ) {
                OppUpd.test__c = 'Sudhir';
                OppUpd.Primary_Reseller__c    = TA.Reseller__c;
                OppUpd.Primary_Distributor__c =  TA.Distributor__c;
                OppUpd.Renewal_Incumbant_Reseller__c =  TA.Incumbent_Reseller__c;
                OppUpd.Renewal_K_12__c   =  TA.Education__c;
                //change the RecordType of this existing Opportunity to Renewal Opportunity Record Type
                OppUpd.recordtypeId = renewal_RT.id;                  
            }  
         Update OpptUp;     

      }   
          
    }    
     
List<Temp_Assets__c> TempAsset = [SELECT Id From Temp_Assets__c Where CreatedById = :userinfo.getUserId()];
//delete TempAsset ;

}

Thanks

Sudhir

Best Answer chosen by Sudhir_Meru
Michael DsozaMichael Dsoza
HI Sudhir,

if ( TA.Existing_Opportunity__c == NULL && TA.New_Opportunity__c != NULL )  
     {
        List<Opportunity> OpptUp = [select Id from Opportunity where id =  :TA.Existing_Opportunity__c ];

Please check above line of code, first you are checking TA.Existing_Opportunity__c as NULL and then passing the same field as ID ???

Definitely you will get nothing because Id field can not be null.

Regards,
Michael

All Answers

Michael DsozaMichael Dsoza
Hi Sudhir,

To update opportunity, update event should also be there.
trigger Temp_Asset2_Opp on Temp_Assets__c (after insert, after update)

Regards,
Michael
Sudhir_MeruSudhir_Meru
Hi Michael,

   I modifed as suggested still its not working. Please check the update code is it correctly written 

Thanks
Sudhir
Michael DsozaMichael Dsoza
Hi Sudhir,

First of all, I would like to clarify one thing, How are you checking update opportunity ???
By simply updating opportunity record or by inserting Temp_Assets__c record. 

As per your trigger, on insertion of custom object record, you also want to update opportunity. is this correct ??

Regards,
Michael
Sudhir_MeruSudhir_Meru
Yes Temp_Assets__c is a temporary object based on two field 
 
 Existing_Opportunity__c means it will update 
 New_Opportunity__c  means it should create a new opportunity

Thanks
Sudhir
Michael DsozaMichael Dsoza
HI Sudhir,

if ( TA.Existing_Opportunity__c == NULL && TA.New_Opportunity__c != NULL )  
     {
        List<Opportunity> OpptUp = [select Id from Opportunity where id =  :TA.Existing_Opportunity__c ];

Please check above line of code, first you are checking TA.Existing_Opportunity__c as NULL and then passing the same field as ID ???

Definitely you will get nothing because Id field can not be null.

Regards,
Michael
This was selected as the best answer
Sudhir_MeruSudhir_Meru
Thanks Michael for helping me to resolve this issue.