You need to sign in to do that
Don't have an account?

Cannot Insert a Customized Item in Database - Excep: Insert failed INVALID_FIELD_FOR_INSERT_UPDATE
Shakespeare
Code:
<apex:form id="pixelReqForm"> <apex:pageBlock title="Pixel Request Form" id="pixelReqPageBlock"><br></br> <apex:pageBlockButtons > <apex:commandButton value="Save" action="{!save}" reRender="pixelReqTable"/> </apex:pageBlockButtons> <apex:pageBlockTable value="{!PixelReq}" var="aPixelReq" id="pixelReqTable"> <apex:column headerValue="Agency"> <apex:inputField value="{!aPixelReq.Agency__c}"/> </apex:column> <apex:column headerValue="Advertiser"> <apex:inputField value="{!aPixelReq.Advertiser__c}"/> </apex:column> </apex:pageBlockTable> </apex:pageBlock> </apex:form> </apex:page>
======================================================= public class PixelReqFormController { List<PixelReqForm__c> pixelReqList; public List<PixelReqForm__c> getPixelReq () { return pixelReqList; } public PageReference save() { string contractID = ApexPages.currentPage().getParameters().get('contractID'); //Call to comparer method. If some item is found to be updated, only that item(s) will be
// returned and will be inserted into db as new records. pixelReqList = comparePixReqObjs (pixelReqList); if (pixelReqList != null && pixelReqList.size() > 0) { insert pixelReqList; } else { ApexPages.Message myMsg = new ApexPages.message(ApexPages.Severity.INFO, 'There were no changes to save.'); ApexPages.addMessage(myMsg); } return null; } public List<PixelReqForm__c> comparePixReqObjs (List<PixelReqForm__c> newList) { List<PixelReqForm__c> updateableList = new List<PixelReqForm__c>(); List<PixelReqForm__c> oldList; PixelReqForm__c pixelReqObj; //getting older pixel list from DB oldList = [SELECT isDeleted__c, Agency__c, Advertiser__C, BCN__c, Value__C, Web_Page_Name__c, Web_Page_URL__c, OAS_System__c, Pixel_Usage__c, Tag_Security__c, Tag_Type__c, Special_Requests__c, ContracID__c FROM PixelReqForm__c where ContracID__c = :ApexPages.currentPage().getParameters().get('contractID') AND isDeleted__c = false order by agency__c desc]; //Comparing older and newer list for differences. If used has updated something, it will be
//saved as a new record. Otherwise no save operation will be done.
for (Integer i=0; i<oldList.size(); i++) { if (newList[i].Agency__c != oldList[i].Agency__c) { //initializing the object with new values, giving me the subject error. pixelReqObj = new PixelReqForm__c (Agency__c = newList[i].Agency__c,
Advertiser__c = newList[i].Advertiser__c); updateableList.add(pixelReqObj); } } return updateableList; } }
Just to add, following is the complete error:
System.DmlException: Insert failed. First exception on row 'nth' with id a XXXXXXXXXXX; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Use the Database.Update() operation.
You cant insert object that already exists in the database...
Thanks for your input Gentleman, the same code started working all of a sudden :) .
Solved.
Regards,
Shakespeare