You need to sign in to do that
Don't have an account?
Soundar Rajan Ponpandi
Soundar.
Insert Failed On After Insert
Hi ,
I am getting below Error when i am trying to update a "Contact Name" in Case Object (In After Insert).
Error : PrimaryAssociateType: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0 with id 5001F000000w4LpQAI; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] Trigger.PrimaryAssociateType: line 60, column 1
My Trigger
trigger PrimaryAssociateType on Case_Associate__c (Before Insert, After Insert) { Set<Id> caseId = New Set<Id>(); set<Id> caseId1 = New Set<Id>(); List<case> updtCon = New List<case>(); Map<Id,Case_Associate__c> casMap = New Map<Id,Case_Associate__c>(); if(Trigger.isBefore && Trigger.isInsert){ For(Case_Associate__c ca : Trigger.New){ If(ca.Case_Number__c != Null){ CaseId.add(ca.Case_Number__c); System.debug('Case Number | ' + CaseId); } } IF(CaseId.size() > 0){ List<Case_Associate__c> cas = [Select id,name,Case_Number__c,Associate_Type__c from Case_Associate__c Where Case_Number__c =:CaseId AND Associate_Type__c = 'Primary']; System.debug('Case Associate Type With Primary |' + cas); System.debug('Size Of Primary | ' + cas.Size()); Case_Associate__c cast1 = New Case_Associate__c(); String curPage = ApexPages.currentPage().getUrl(); IF(cas.Size() > 0 && curPage.contains('Primary')){ System.debug('Primary is Already Created'); for(Case_Associate__c cas1 : Trigger.New){ cas1.addError('Primary Associate Type was Already Created. So Please Choose "Secondary" or "Other" '); } } else { ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Case Associate Created Successfully')); System.debug('Secondary || Other'); } } } if(Trigger.isAfter && Trigger.isInsert){ For(Case_Associate__c ca1 : Trigger.New){ IF(ca1.Case_Number__c != Null && ca1.Entity_Type__c == 'Provider' && ca1.Associate_Type__c == 'Primary'){ caseId1.add(ca1.Case_Number__c); casMap.put(ca1.Case_Number__c,ca1); System.debug('Case Number 1 | ' + caseId1); } } System.debug('caseId1.size() | ' + caseId1.size()); IF(caseId1.size() > 0){ for(string irow : casMap.keyset()){ Case c = New Case(id=irow); System.debug('&&&&&&& irow |'+ irow); c.ContactId = casMap.get(irow).Entity1__c; system.debug('Contact Id |' + c.ContactId); updtCon.add(c); } System.debug('&&&&&&&&& Size Of updtCon |' + updtCon.size()); If(updtCon.size() > 0){ Insert updtcon; } } } }Regards,
Soundar.
It seems from the error message that, you are passing accountId instead of contactId to the below statement: Kindly verify from system.debug that, Entity1__c holds the contactid not accountId.
Thanks,
Aman
All Answers
In the above code, It seems that you are trying to insert a record with provided ID, that is impossible because record is created already.
You need to update 'updtcon' list instead of insert.
Thanks,
Aman
Thanks for your quick response.. now i have update 'updtcon', now it's showing a below Error
Error : PrimaryAssociateType: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 5001F000000w4LpQAI; first error: FIELD_INTEGRITY_EXCEPTION, Contact ID: id value of incorrect type: 0011F000003SweGQAS: [ContactId] Trigger.PrimaryAssociateType: line 62, column 1
Regards,
Soundar.
It seems from the error message that, you are passing accountId instead of contactId to the below statement: Kindly verify from system.debug that, Entity1__c holds the contactid not accountId.
Thanks,
Aman
Make sure that your map of key will return of type Id .But you defined as String try to change it from String to Id.
Regards,
Kiran
Entity was Accoount, I found my mistake and rectified as well.
Regards,
Soundar.