0

Error in saving unable to apply filter on opportunity

global class batchClassOppUpdate implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC){
       String query='Select id   from OpportunityContactRole Where IsPrimary=true AND OpportunityId IN (SELECT Id from Opportunity where recordtype.name = 'XYZ Solutions')';
         
          return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<OpportunityContactRole> Scope){
        for(OpportunityContactRole a:scope){
            a.isPrimary= true;
            
            
        }
        update scope;
    }
    global void finish(Database.BatchableContext BC){
        
    }
}
0
       String query='Select id   from OpportunityContactRole Where IsPrimary=true AND OpportunityId IN (SELECT Id from Opportunity where recordtype.name = 'XYZ Solutions')';
    

You have to escape nested single quotes in Apex.

But here, you don't need Dynamic SOQL at all. Just use a static query:

return Database.getQueryLocator([
    SELECT Id 
    FROM OpportunityContactRole 
    WHERE IsPrimary = true AND OpportunityId IN 
          (SELECT Id FROM Opportunity WHERE RecordType.Name = 'XYZ Solutions')
]);

You also don't need the semi-join. You can traverse the relationship to Opportunity directly in your query:

return Database.getQueryLocator([
    SELECT Id 
    FROM OpportunityContactRole 
    WHERE IsPrimary = true AND Opportunity.RecordType.Name = 'XYZ Solutions'
]);
0

You would need to escape the quote characters (e.g. \'XYZ Solutions\'). However, you should use an inline query instead:

  return Database.getQueryLocator([
      Select id   from OpportunityContactRole Where IsPrimary=true AND OpportunityId IN (SELECT Id from Opportunity where recordtype.name = 'XYZ Solutions')
  ]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.