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
Shawn ReichnerShawn Reichner 

Apex Trigger Creation to Sum Case field from Work Order Formula Currency Field

Hello,

First time question here from a Apex Newbie.....

I have a standard object (Case) which will have a currency field, that I want to update from a rollup summary field on a custom object (Work Order) called Price__c.  The Price__c field on the Work Order Custom Object totals all work order work detail lines, and I want the Price__c field to update the currency field on the case record.  

So far I have the following code written, but I am getting an Invalid Ofreign Key error when tring to quick save.  Can anyone help me here......Thank you in advance

trigger DoRollup on SVMXC__Service_Order__c (after insert, after update, after delete, after undelete) {
    Set<Id> setCaseId=new Set<Id>();
    Map<string,decimal> CaseWithTotalWorkOrders=new Map<string,decimal>();
    for(SVMXC__Service_Order__c mst:Trigger.New){
    if(Case.Total_Billable_Work_Orders__c!=null){
     setCaseId.add(mst.SVMXC__Service_Order__c.Price__c);
    }
    }
   

}
Best Answer chosen by Shawn Reichner
Vinit_KumarVinit_Kumar
My Bad Shawn,

I forgot to add semicolon,try this one :-

trigger DoRollup on SVMXC__Service_Order__c (before insert, before update) {
    List<Id> CaseIds=new List<Id>();
	
	//Populate the CaseIds to the List
    for(SVMXC__Service_Order__c mst:Trigger.New){
    if(mst.SVMXC__Case__c!=null){
     CaseIds.add(mst.SVMXC__Case__c);
    }
    }
   
   // Query the related Case record
   Map<Id,Case> caseMap = new Map<Id,Case>([select id,Total_Billable_Work_Orders__c from Case where id in:CaseIds]);
   
   for(SVMXC__Service_Order__c mst:Trigger.New)
   {
		if(!caseMap.IsEmpty())
		{
			caseMap.get(mst.SVMXC__Case__c).Total_Billable_Work_Orders__c = mst.Price__c;
		}
   }
   // Update case record
   if(!caseMap.IsEmpty())
   {
		update caseMap.values();
   }
   

}

If this helps,please mark it as best answer to help others :)

All Answers

Anil SavaliyaAnil Savaliya
Hey Shawn,

Can you assign appropriate Recody type for 
Set<Id> setCaseId=new Set<Id>();
Id,You can use for record id only,But here You trying to add some Price__C ,It may number or decimal.
Shawn ReichnerShawn Reichner
Anil, thank you for your reply, again I am new to Apex and am eager to learn, so needless to say, I am not understanding your suggestion.  Could you please expand?  The Price__c field is the rollup summary field on my custom object that I want to continue to add or sum to the standard object (Case) field total_billable_work_Orders__c.    Does that make sence?   
Ramu_SFDCRamu_SFDC
The problem seems to be at this line setCaseId.add(mst.SVMXC__Service_Order__c.Price__c) . It should be something mst.SVMXC__Service_Order__r.Price__c . For more information review the below link
http://salesforce.stackexchange.com/questions/15244/access-field-on-lookup-relationship-in-trigger
Shawn ReichnerShawn Reichner
Thank you Ramu for your reply.  I did go to the link, but however that did look to help.  In your reply above you stated..... It should be something mst.SVMXC__Service_Order__r.Price__c .   Is that what I woudl replace my line with exactly?  What does something mean?


Thank you again,
Ramu_SFDCRamu_SFDC
If you ask me specifically I would say try the below two lines one at a time and let me know if that resolved your issue

mst.SVMXC__Service_Order__r.Price__c
mst.SVMXC__Service_Orders__r.Price__c
Shawn ReichnerShawn Reichner
Here is the new code.....No matter which line I use, I am getting the following error..

trigger DoRollup on SVMXC__Service_Order__c (after insert, after update, after delete, after undelete) {
    Set<Id> setCaseId=new Set<Id>();
    Map<string,decimal> CaseWithTotalWorkOrders=new Map<string,decimal>();
    for(SVMXC__Service_Order__c mst:Trigger.New){
    if(Case.Total_Billable_Work_Orders__c!=null){
     mst.SVMXC__Service_Order__r.Price__c;
    }
    }
   

}

The error I am getting is: Error: Compile Error: Invalid foreign key relationship: SVMXC__Service_Order__c.SVMXC__Service_Order__r at line 6 column 6
Vinit_KumarVinit_Kumar
Shawn,

How Case and your custom object SVMXC__Service_Order__c are related ,I mean based on which field.
Shawn ReichnerShawn Reichner
Service Order is related to the Case object by their IDs….meaning When I am in a case record, I have work orders(Service_Order__c) attached to that case in a related list at the bottom.
Vinit_KumarVinit_Kumar
That means Case is parent and Service_Order__c is child.

So,there must be a field on Service_Order__c which would be referencing to Case object,could you check which field is that ??
Shawn ReichnerShawn Reichner
The only Lookup field on the Service Order object that references Case is a field called SVMXC__Case__c
Vinit_KumarVinit_Kumar
Shawn,

try below code :-

trigger DoRollup on SVMXC__Service_Order__c (before insert, before update) {
    List<Id> CaseIds=new List<Id>();
	
	//Populate the CaseIds to the List
    for(SVMXC__Service_Order__c mst:Trigger.New){
    if(mst.SVMXC__Case__c!=null){
     CaseIds.add(mst.SVMXC__Case__c)
    }
    }
   
   // Query the related Case record
   Map<Id,Case> caseMap = new Map<Id,Case>([select id,Total_Billable_Work_Orders__c from Case where id in:CaseIds]);
   
   for(SVMXC__Service_Order__c mst:Trigger.New)
   {
		if(!caseMap.IsEmpty())
		{
			caseMap.get(mst.SVMXC__Case__c).Total_Billable_Work_Orders__c = mst.Price__c;
		}
   }
   // Update case record
   if(!caseMap.IsEmpty())
   {
		update caseMap.values();
   }
   

}

If this helps,please mark it as best answer to help others :)
Shawn ReichnerShawn Reichner
I tried the above code and I am getting Error: Compile Error: expecting a semi-colon, found '}' at line 8 column 4
Vinit_KumarVinit_Kumar
My Bad Shawn,

I forgot to add semicolon,try this one :-

trigger DoRollup on SVMXC__Service_Order__c (before insert, before update) {
    List<Id> CaseIds=new List<Id>();
	
	//Populate the CaseIds to the List
    for(SVMXC__Service_Order__c mst:Trigger.New){
    if(mst.SVMXC__Case__c!=null){
     CaseIds.add(mst.SVMXC__Case__c);
    }
    }
   
   // Query the related Case record
   Map<Id,Case> caseMap = new Map<Id,Case>([select id,Total_Billable_Work_Orders__c from Case where id in:CaseIds]);
   
   for(SVMXC__Service_Order__c mst:Trigger.New)
   {
		if(!caseMap.IsEmpty())
		{
			caseMap.get(mst.SVMXC__Case__c).Total_Billable_Work_Orders__c = mst.Price__c;
		}
   }
   // Update case record
   if(!caseMap.IsEmpty())
   {
		update caseMap.values();
   }
   

}

If this helps,please mark it as best answer to help others :)
This was selected as the best answer
Shawn ReichnerShawn Reichner
I believe we are hopefully getting closer.....I am now getting the error Error: Compile Error: Illegal assignment from Decimal to Id at line 18 column 45
Vinit_KumarVinit_Kumar
Shawn,

it seems your field datatypes  are not correct,will have to see then I can modify it accordingly.But,I gave you the approach as how you can do it.
Shawn ReichnerShawn Reichner
Vinit,

Thank you very much for your work and time.  I will play with the field types and learn a little more about which field types are not allowed in code as I never ran into this issue before.  Thanks again.  
Vinit_KumarVinit_Kumar
Happy to help Shawn :)