+ Start a Discussion
farah sheriffarah sherif 

hello guys the below trigger is not working can anyone help

this is the requirment:
When Cancellation_Received_Date__c is populated on an AssetRecord,

Copy Actual_Cancellation_Date__c from the Asset Object

To Actual_Cancellation_Date__c on the OpportunityLineItem Object

Use Opportunity_Product_ID__c on the Asset Object to determine which Opportunityline item to update


trigger CopyDateTrigger on Asset (after update, after delete) {

    map<id,date> toupdate = new map<id,date>();
    map<id,id> toupdate2 = new map<id,id>();
    map<Asset,id> toupdate3 = new map<Asset,id>();
    //set<id> opp = new set<id>();
    for(Asset a:trigger.new){
        
        if(a.Cancellation_Received_Date__c  != null){
            toupdate.put(a.Opportunity_Product_ID__c,a.Actual_Cancellation_Date__c);

            toupdate2.put(a.Opportunity__c,a.Opportunity_Product_ID__c);
            toupdate3.put(a,a.Opportunity_Product_ID__c);
        }
    }
    if(!toupdate.isEmpty() ){
        List<OpportunityLineItem> o = [select Casesafeid__c,Actual_Cancellation_Date__c from OpportunityLineItem where Casesafeid__c in:toupdate.keySet()];
    
        
        for(OpportunityLineItem opp:o){
            for(Asset ast:toupdate3.keySet()){
             
                if(ast.Opportunity_Product_ID__c == opp.Casesafeid__c){
                    opp.Actual_Cancellation_Date__c  =ast.Actual_Cancellation_Date__c;
                }
            }
    }
    }
}

 
Best Answer chosen by farah sherif
Raj VakatiRaj Vakati
Try this
 
rigger CopyDateTrigger on Asset (after update, after delete) {

    map<id,date> toupdate = new map<id,date>();
    map<id,id> toupdate2 = new map<id,id>();
    map<id,Asset> toupdate3 = new map<id,Asset>();
    //set<id> opp = new set<id>();
    for(Asset a:trigger.new){
        
        if(a.Cancellation_Received_Date__c  != null){
            toupdate.put(a.Opportunity_Product_ID__c,a.Actual_Cancellation_Date__c);

            toupdate2.put(a.Opportunity__c,a.Opportunity_Product_ID__c);
            toupdate3.put(a.Opportunity_Product_ID__c,Asset);
        }
    }
    if(!toupdate.isEmpty() ){
        List<OpportunityLineItem> o = [select Casesafeid__c,Actual_Cancellation_Date__c from OpportunityLineItem where Casesafeid__c in:toupdate.keySet()];
    
        
        for(OpportunityLineItem opp:o){
            for(Id ast:toupdate3.keySet()){
             
                if(toupdate3.get(ast).Opportunity_Product_ID__c == opp.Casesafeid__c){
                    o.Actual_Cancellation_Date__c  =ast.Actual_Cancellation_Date__c;
                }
            }
    }
	
	update o ;
    }
}

 

All Answers

Raj VakatiRaj Vakati
Try this
 
rigger CopyDateTrigger on Asset (after update, after delete) {

    map<id,date> toupdate = new map<id,date>();
    map<id,id> toupdate2 = new map<id,id>();
    map<id,Asset> toupdate3 = new map<id,Asset>();
    //set<id> opp = new set<id>();
    for(Asset a:trigger.new){
        
        if(a.Cancellation_Received_Date__c  != null){
            toupdate.put(a.Opportunity_Product_ID__c,a.Actual_Cancellation_Date__c);

            toupdate2.put(a.Opportunity__c,a.Opportunity_Product_ID__c);
            toupdate3.put(a.Opportunity_Product_ID__c,Asset);
        }
    }
    if(!toupdate.isEmpty() ){
        List<OpportunityLineItem> o = [select Casesafeid__c,Actual_Cancellation_Date__c from OpportunityLineItem where Casesafeid__c in:toupdate.keySet()];
    
        
        for(OpportunityLineItem opp:o){
            for(Id ast:toupdate3.keySet()){
             
                if(toupdate3.get(ast).Opportunity_Product_ID__c == opp.Casesafeid__c){
                    o.Actual_Cancellation_Date__c  =ast.Actual_Cancellation_Date__c;
                }
            }
    }
	
	update o ;
    }
}

 
This was selected as the best answer
farah sheriffarah sherif
thanks raj