+ Start a Discussion
The new LearnerThe new Learner 

Need to update the case when ever the order is created--- urgent

Hi Experts,

i wrote below code after order is created , i need to update the case status 'New'  after any field is updated on case  and i wrote the code like below but  lot of errors, i am not sure how to take case and to update. can you help me pls i wrote the code its totally wrong kindly need a help on this

i am not sure how to use the case here Map<Id, Case> oldMap

if(Trigger.isInsert && Trigger.isAfter){
        OrderHandler.OnAfterInsert(Trigger.new, Trigger.newMap);
}
public static void OnAfterInsert(List<order> lstObject, Map<Id, Case> oldMap)
  {
    
    set<id> caseIds = new set<id>();
    for(order ord : lstObject)
    {
     if(ord.Case__c!= null)
     {
       caseIds.add(ord.Case__c);
     }
     }
     if(caseIds != null)
     {
      
      for(Case obj : caseIds)
        {
            for (String fieldName: schemaFieldMap.keySet())
                {
                 if((obj.get(fieldName) != oldMap.get(obj.Id).get(fieldName)))
                {
                    (obj.status=='Partially Complete')) ? obj.status : 'New';
                    break;
                }
            
                
        }
        }
      
      }
      }
Subramani_SFDCSubramani_SFDC
trigger updateCase on Order(after insert) {
    List<Id> CaseIds=new List<Id>();
	
	//Populate the CaseIds to the List
    for(order mst:Trigger.New){
    if(mst.Case__c!=null){
     CaseIds.add(mst.Case__c)
    }
    }
   
   // Query the related Case record
   Map<Id,Case> caseMap = new Map<Id,Case>([select id,status from Case where id in:CaseIds]);
   
   for(order mst:Trigger.New)
   {
		if(!caseMap.IsEmpty())
		{
			caseMap.get(mst.Case__c).status= 'New';
		}
   }
   // Update case record
   if(!caseMap.IsEmpty())
   {
		update caseMap.values();
   }
   

}

 
Subramani_SFDCSubramani_SFDC
1.You can use this trigger logic to implement handler for whenever order is created.
2.you can setup workflow with criteria whenever the case is created/edited
use for below formula in condition 
ischanged(lastmodifieddate)
NOT(TEXT(STATUS)='NEW')

Immediate Action: 
Update Record That Started This Process
Update Case Status Field to "New" 
Raj VakatiRaj Vakati
try this code .. please modify based on requirement 
 
public static void OnAfterInsert(List<order> lstObject, Map<Id, order> oldMap)
{

set<id> caseIds = new set<id>();
List<Case> casestobeUpdated = new List<Case>();
for(order ord : lstObject)
{
 if(ord.Case__c!= null)
 {
	 if(ord.Case__r.status !=oldMap.get(old.Id).Case__r.status){
   caseIds.add(ord.Case__c);
	 }
 }
 }
 if(caseIds != null)
 {
 List<Case> cas = [Select id,status from Case where Id in : caseIds];
 
  for(Case obj : cas)
	{
		
if((obj.status=='Partially Complete'))
				{
casestobeUpdated.add(obj);

			}
		
			
	}
	}

update casestobeUpdated ;
}