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
Rafael Franco Moreno 24Rafael Franco Moreno 24 

error vf

I have this error when save text:


Insert failed. First exception on row 0 with id 0035f000008gFMYAA2; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Error is in expression '{!save}' in component <apex:commandButton> in page mensaje: Class.mensajeController.save: line 25, column 1
An unexpected error has occurred. Your development organization has been notified.

User-added image
<apex:page controller="mensajeController"  >
<apex:form >
 <apex:pageBlock >
 <apex:pageBlockButtons >
   <apex:commandButton value="Process Selected" action="{!processSelected}" rerender="test"/>
  </apex:pageBlockButtons>
  <apex:pageBlockTable value="{!contacts}" var="c" >
   <apex:column >
    <apex:inputCheckbox value="{!c.selected}"/>
   </apex:column>
    <apex:column value="{!c.con.Name}" />
    <apex:column value="{!c.con.Email}" />
    <apex:column value="{!c.con.Phone}" />
  </apex:pageBlockTable>
 </apex:pageBlock>
    <apex:pageBlock >
        <!--<p>Current description: {!mensajeContenido}</p>-->
        <p>Current description: {!oMessageDescription}</p>
        <p>Change description to:</p> 
        <!--<apex:inputTextarea id="newDesc" value="{!mensajeContenido}"/><p/>-->
        <apex:inputTextarea id="newDesc" value="{!oMessageDescription}"/><p/>
        
        <apex:commandButton value="Save" action="{!save}"/>
    </apex:pageBlock>
 <apex:pageBlock id="test">
  Total No of Selected Records :<apex:outputText value="{!value }"/>
  <apex:pageBlockTable value="{!SelectedContacts}" var="c" >
    <apex:column value="{!c.Name}" />
    <apex:column value="{!c.Email}" />
    <apex:column value="{!c.Phone}" />
  </apex:pageBlockTable>
 </apex:pageBlock>
 </apex:form>
</apex:page>
User-added image
I’m saving a text in a vf
but don’t save that text

I have to do this: 
 enter a description of the message and finally be able to save

 when saving what the controller must do is create a message object, with the description that we have entered, for each of the selected accounts
Best Answer chosen by Rafael Franco Moreno 24
Maharajan CMaharajan C
Hi Rafael.

Please update your Save method like below :
 
public Pagereference save(){
	
	system.debug('test==> ');
	List<Contact> listSelected = getSelectedContacts();
	List<Mensaje__c> mensajeList = new List<Mensaje__c>();
	for(Contact contact : listSelected){
		Mensaje__c mensaje = new Mensaje__c();
		mensaje.oMessageDescription__c = oMessageDescription;
		contact.Mensaje__c = oMessageDescription; 
		mensajeList.add(mensaje);
	}
	
	if(!mensajeList.isEmpty()){
		insert mensajeList;
	}
	
	if(!listSelected.isEmpty()){
		upsert listSelected;
	}
	return null;

}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Rafael.

Please update your Save method like below :
 
public Pagereference save(){
	
	system.debug('test==> ');
	List<Contact> listSelected = getSelectedContacts();
	List<Mensaje__c> mensajeList = new List<Mensaje__c>();
	for(Contact contact : listSelected){
		Mensaje__c mensaje = new Mensaje__c();
		mensaje.oMessageDescription__c = oMessageDescription;
		contact.Mensaje__c = oMessageDescription; 
		mensajeList.add(mensaje);
	}
	
	if(!mensajeList.isEmpty()){
		insert mensajeList;
	}
	
	if(!listSelected.isEmpty()){
		upsert listSelected;
	}
	return null;

}

Thanks,
Maharajan.C
This was selected as the best answer
Rafael Franco Moreno 24Rafael Franco Moreno 24
Thank you Maharajan C it works