• Kiran Kumar 690
  • NEWBIE
  • 5 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 4
    Replies

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.
we have two object transcation and balance with lookup relationship,
when ever balance child records is more than 50 which is associated to transtion record
if i try to add 51 need to show error . how can i acheive it ?
trigger UpdateItemRollup on Balance__c (after delete, after insert, after update) {

    Set<id> TransactionIds = new Set<id>();
    List<Transaction__c> tr = new List<Transaction__c>();
    List<Balance__c> ba = new List<Balance__c>();

    for (Balance__c item : Trigger.new)
        TransactionIds.add(item.Transaction__c);

    if (Trigger.isUpdate || Trigger.isDelete) {
        for (Balance__c item : Trigger.old)
            TransactionIds.add(item.Transaction__c);
    }

    // get a map of the traments with the number of items
    Map<id,Transaction__c> TransactionMap = new Map<id,Transaction__c>([select id, RollUp_Count__c from Transaction__c where id IN :TransactionIds]);

    // query the traments and the related inventory items and add the size of the inventory items to the trament's RollUp_Count__c
    for (Transaction__c tra : [select Id, Name, RollUp_Count__c,(select id from Balance__r) from Transaction__c where Id IN :TransactionIds]) {
        TransactionMap.get(tra.Id).RollUp_Count__c = tra.Balance__r.size();
        // add the value/trament in the map to a list so we can update it
        tr.add(TransactionMap.get(tra.Id));
    }

    update tr;

}
Showing error at relationship error
 
[select Id, Name, RollUp_Count__c,(select id from Balance__r) from Transaction__c where Id IN :TransactionIds]) {

we cannot want to install third paty rollup helper or similar ..

Thanks on advance
 
why is list ordered and map/set are unordered??
can someone explain in detail.i know that list is ordered and set is unordered