+ Start a Discussion
wcwill978wcwill978 

Compile Error: Field is not writeable: Win_Loss_Analysis__c.Id at line 72 column 21

Hi I have a trigger to create a win loss analysis when the opportunity stage is set to closed lost and the win loss reason field is set to one of the three values (price, product feature, relationship). When I try to save the trigger I get the following error "Compile Error: Field is not writeable: Win_Loss_Analysis__c.Id at line 72 column 21" I want to create the opportunity, but is a win loss analysis record already exists and if the reason is changed I want the object to be updated with the new value rather than create a new win loss analysis. Can some one help me figure this error out so I can get this running please:

 

trigger AutoCreateWinLossAnalysisOnOpportunity on Opportunity (after update)
{

  List<Opportunity> closedOps = new List<Opportunity>();
  List<Win_Loss_Analysis__c> newwinLossObjects = new List<Win_Loss_Analysis__c>();  
  List<Win_Loss_Analysis__c> updatewinLossObjects = new List<Win_Loss_Analysis__c>();
 
  List<RecordType> recordTypesForOppty = new List<RecordType>();
  recordTypesForOppty = [Select Name, ID From RecordType where SobjectType = 'Win_Loss_Analysis__c'];
    List<Id> ExistingIds = new List<Id>();
    
     
    Integer  count = Trigger.new.size();
    for(Opportunity newvalue : Trigger.new)
    {
      Opportunity oldvalue = Trigger.oldMap.get(newvalue.Id);
        if(oldvalue.StageName != newvalue.StageName || oldvalue.Loss_Analysis__c != newvalue.Loss_Analysis__c)  
       {
           if(newvalue.StageName == 'Closed Lost'){
            if(newvalue.Loss_Analysis__c == 'Price'
              || newvalue.Loss_Analysis__c == 'Product Feature/Functionality'
              || newvalue.Loss_Analysis__c =='Relationship')
            {
               closedOps.add(newvalue);
                 ExistingIds.add(newvalue.Id);
       
            }
          }
       }
    }
    system.debug('Found : ' + closedOps.size() + ' Opps that need analysis records created');
    if (closedOps.size() > 0)  
    {
      List<Win_Loss_Analysis__c> existing = [SELECT Id,Opportunity_Name__c FROM Win_Loss_Analysis__c WHERE  Opportunity_Name__c  in :ExistingIds];
      system.debug('Found : ' + existing.size() + ' Existing Win Loss Records');
      string LossAnalysis = null;
      for (Opportunity oppty : closedOps)
      {       
              // get the recordTypeId
              Id WinLossObjectRecordTypeId = null;
              string typeName;
              LossAnalysis = oppty.Loss_Analysis__c.toLowerCase();
          for (RecordType recordType : recordTypesForOppty)
          {
            typeName = recordType.Name.toLowerCase();
            if (LossAnalysis == 'price' && typeName == 'price')
            {
              WinLossObjectRecordTypeId = recordType.Id;
            }
            if (LossAnalysis == 'product feature/functionality' && typeName == 'productfeature')
            {
              WinLossObjectRecordTypeId = recordType.Id;
            }
            if (LossAnalysis == 'relationship' && typeName == 'relationship')
            {
              WinLossObjectRecordTypeId = recordType.Id;
            }
          }
          system.debug('Record type id: ' + WinLossObjectRecordTypeId + ' found for oppt id' + WinLossObjectRecordTypeId );
              // construct the new custom object with the required fields set
              
              Win_Loss_Analysis__c wL = new Win_Loss_Analysis__c();
              wL.Opportunity_Name__c = oppty.Id;
              wL.RecordTypeId = WinLossObjectRecordTypeId;
              wL.Account_Name__c = oppty.AccountId;
              if(existing.size() > 0)
              {
                   for(Win_Loss_Analysis__c exist : existing)
                {
                  if(exist.Opportunity_Name__c == oppty.Id)
                  {

              //the line below is line 72 this is where the error is generating from

                    wL.Id = exist.Id;
                    break;  
                  }              
                }
              }
              
              if(wL.Id == null)
              {
                newwinLossObjects.add(wL);
              }
              else
              {
                updatewinLossObjects.add(wL);
              }
              
      }
    }
    system.debug('Inserting ' + newwinLossObjects.size() + ' new Win Loss Objects' );
    system.debug('Updating ' + updatewinLossObjects.size() + '  Win Loss Objects' );
    if(newwinLossObjects.size() > 0)
         insert newwinLossObjects;
   
    if(updatewinLossObjects.size() > 0)
         update updatewinLossObjects;

}

MandyKoolMandyKool

Hi,

 

The error occurs as you are explicitly trying to write the ID field.

The ID fields are not writable. 

 

If you want you can relate(using lookup relationship) Win Loss Analysis object with Win Loss Analysis objects.

 

 

wcwill978wcwill978

How would I do that? I dont understand, this code was working in our Test Sandbox and when i moved the same thing to DEV Sandbox this error occurred.