• Keith Ashley
  • NEWBIE
  • 10 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
Hi All,
I have written the trigger for changing the case owner field from livechattranscript object  case owner field.If one chat request came and agent accept that chat and after that agent assign that request to new agent.If we assign the same case to another agent.when request came to another agent and agent accept that chat then transcript case owner field is changing to "who accept the Chat" but when case is linked to that chat then case owner field is not changing to current agent name .Please anyone suggest how we can write the trigger for that .I have written the trigger but its not working.
when chat request is coming then its not associating case to that chat.
My code below.

trigger LiveChatTranscriptTrigger on LiveChatTranscript (after insert) {

    if(Trigger.isInsert && Trigger.isAfter){        
        Set<String> caseIds = new Set<String>();
        for ( LiveChatTranscript transcript : Trigger.new ) {
            if ( !String.isBlank( transcript.caseId ) ) {
                caseIds.add( transcript.caseId );
                system.debug(caseIds);
            }
            
        }
        System.debug('CaseIds' +caseIds);
        if ( caseIds.size() > 0 ) {
            List<Case> cases = new List<Case>([SELECT id from Case WHERE Id IN :caseIds ORDER BY createdDate ASC ]);            
            Map<String, Case> CasesIdsMap = new Map<String, Case>();
            System.debug('2');
            for ( Case cs : cases ) {
                CasesIdsMap.put( cs.id, cs );
            }
            System.debug( 'CasesIdsMap=' + CasesIdsMap );
            List<case> caseToUpdate = new List<case>();
            
            for ( LiveChatTranscript transcript : Trigger.new ) {
             System.debug('3');
                if ( !String.isBlank( transcript.caseId ) ) {
                    Case cs = CasesIdsMap.get( transcript.caseId );
                    if ( cs != null ) {
                        caseToUpdate .add( new case(
                        ownerId=transcript.ownerId,
                            id = transcript.caseid
                            //caseId = cs.id
                            
                        ));
                    }
                    
                }
                
            }
            if ( caseToUpdate .size() > 0 ) {
                System.debug( 'caseToUpdate : ' +caseToUpdate );
                 update caseToUpdate ;
            }
            
        }
    }
    
}
And Trigger is not genering the log also
For the above qus I wrote the below code : 

trigger ClosedOpportunityTrigger on Opportunity (after insert,after update) {

List<Task> t1=new List<Task>();


for (opportunity a :[select ID,StageName  from opportunity where StageName='Closed Won' and ID IN :Trigger.New])
{


t1.add(new Task(subject= 'Follow Up Test Task', WhatId=a.ID));

if(t1.size() >0 && t1.size()<=200)
{
insert t1;
}
}
}



But getting a error like   :
Challenge Not yet complete... here's what's wrong: 
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, trig_conts_opprty: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0 with id 0032800000QkI6aAAF; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] Trigger.trig_conts_opprty: line 10, column 1: []


Anyone please tell me the possible reason for the error ?