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
Venkat Reddy sfdVenkat Reddy sfd 

Trigger not inserting record

Hi all,
Below error I am getting when I am trying to save my Trigger. please any one could  help me.
Error: Field is not writeable: METADATA_Source__c.SOURCE_CODE__c

My code is here:
trigger BrandTrigger on METADATA_Brand__c (After insert, after delete, after undelete,  before delete,after update, before insert, before update) {
  TACTHelper.setStatus();
  
   if(Trigger.isInsert && Trigger.isAfter){
    List<Metadata_Campaign__c> NewCampaign= new list<Metadata_Campaign__c>();
    List<METADATA_Source__c> NewSource= new list<METADATA_Source__c>();
    Map<string,string> Map1 = new Map<string,string>();
    List<Metadata_Program__c> Program= [select id,name from Metadata_Program__c where Program_ID__c='54'];
    List<METADATA_Therapy_Class__c> therapy=[select id,name from METADATA_Therapy_Class__c where Therapy_Class_ID__c='6'];
    String s1,s2,s3,s4;
    
    List<METADATA_Source__c> Sr=[SELECT SOURCE_CODE__c from METADATA_Source__c where SOURCE_CODE__c like 'CGI%' ORDER BY SOURCE_CODE__c DESC ];

     for(METADATA_Brand__c brand : Trigger.new){
      Metadata_Campaign__c camp= new Metadata_Campaign__c();
      METADATA_Source__c Sourc= new METADATA_Source__c();
        
        if(Program.size() > 0){
         camp.Program__c=Program[0].Id;
       }
        if(therapy.size() > 0){
         camp.Therapy_Class__c=therapy[0].Id;
       }
         camp.Name=brand.Name;
         camp.Campaign_Type__c='CIC';
         camp.Description__c=brand.Name +'CIC General Information';
         camp.Metadata_Status__c='A';
         camp.DISPLAY_FLAG__c='Y';
         camp.Branded__c='Yes';
         camp.USMM__c='No';
         camp.HCP__c='No' ;
         Newcampaign.add(camp);
         
         Integer k=1;
         for(METADATA_Source__c sorce: Sr){
         	Integer l=1;
         //	System.debug(sorce.);
         	S1= sorce.SOURCE_CODE__c;
         	S2=S1.Substring(5,S1.length());
         	Integer M= integer.valueof(S2);
         	Integer J=M+1;
         	
         if(Newcampaign.size() > 0){
         	Sourc.Campaign__c=Newcampaign[0].Id;
         }
         if(J.size()> 0){
         Sourc.SOURCE_CODE__c='CGIPH'+J;  // Showing ERROR Here

         Sourc.Name='Trigger'; // This has to be Delete. there is no Field called Source Name in Source Object
         Sourc.SOURCE_DESCRIPTION__c= brand.Name +'CIC General Information';
         Sourc.MEDIUM_TYPE__c='PH';
         Sourc.MEDIA_SOURCE_CODE__c='PHONE';
         Sourc.PURPOSE_CODE__c='CS';
         Sourc.ORIGIN__c='IRMA';
         Sourc.METADATA_STATUS__c='A'; // Probably this is default no need to mentioned here  
         NewSource.add(Sourc);
     }
     }
     }
      
  insert NewCampaign;
  insert NewSource;
   }
}

Trigger can able to insert record into Metadata_campaign__c and Unable to insert record into  METADATA_Source__c.
Please any one can help.. its Urgent
Advance Thanks...
BalajiRanganathanBalajiRanganathan
what is the data type of SOURCE_CODE__c field? if it is formula or auto number, you will not able to set it. remove the line of code or change the field type
Venkat Reddy sfdVenkat Reddy sfd
Hi Balaji, 

Thanks for the Reply, Here field data type is Text but I am trying to Insert Number value. As per Salesforce default functionlity text data type will allow numbers also. I guess this in not a problem.. could you please recheck and let me know.
BalajiRanganathanBalajiRanganathan
but your data type of SOURCE_CODE__c is text or Formula (Text)?. if it formula you will not able to wite it.