0

The line "Integer days =" has an error, which I am trying to bind the Case record id to get "days" to be passed to the "createCase.Date_Due__c = date.today() + days; " Equipment__c is a lookup field on Case to Product2. What is wrong here? Thanks for your help!

public with sharing class MaintenanceRequestHelper {

public static void updateWorkOrders(List<Case> newCase, Map<Id,Case> oldMapCase) {
    List<Case> caseList = new List<Case>();
    for (Case thisCase: newCase)         
    {
     Integer days = [Select Maintenance_Cycle__c from Product2 where id =:thisCase.Equipment__c];

        if (thisCase.Status=='Closed' && oldMapCase.get(thisCase.id).Status != 'Closed'){
            Case createCase = new Case();
            createCase.RecordTypeId = '0124W00000160QvQAI';
            createCase.subject = 'New Case Created';
            createCase.Equipment__c = thisCase.Equipment__c;
            createCase.Date_Due__c = date.today() + days; 
            caseList.add(createCase);
        }
    }
    insert caseList;
}

}

2
  • A SOQL query returns a List<SObject> in most cases, and that can't be converted to an Integer without a bit more work. What is the type of your Maintenance_Cycle__c field?
    – Derek F
    Commented Oct 18, 2020 at 21:09
  • It is decimal and I've used valueOf() to convert it.
    – Pluto
    Commented Oct 20, 2020 at 2:20

1 Answer 1

0

If maintenance cycle is an integer field then you can simply use it as.

Integer days = [Select Maintenance_Cycle__c from Product2 where id =:thisCase.Equipment__c].Maintenance_Cycle__c;

You may need to cast it to an integer to take the integer value if it is a decimal using Integer valueOf().

The code assumes that query will always return something, based on your data model, additional checks can be added if the query fails.

3
  • 1
    The new safe navigation operator makes this easier: Integer days = [Select Maintenance_Cycle__c from Product2 where id =:thisCase.Equipment__c]?.Maintenance_Cycle__c?.intValue();
    – sfdcfox
    Commented Oct 18, 2020 at 22:13
  • @manjit thanks for your comment. I got the code to work. Just a bit more for my understanding - The SOQL returns the value of Maintenance_Cycle__c and then the dot notation follows it makes it as Maintenance_Cycle__c.Maintenance_Cycle__c? What kind of traverse would this be? I understand a similar use of it i.e. account.industry can you elaborate?
    – Pluto
    Commented Oct 19, 2020 at 3:37
  • The SOQL will return the complete record of Product 2 and from that we are taking out the value of maintenance cycle. In Salesforce a SOQL query either return a record, a list of records or an integer (if you run a count) Please review the SOQL reference documentation
    – manjit5190
    Commented Oct 19, 2020 at 4:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .