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
LoïcLoïc 

Get number of solutions attached to a case - Need your help

Hello,

 

I'm trying to get the number of solutions attached to case (the goal is to be able to use a email template displaying the last added solution only with something like :

<apex:repeat first="{!relatedTo.nb_solutions__c-1}" value="{!relatedTo.CaseSolutions}" var="line">
<apex:outputText value="{!line.Solution.SolutionNote}" escape="false" />
</apex:repeat>).

 

Here is my code:

 

trigger CountSolution on Case (after update) {
		case.nb_solutions__c = [select casesolution.SolutionId from casesolution where caseID = :case.Id];
		update case.nb_solutions__c;
}

 

But I'm getting this error:

Save error: Invalid bind expression type of Schema.SObjectField for column of type Id CountSolution.trigger /Test Trigger/src/triggers line 2 Force.com save problem

 

I'm a "newbie", so any help or direction is appreciated. :smileyembarrassed:

Best Answer chosen by Admin (Salesforce Developers) 
LoïcLoïc

Here is the final code and tricks :

 

Trigger

 

trigger CountSolution on case(before update) {
    if(Trigger.isUpdate)
    {
        List<Case> objCase= new List<Case>();
        for(Case c:Trigger.New)
        {
           Integer intNum = 0;
           intNum =[select Count() from casesolution where caseID =:c.id];
            c.nb_solutions__c =intNum;
           objCase.add(c);
        }
    }
}

 

Test Class

 

@isTest
public class TestCountSolutions {

    static testMethod void myUnitTest() {
        Case cse=new Case();
        insert cse;
        Solution sol=new Solution();
        sol.SolutionName='SolutionTest';
        insert sol;
 
        CaseSolution attach=new CaseSolution();     
        attach.caseId=cse.id;
        attach.solutionId=sol.id;
        insert attach;
    Update cse;
        
    }
}

 Button "Quick Answer" (Update Case Status to "Closed" and update "nb_solutions__c"

 

{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")}
var caseObj = new sforce.SObject("Case");
caseObj.Id = '{!Case.Id}';
caseObj.Status = 'Closed';
var result = sforce.connection.update([caseObj]);
if (result[0].success=='false') {
alert(result[0].errors.message);
} else {
location.reload(true);
}
location.replace('/email/author/emailauthor.jsp?retURL=/{!Case.Id}&isdtp=mn&p3_lkid={!Case.Id}&rtype=003&p2_lkid={!Case.ContactId}&template_id=00XA0000001ZgLy&p26=support@praxedo.com&p5=');

 

All Answers

AmitSahuAmitSahu

Try this : 

 

trigger CountSolution on Case (after update) {
Case objCase;
String strNum = [select casesolution.SolutionId from casesolution where caseID = :case.Id];
	objCase.nb_solutions__c = strNum ;

		update objCase;

 Hope this helps..

Regards,

 

J

LoïcLoïc

Hello J20,

 

Many thanks for your reply.

 

I tought it would work... but I got the exact same error testing your code:

Save error: Invalid bind expression type of Schema.SObjectField for column of type Id CountSolution.trigger /Test Trigger/src/triggers line 3 Force.com save problem

 

trigger CountSolution on Case (after update) {
Case objCase;
String strNum = [select casesolution.SolutionId from casesolution where caseID = :case.Id];
objCase.nb_solutions__c = strNum;
update objCase;
}

 

Other suggesstions are welcome.

 

Thanks again for your help!

AmitSahuAmitSahu
Sorry I forgot to change the query. Change it from case.id to trigger.new.id
LoïcLoïc

Thank you.

 

Now, I'm getting a new error:

Description Resource Path Location Type
Save error: Initial term of field expression must be a concrete SObject: LIST<Case> CountSolution.trigger /Test Trigger/src/triggers line 3 Force.com save problem

 

trigger CountSolution on Case (after update) {
Case objCase;
String strNum = [select casesolution.SolutionId from casesolution where caseID = :trigger.new.id];
objCase.nb_solutions__c = strNum;
update objCase;

 

I'm a newbie. :smileyembarrassed:

 

Thanks for you help!!

 

 

AmitSahuAmitSahu

Where exactly you are getting the error ? on VF page or on Trigger ?

 

Try this  but this will work on a single record and bulkify this code if you want to handle more than 1 record at a time :

 

trigger CountSolution on Case (after update) {
Case objCase;
String strNum = [select casesolution.SolutionId from casesolution where caseID = :trigger.new[0].id];
objCase.nb_solutions__c = strNum;
update objCase;
LoïcLoïc

Hello j020,


I'm using Eclipse to test the trigger and I'm having a new error  :

Save error: Illegal assignment from LIST<CaseSolution> to String


trigger CountSolution on Case (after update) {
Case objCase;
String strNum = [select casesolution.SolutionId from casesolution where caseID = :trigger.new[0].id];
objCase.nb_solutions__c = strNum;
update objCase;

(Also I tried to save it manually into my SFDC account but I got the same message)

 

Any idea why I'm getting this error?

 

 

AmitSahuAmitSahu

About the query...

 

select casesolution.SolutionId from casesolution where caseID =:trigger.new.id

 

If I am not wrong you need a number to assign it to the case, right ?

 

But according to the query you are querying for solutionID ....

 

Can you modify it to select Count(ID) from casesolution where caseID =:trigger.new.id

LoïcLoïc

 

Error in the first case :

Description Resource Path Location Type
Save error: Initial term of field expression must be a concrete SObject: LIST<Case> CountSolution.trigger /Test Trigger/src/triggers line 3 Force.com save problem

 

trigger CountSolution on Case (after update) {
Case objCase;
String strNum = [select casesolution.SolutionId from casesolution where caseID =:trigger.new.id];
objCase.nb_solutions__c = strNum;
update objCase;
}

 And I got the same error with :

 

trigger CountSolution on Case (after update) {
Case objCase;
String strNum = [select Count(ID) from casesolution where caseID =:trigger.new.id];
objCase.nb_solutions__c = strNum;
update objCase;
}

 My goal is to count the solutions attached to a case and add this value to a custom field in the case.

 

The code above might not be the best way to do these. Do you have a suggestion or another correction to add to the code?

 

Thanks you for your help!

 

AmitSahuAmitSahu

Hope this works..

 

trigger CountSolution on Case (after update) {
Case objCase;
String strNum = String.ValueOf([select Count(ID) from casesolution where caseID =:Trigger.New[0].id]);
objCase.nb_solutions__c = strNum;
update objCase;
}

 

Sorry wasreplying earlier without accessing machines .....

 

Regards,

 

J

LoïcLoïc

Hello,

 

I'm having a new error but we are close I'm sure:

Description Resource Path Location Type
Save error: Illegal assignment from String to Decimal CountSolution.trigger /Test Trigger/src/triggers line 4 Force.com save problem

 

 

trigger CountSolution on Case (after update) {
Case objCase;
String strNum = String.ValueOf([select Count(ID) from casesolution where caseID =:Trigger.New[0].id]);
objCase.nb_solutions__c = strNum;
update objCase;
}

 

Thank you again!!!

 

EDIT :

 

I found the solution and change the filed type of my custom field nb_solution__c from number to text.

It works!!!

 

Now, I'm trying to create the test class. I did one but that one convers only 56% of the trigger.

 

private class TestCountSolutions {

    static testMethod void myUnitTest() {
       	Case cse=new Case();
        insert cse;
        Solution sol=new Solution();
        sol.SolutionName='SolutionTest';
        insert sol;
 
        CaseSolution attach=new CaseSolution();   	
        attach.caseId=cse.id;
        attach.solutionId=sol.id;
        insert attach;
    	
    }
}

 

What could I add to this test class to reach at least 75%?

 

In addition, it seems the trigger is not totaly right, because when a email to case comes the case can't be created. May be because I forgot to add a condition in the trigger in case the "casesolution" doesn't exist (this will be the case for all new cases...). What do you think about that?

 

Here is the error that I received by email after I succed in saving the trigger :

 

The following errors were encountered while processing an incoming email:

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY : CountSolution: execution of AfterUpdate

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.CountSolution: line 4, column 1

 

or if I try to modifie a case and to save it :

 

Apex trigger CountSolution caused an unexpected exception, contact your administrator: CountSolution: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.CountSolution: line 4, column 1

 

That will be great if you could help me again!

LoïcLoïc

Here is the final code and tricks :

 

Trigger

 

trigger CountSolution on case(before update) {
    if(Trigger.isUpdate)
    {
        List<Case> objCase= new List<Case>();
        for(Case c:Trigger.New)
        {
           Integer intNum = 0;
           intNum =[select Count() from casesolution where caseID =:c.id];
            c.nb_solutions__c =intNum;
           objCase.add(c);
        }
    }
}

 

Test Class

 

@isTest
public class TestCountSolutions {

    static testMethod void myUnitTest() {
        Case cse=new Case();
        insert cse;
        Solution sol=new Solution();
        sol.SolutionName='SolutionTest';
        insert sol;
 
        CaseSolution attach=new CaseSolution();     
        attach.caseId=cse.id;
        attach.solutionId=sol.id;
        insert attach;
    Update cse;
        
    }
}

 Button "Quick Answer" (Update Case Status to "Closed" and update "nb_solutions__c"

 

{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")}
var caseObj = new sforce.SObject("Case");
caseObj.Id = '{!Case.Id}';
caseObj.Status = 'Closed';
var result = sforce.connection.update([caseObj]);
if (result[0].success=='false') {
alert(result[0].errors.message);
} else {
location.reload(true);
}
location.replace('/email/author/emailauthor.jsp?retURL=/{!Case.Id}&isdtp=mn&p3_lkid={!Case.Id}&rtype=003&p2_lkid={!Case.ContactId}&template_id=00XA0000001ZgLy&p26=support@praxedo.com&p5=');

 

This was selected as the best answer
Ricky MartinRicky Martin

Hi All,

When attache the solution form the case page, after update trigger is not triggered so how can we calculate the count. I want to achieve when attach the soution, send the email notification. how to achieve this please let me know.