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
tiatia 

there are four objects accounts , policy(custom object) , opportunity , application(custom object). account and policy have masterdetail relationship , account and opportunity also have MD relation. and opportunity have lookup relation with application .

 there are four objects accounts , policy(custom object) , opportunity , application(custom object). account and policy have masterdetail relationship , account and opportunity also have MD relation. and opportunity have lookup relation with application . every application must have one opportunity assigned.
there is one  field on application called 'status'. 
requiremet :when application status is 'approved by CMS' .  check if related account with opportunity have assigned active  policy?if yes , update application status to "Enrolled" .


code i have written
trigger UpdateApplicationStatusToEnrolled on vlocity_ins__Application__c (before update) {
Map <string,List<vlocity_ins__Application__c>> strMap = new Map <string,List<vlocity_ins__Application__c>>();
set<id> oppid = new set<id>();

for(vlocity_ins__Application__c app :trigger.new)
{
if(app.vlocity_ins__Status__c=='Approved by CMS')
{
oppid.add(app.id);}}




List<opportunity> oppDetails=[SELECT id, AccountId, Account.name ,Account.vlocity_ins__ActivePolicyNumber__c  
 FROM Opportunity where Account.vlocity_ins__ActivePolicyNumber__c >=1 and id IN: oppid];
 
 for(vlocity_ins__Application__c app : Trigger.new)

{
 List<vlocity_ins__Application__c> appList = strMap.containsKey(app.opportunityid) ? strMap.get(app.opportunityId) : new List<vlocity_ins__Application__c>();

                        appList.add(app);

                        strMap.put(app.opportunityId, appList);




 
}

getting error , please help 
Anant KamatAnant Kamat
Can you specify the exact error you are getting. Is it runtime error or compile time error.
tiatia
its a compile error Error
Error: Compile Error: Variable does not exist: oppDetails at line 20 column 69 . and please let me know if i am doing it correctly or not . its urgent please help.
Thanks
Tad Aalgaard 3Tad Aalgaard 3
As requested by Anant, please include the compile error.  Otherwise it's much harder to figure out your issue.

The only thing that I might guess is the issue is you could change the key of the map from string to Id as that is what you are really dealing with.    
Map <string,List<vlocity_ins__Application__c> > strMap = new Map <string,List<vlocity_ins__Application__c> >();
to
Map <Id,List<vlocity_ins__Application__c> > strMap = new Map <Id,List<vlocity_ins__Application__c> >();

 
Anant KamatAnant Kamat
From your code, I can see that you are just querying the List<Opportunity> but not using it anywhere else. Do you really need that list.
tiatia
yes i used this list to  check if related account with opportunity have assigned active  policy.  i am now stuck with this code. can you please suggest any other better way to write trigger for this funcationility?
tiatia
can anyone help me in this?
Anant KamatAnant Kamat
You can use Roll-Up Summary fields to get a count of Active Opportunities in Account. Then if the count > 0 and Status = 'Approved by CMS', you can update the Status = 'Enrolled' using Workflows.
tiatia
my lead want me to write only trigger for this. he does not want to use workflow or process builder.
Tad Aalgaard 3Tad Aalgaard 3
The compile time error you provided does not match the line numbers of the code.  Please repost your code along with the latest compilation error.
tiatia
trigger UpdateApplicationStatusToEnrolled on vlocity_ins__Application__c (before update) {
Map <string,List<vlocity_ins__Application__c>> strMap = new Map <string,List<vlocity_ins__Application__c>>();
set<id> oppid = new set<id>();

for(vlocity_ins__Application__c app :trigger.new)
{
if(app.vlocity_ins__Status__c=='Approved by CMS')
{
oppid.add(app.id);}}




List<opportunity> oppDetails=[SELECT id, AccountId, Account.name ,Account.vlocity_ins__ActivePolicyNumber__c  
 FROM Opportunity where Account.vlocity_ins__ActivePolicyNumber__c >=1 and id IN: oppid];
 
 for(vlocity_ins__Application__c appl : Trigger.new)

{
 List<vlocity_ins__Application__c> appList = strMap.containKey(appl.oppDetails.id) ? strMap.get(appl.oppDetails.id) : new List<vlocity_ins__Application__c>();

                        appList.add(appl);

                        strMap.put(appl.oppDetails.id, appList);



}

Error: Error Error: Compile Error: Variable does not exist: oppDetails at line 20 column 69 
Tad Aalgaard 3Tad Aalgaard 3
I don't know where you are going with the following, but that isn't going to work.
appl.oppDetails.id
I'm pretty sure your custom vlocity_ins__Application__c object doesn't have an oppDetails field.  I think you meant to write oppDetails__c field with the __c.  All custom fields and objects have __c on the end of their API name.
 
tiatia
i was using list (oppDetails). i think my syntex is wrong.  opportunity and application have lookup relationship and filed name is opportunity__c. and when i tried to replace oppdetails with Opportunity__c , i am getting new error as below,

Error: Compile Error: A non foreign key field cannot be referenced in a path expression: Opportunity__c at line 20 column 69
Anant KamatAnant Kamat
Basically you need to iterate through the oppDetails as well since its a list and can contain more than 1 record matching your criteria. Incase you are limiting the results to 1, then in that case you can use oppDetails[0] instead of oppDetails[i] below.

List<vlocity_ins__Application__c> appList = strMap.containKey(appl.oppDetails[i].id) ? strMap.get(appl.oppDetails[i].id) : new List<vlocity_ins__Application__c>();

i will be the integer for iterating based on the size of the oppDetails.
tiatia
yes i am doing same but its throwing compile error says oppdetails does not esist.
Error: Compile Error: Variable does not exist: oppDetails at line 20 column 68
Anant KamatAnant Kamat
From the code that you have shared you are not iterating through the list. By Iteration I mean
for(Opportunity opp : oppDetails)
                     or
for(Interger i ; i <= oppDetails.size(); i++)

I do not see you doing that at all. Unless that is done you may not find the desired result you are expecting.
tiatia
Hi Anant thanks for the help but still i am getting error while saving trigger. see my lattest code 

trigger UpdateApplicationStatusToEnrolled on vlocity_ins__Application__c (before update) {
Map <string,List<vlocity_ins__Application__c>> strMap = new Map <string,List<vlocity_ins__Application__c>>();
set<id> oppid = new set<id>();

for(vlocity_ins__Application__c app :trigger.new)
{
if(app.vlocity_ins__Status__c=='Approved by CMS')
{
oppid.add(app.id);}}




List<opportunity> oppDetails=[SELECT id, AccountId, Account.name ,Account.vlocity_ins__ActivePolicyNumber__c  
 FROM Opportunity where Account.vlocity_ins__ActivePolicyNumber__c >=1 and id IN: oppid];
 
 for(Opportunity opp : oppDetails)

{
List<vlocity_ins__Application__c> appList = strMap.containKey(opp.oppDetails[i].id) ? strMap.get(opp.oppDetails[i].id) : new List<vlocity_ins__Application__c>();
                        appList.add(opp);

                        strMap.put(opp.OppDetails[i].id, appList);



}

getting below error: Error Error: Compile Error: Variable does not exist: oppDetails at line 20 column 67 
Anant KamatAnant Kamat
Based on your updated code replace with below line

List<vlocity_ins__Application__c> appList = strMap.containKey(opp.id) ? strMap.get(opp.id) : new List<vlocity_ins__Application__c>();

It should work as expected since you are iterating through Opportunity.