function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
WikWik 

Error: Compile Error: Comparison arguments must be compatible types: Schema.SObjectField

Hi, 
There are two objects named Opportunity and Equipment. They are related using a Junction object named "Opportunity_Equipment__c".

Whenever there is an Equipment with status as "Pending Sale" and the related Opportunity Stagename is "Closed Lost" or Closed Abandoned", i need to delete the equipment.


trigger cascadeJunctionDelete on Opportunity_Equipment__c (after update) {

    List<Opportunity> oppList = new List<Opportunity>();

    List<Equipment__c> eqList = new List<Equipment__c>();

    for(Opportunity_Equipment__c ed : Trigger.old) {


if((Opportunity.StageName == 'Closed Lost' ||  Opportunity.StageName == 'Closed-Abandoned') && ( Equipment__c.Status__c == 'Pending Sales') ) {
        oppList.add(new Opportunity(id=ed.Opportunity__c));


        eqList.add(new Equipment__c(id=ed.Equipment__c)); }  

    }


    
    if(eqList.size() > 0) {

        delete eqList;

    }

}

But am geeting an error while saving this : 
Error: Compile Error: Comparison arguments must be compatible types: Schema.SObjectField, String at line 10 column 5
i.e. at the highlighted line

Best Answer chosen by Wik
SarfarajSarfaraj
Hi 

I think you need a trigger on Opportunity instead of Opportunity Equipment.

Use this,
 
trigger cascadeJunctionDeleteOpp on Opportunity (after update) 
{
	List<Id> idsOpp = new List<Id>();
	for(Opportunity opp: trigger.new)
		if(opp.StageName == 'Closed Lost' ||  opp.StageName == 'Closed-Abandoned')
			idsOpp.add(opp.Id);
	List<Id> idsEq = new List<Id>();
	List<Equipment__c> eqList = new List<Equipment__c>();
	for(Opportunity_Equipment__c oppEq : [Select Id, Equipment__r.Id From Opportunity_Equipment__c Where Opportunity__c in :idsOpp And Equipment__r.Status__c = 'Pending Sales'])
		eqList.add(oppEq.Equipment__r);
	if(eqList.size() > 0)
		delete eqList;
}

Deactivate the old trigger.

All Answers

Virendra ChouhanVirendra Chouhan
Hi,
the error is occure because in IF statment you write Opportunity.stageName == 'Closed Lost'
So here Opportunity is SObject not a instance of SObject.
First you have to query to access the fields of Related object.
Virendra ChouhanVirendra Chouhan
Try this Code --
trigger cascadeJunctionDelete on Opportunity_Equipment__c (after update) {

    List<Opportunity> oppList = new List<Opportunity>();
    List<Equipment__c> eqList = new List<Equipment__c>();
    List<Equipment__c> Fordel = new list<Equipment__c>();    
	Set<id> OppId = new set<Id>(); 
	Set<id> EquId = new set<Id>(); 

    for(Opportunity_Equipment__c ed : Trigger.old) {
		OppId.add(ed.opportunity__c);
		EquId.add(ed.Equipment__c);
        }
  oppList = [select StageName From Opportunity where Id IN : OppId];
  eqList = [select Status__c From Equipment__c where Id IN : EquId];
 
	if((OppList[0].StageName == 'Closed Lost' ||  OppList[0].StageName == 'Closed-Abandoned') && ( eqList[0].Status__c == 'Pending Sales') ) {
        
		//oppList.add(new Opportunity(id=ed.Opportunity__c));
	        //eqList.add(new Equipment__c(id=ed.Equipment__c)); 
		ForDel.add(new Equipment__c(id=ed.Equipment__c)); 
  	}  

    if(ForDel.size() > 0) {
        delete eqList;
    }
}

Note : This code is not Tested
WikWik
this throws an error and does not let me save:
Error: Compile Error: Variable does not exist: ed.Equipment__c at line 20 column 40
SarfarajSarfaraj
Hi

Use this instead,
 
trigger cascadeJunctionDelete on Opportunity_Equipment__c (after update) 
{
	List<Id> idsOpp = new List<Id>();
	List<Id> idsEq = new List<Id>();
    for(Opportunity_Equipment__c e: trigger.new)
	{
        idsOpp.add(e.Opportunity__c);
		idsEq.add(e.Equipment__c);
	}
    Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>([Select Id, StageName From Opportunity Where Id in :idsOpp]);
	Map<Id, Equipment__c> eqMap = new Map<Id, Equipment__c>([Select Id, Status__c From Equipment__c Where Id in :idsEq]);
    List<Opportunity> oppList = new List<Opportunity>();
    List<Equipment__c> eqList = new List<Equipment__c>();
    for(Opportunity_Equipment__c ed : Trigger.new) 
	{
		Opportunity opp = oppMap.get(ed.Opportunity__c);
		Equipment__c eq = eqMap.get(ed.Equipment__c);
		if((opp.StageName == 'Closed Lost' ||  opp.StageName == 'Closed-Abandoned') && ( eq.Status__c == 'Pending Sales')) 
		{
			oppList.add(opp);
			eqList.add(eq); 
		}  
	}
	if(eqList.size() > 0)
		delete eqList;
}

 
WikWik
Hey akram ,

tried your code, it saved , but the deletion did not happen
Virendra ChouhanVirendra Chouhan
Hy Wik
sorry that's my mistak
replace line 20 with
ForDel.add(new Equipment__c(id=EquId[0]));


 
SarfarajSarfaraj
Hi

Is this condition returns true for your data set?


((opp.StageName == 'Closed Lost' || opp.StageName == 'Closed-Abandoned') && ( eq.Status__c == 'Pending Sales'))

You may use debug logs to test it.

 
WikWik

i checked the debug log, this trigger is not in business.

i could not find this in the log itself. is this not running when i update an opportunity ?

SarfarajSarfaraj
Also, this code will work only on after update event. To make it work event with insert change first line to this,
trigger cascadeJunctionDelete on Opportunity_Equipment__c (after update, after insert) 
SarfarajSarfaraj
This triger will run only on update of Opportunity Equipment. to make it work with opportunity update, (which seems to me is more meaningful) use the following code (will post shortly).
WikWik

no success, the equipment is not getting deleted

and i dont find this trigger running in the Debug Log.

Any suggestion?

SarfarajSarfaraj
Hi 

I think you need a trigger on Opportunity instead of Opportunity Equipment.

Use this,
 
trigger cascadeJunctionDeleteOpp on Opportunity (after update) 
{
	List<Id> idsOpp = new List<Id>();
	for(Opportunity opp: trigger.new)
		if(opp.StageName == 'Closed Lost' ||  opp.StageName == 'Closed-Abandoned')
			idsOpp.add(opp.Id);
	List<Id> idsEq = new List<Id>();
	List<Equipment__c> eqList = new List<Equipment__c>();
	for(Opportunity_Equipment__c oppEq : [Select Id, Equipment__r.Id From Opportunity_Equipment__c Where Opportunity__c in :idsOpp And Equipment__r.Status__c = 'Pending Sales'])
		eqList.add(oppEq.Equipment__r);
	if(eqList.size() > 0)
		delete eqList;
}

Deactivate the old trigger.
This was selected as the best answer
WikWik
thanx . that worked.