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
pdopdo 

Error On Trigger

Hi All, 

I am geeting the following error on my trigger and cannot figure out why I am getting it.  Can anyone help?

 

trigger wfcallupdate on Case (after insert) {
//Declare Variable
public set<id> caseid = new set<id>();
public set<id> oppid = new set<id>();
public list<opportunity> opptoupdate = [select id from opportunity where id in :oppid];

if(Trigger.isInsert){
    for(case c: Trigger.new){
    if(c.recordtype.name == 'Workflow')
    oppid.add(c.opportunity__r.id);
    caseid.add(c.id);
    }
}

//Update Opportunity
if(oppid.size()>0){
    for(opportunity o: [Select id, workflow_case__c from opportunity where id in :oppid])
    opptoupdate.add(new opportunity(id=o.id, workflow_case__c=caseid));
}
update opptoupdate;
}

 

 

Error: Compile Error: Invalid initial expression type for field Opportunity.Workflow_Case__c, expecting: Id at line 18 column 63

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Your logic is pretty far off-base; I'm actually having a hard time even understanding what your code is attempting to do.

 

Let me see if I have this right:

 

Opportunity: Workflow_Case__c (Lookup: Case)

Case: Opportunity__c (Lookup: Opportunity)

 

Each Opportunity would have one case known was the Workflow Case, which in turn would have its reference on the case, stored in Opportunity__c, making a strict 1:1 relationship.

 

Assuming this is what you are indeed after, you might need some validation logic to enforce the 1:1 relationship. Other than that, you'll need to use a Map, not a Set, to track these related records and perform the requisite updates.

 

The process would be as follows:

 

Map the Opportunity Id to the Case Id.

Query all opportunities, and update the Workflow_case__c field.

Save the changes.

 

trigger wfupdate on Case ( after insert ) {
  // Map opportunities to cases
  map<id,id> oppcasemap = new map<id,id>();
  // Use a loop here
  for(case c:trigger.new) {
    oppcasemap.put(c.opportunity__c,c.id);
  }
  // Query related opportunities
  opportunity[] opplist = [select id from opportunity where id in :oppcasemap.keyset()];
  // Populate the workflow_case__c field
  for(opportunity o:opplist) {
    opplist.workflow_case__c = oppcasemap.get(o.id);
  }
  // Update related opportunities
  update opplist;
}

I'm ignoring the situation of a 1:n relationship sneaking in and mucking up your data, but this is basically it. You don't need to check the record type unless Opportunity__c is used in more than one place; it would suffice to simply make a system validation rule that forces this field to be empty if the wrong record type is used.

 

 

All Answers

sfdcfoxsfdcfox

Your logic is pretty far off-base; I'm actually having a hard time even understanding what your code is attempting to do.

 

Let me see if I have this right:

 

Opportunity: Workflow_Case__c (Lookup: Case)

Case: Opportunity__c (Lookup: Opportunity)

 

Each Opportunity would have one case known was the Workflow Case, which in turn would have its reference on the case, stored in Opportunity__c, making a strict 1:1 relationship.

 

Assuming this is what you are indeed after, you might need some validation logic to enforce the 1:1 relationship. Other than that, you'll need to use a Map, not a Set, to track these related records and perform the requisite updates.

 

The process would be as follows:

 

Map the Opportunity Id to the Case Id.

Query all opportunities, and update the Workflow_case__c field.

Save the changes.

 

trigger wfupdate on Case ( after insert ) {
  // Map opportunities to cases
  map<id,id> oppcasemap = new map<id,id>();
  // Use a loop here
  for(case c:trigger.new) {
    oppcasemap.put(c.opportunity__c,c.id);
  }
  // Query related opportunities
  opportunity[] opplist = [select id from opportunity where id in :oppcasemap.keyset()];
  // Populate the workflow_case__c field
  for(opportunity o:opplist) {
    opplist.workflow_case__c = oppcasemap.get(o.id);
  }
  // Update related opportunities
  update opplist;
}

I'm ignoring the situation of a 1:n relationship sneaking in and mucking up your data, but this is basically it. You don't need to check the record type unless Opportunity__c is used in more than one place; it would suffice to simply make a system validation rule that forces this field to be empty if the wrong record type is used.

 

 

This was selected as the best answer
pdopdo

You're awesome!  I had to make one small modification so I wouldn't get the Compile Error: Initial term of field expression must be a concrete SObject: LIST<Opportunity>

 

trigger wfupdate on Case ( after insert ) {
  // Map opportunities to cases
  map<id,id> oppcasemap = new map<id,id>();
  // Use a loop here
  for(case c:trigger.new) {
    oppcasemap.put(c.opportunity__c,c.id);
  }
  // Query related opportunities
  opportunity[] opplist = [select id from opportunity where id in :oppcasemap.keyset()];
  // Populate the workflow_case__c field
  for(opportunity o:opplist) {
    opplist[0].workflow_case__c = oppcasemap.get(o.id);
  }
  // Update related opportunities
  update opplist;
}