+ Start a Discussion
srinivasasarmahere1.390217033575374E12srinivasasarmahere1.390217033575374E12 

Trigger to update field 5 days after the status has not being changed

hi, 

I am writing a trigger 

There are tow Objects 
1. Case (standard $Object)
2. Ticket__C (Custom $Object) 

Ticket has a lookup to Case, and a Case can have multiple ticket_C records but a ticket__c can be allocated to one case only. 

The requirement is when all the tickets associated to the case are closed but the case status is not closed after 5 days of the ticket__c being closed. the trigger should change the status of the Case to closed. 


fields on the objects
Case  - Status (Field)
Ticket__C- GLOBAL_Ticket_Status__c (Field)

The trigger should look at all the records of a ticket__c associated to case and check for GLOBAL_Ticket_Status__c== 'Closed' . 
If the status of all records are closed and case status is not changed to close for 5 days then change the status of the Case to closed. 

This is what i came up with 

Trigger Ticket on Ticket__c (after insert, after update) {
    Set<Id> closedTicketIds = new Set<Id>();
     for (Ticket__c ticket: Trigger.new) {
        if (ticket.GLOBAL_Ticket_Status__c == 'closed') {
            closedTicketIds.add(ticket.id);
        }   
    }
    List<Case> ticketForClosing= [SELECT Status FROM Case
                                  WHERE Ticket__c IN: closedTicketIds];
    for (Case c: ticketForClosing) {
        c.Status = 'closed';
    }
    update ticketForClosing;
}


Compile Error: No such column 'Ticket__c' on entity 'Case'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 8 column 34

Any help is appreciated

Thanks
Best Answer chosen by srinivasasarmahere1.390217033575374E12
sfdc_ninjasfdc_ninja
As you said in your own question.  Ticket has a lookup to case.  So on the Ticket record, there is likely a field Case__c.  Since many tickets can be related to the same case, there is no Ticket__c field on the case.  Ticket objects are children of the case, you are trying to look them up as though they are the parent.

I think you are trying to do something more like this

Trigger Ticket on Ticket__c (after insert, after update) {
    Set<Id> closedTicketIds = new Set<Id>();
    for (Ticket__c ticket: Trigger.new) {
     if (ticket.GLOBAL_Ticket_Status__c == 'closed') {
     closedTicketIds.add(ticket.id);
    }  
    }
    List<Ticket__c> ticketForClosing= [SELECT Id, Case__c FROM Ticket__c
                                  WHERE Id IN: closedTicketIds];

    set<Id> caseIDs = new set<Id>();
    for(Ticket__c t : ticketForClosing){
    caseIDs.add(t.Case__c);
    }
    set<Case> casesToUpdate = new set<Case>();
    for (Case c : [Select Id, Status From Case Where Id In : caseIDs]) {
    c.Status = 'closed';
    casesToUpdate.add(c);
    }
    update casesToUpdate;
}


All Answers

Subramani_SFDCSubramani_SFDC
check ur field name in case relationship field (it is ticket__c or some name)...it will solve ur pbm....
srinivasasarmahere1.390217033575374E12srinivasasarmahere1.390217033575374E12
Actually there is no field called Ticket__c in case object. Ticket__c has a look up to case 
sfdc_ninjasfdc_ninja
As you said in your own question.  Ticket has a lookup to case.  So on the Ticket record, there is likely a field Case__c.  Since many tickets can be related to the same case, there is no Ticket__c field on the case.  Ticket objects are children of the case, you are trying to look them up as though they are the parent.

I think you are trying to do something more like this

Trigger Ticket on Ticket__c (after insert, after update) {
    Set<Id> closedTicketIds = new Set<Id>();
    for (Ticket__c ticket: Trigger.new) {
     if (ticket.GLOBAL_Ticket_Status__c == 'closed') {
     closedTicketIds.add(ticket.id);
    }  
    }
    List<Ticket__c> ticketForClosing= [SELECT Id, Case__c FROM Ticket__c
                                  WHERE Id IN: closedTicketIds];

    set<Id> caseIDs = new set<Id>();
    for(Ticket__c t : ticketForClosing){
    caseIDs.add(t.Case__c);
    }
    set<Case> casesToUpdate = new set<Case>();
    for (Case c : [Select Id, Status From Case Where Id In : caseIDs]) {
    c.Status = 'closed';
    casesToUpdate.add(c);
    }
    update casesToUpdate;
}


This was selected as the best answer
srinivasasarmahere1.390217033575374E12srinivasasarmahere1.390217033575374E12
Hi SFDC-Ninja, thank you for the reply

Compile Error: No such column 'Case__c' on entity 'Ticket__c'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 8 column 39.

this is the error Salesforce is throwing

srinivasasarmahere1.390217033575374E12srinivasasarmahere1.390217033575374E12
I have changed the field name and when i am trying to save. this is the error which is thrown 

Compile Error: DML requires SObject or SObject list type: SET<Case> at line 20 column 5

Trigger Ticket on Ticket__c (after insert, after update) {
        Set<Id> closedTicketIds = new Set<Id>();
            for (Ticket__c ticket: Trigger.new) {
             if (ticket.GLOBAL_Ticket_Status__c == 'closed') {
             closedTicketIds.add(ticket.id);
        } 
    }
    List<Ticket__c> ticketForClosing= [SELECT Id, GLOBAL_Goal__c FROM Ticket__c
                                  WHERE Id IN: closedTicketIds];

        set<Id> caseIDs = new set<Id>();
            for(Ticket__c t : ticketForClosing){
            caseIDs.add(t.GLOBAL_Goal__c);
    }
        set<Case> casesToUpdate = new set<Case>();
            for (Case c : [Select Id, Status From Case Where Id In : caseIDs]) {
            c.Status = 'closed';
            casesToUpdate.add(c);
    }
    update casesToUpdate;
}


sfdc_ninjasfdc_ninja
Oh yes, you just have to create a list and then add the set to the list.

List<Case> caseList = new list<Case>();
caseList.addAll(casesToUpdate);
update caseList;
srinivasasarmahere1.390217033575374E12srinivasasarmahere1.390217033575374E12
this is what i have done 

Trigger Ticket on Ticket__c (after insert, after update) {
        Set<Id> closedTicketIds = new Set<Id>();
            for (Ticket__c ticket: Trigger.new) {
             if (ticket.GLOBAL_Ticket_Status__c == 'closed') {
             closedTicketIds.add(ticket.id);
        }
    }
    List<Ticket__c> ticketForClosing= [SELECT Id, GLOBAL_Goal__c FROM Ticket__c
                                  WHERE Id IN: closedTicketIds];

        set<Id> caseIDs = new set<Id>();
            for(Ticket__c t : ticketForClosing){
            caseIDs.add(t.GLOBAL_Goal__c);
    }
        List<Case> casesToUpdate = new List<Case>();
            for (Case c : [Select Id, Status From Case Where Id In : caseIDs]) {
            c.Status = 'closed';
            casesToUpdate.add(c);
    }
    update casesToUpdate;
}

can you please tell me where do i add the code you have sent me. I will chane the list to set
sfdc_ninjasfdc_ninja
What you have done should work.  you are now adding the cases to a list rather than a set, and then updating the list.  This should work.
srinivasasarmahere1.390217033575374E12srinivasasarmahere1.390217033575374E12
i have tested it by

creating a case and tickets and associated the tickets to the case

Changed the status of two ticket recors to close and the thrid records status is open. however the case was closed despote one of its ticket being open 

The requirement was only if all the tickets associated to the case are close then the case status should be closed.




srinivasasarmahere1.390217033575374E12srinivasasarmahere1.390217033575374E12
How can i change the code to make it work. 

sfdc_ninja, can you please help me put

Thank you

sfdc_ninjasfdc_ninja
Your question was around fixing an error.  I did not look at the logic of your trigger.  It might take a decent amount of work to get this to work in the way you want.  I would ask a new question so it doesnt get buried in this thread.

Ask a new question with the working trigger and the requirements.