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
SFDC New learnerSFDC New learner 

Need help to fix the code

Hi Everyone,

I am getting the following error when trying to callout an external service using trigger and future method and update the object.
trigger TriggerAfterInsertUpdate on CustomObject __c(after insert, after update ) {
if(Trigger.isAfter){
         if(updation.isfutureupdate!=true)
            {
               id lst;
                for(CustomObject e :Trigger.new)
                {
                   // lst=e.Id;
                    system.debug('$$$$$$$$$$$'+e.Id);
                    handler.dofuture(e.Id);
                }
                
                
            }
public with sharing class handler {
@future(callout=true)
    public static void dofuture(Id recordId){
        CustomObject __C  cust = [SELECT Id, Name, , IsChecked,
            Day_H__c, Month_H__c, Year_H__c,  Day_G__c, Month_G__c, Year_G__c FROM CustomObject__c
            WHERE Id = :recordId];
        List<String> hDate = verifyfields(cust ,cust .Day_G__c,cust .Month_G__c,cust .Year_G__c,cust .IsChecked);
                     cust .Year_H__c = hDate[0];
                    cust .Month_H__c = hDate[1];
                    cust .Day_H__c = hDate[2];
        List<CustomObject__c> dec1 = new List<CustomObject__c>();
        dec1.add(cust );
        update dec1;
        updation.isfutureupdate = true;
    }
    public static List<string> verifyfields(SObject sobj, String dayG, String monthG, String yearG, Boolean IsChecked){
        String urlPrefix = 'urlpath'
        String retUrl = urlPrefix;
        retUrl += '&gy=' + yearGregorian;
        retUrl += '&gm=' + monthGregorian;
        retUrl += '&gd=' + dayGregorian;
        retUrl += '&gs=' + (afterSunset ? '1' : '0');
        Http h = new Http();
            
            // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint 
            HttpRequest req = new HttpRequest();
            req.setEndpoint(retUrl);
            req.setMethod('GET');
            
            // Send the request, and return a response 
            HttpResponse res = h.send(req);
                                              
                                          
            if(res.getStatusCode() != 200) {
        System.debug('The status code returned was not expected: ' +
            res.getStatusCode() + ' ' + res.getStatus());
     }else {
        System.debug('********response********'+res.getBody());
     } 
       List<String> ret = new String[3];
        JSONParser parser = System.Json.createParser(res.getBody());
                                                          
        while (parser.nextToken() != null) {
            system.debug('inside while'+JSONToken.FIELD_NAME);
            if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                if (parser.getText() == 'hy') {
                    // Get the value. 
                    parser.nextToken();
                    ret[0] = parser.getText();
                    System.debug('Year='+ret[0]);
                }
                else if (parser.getText() == 'hm') {
                    // Get the value. 
                    parser.nextToken();
                    ret[1] = parser.getText();
                    if (ret[1].equals('xxx') || ret[1].equals('yyy'))
                        ret[1] = 'yyy';
                    else if (ret[1].equals('zzz'))
                        ret[1] = 'zzz';

                    System.debug('Month='+ret[1]);
                }
                else if (parser.getText() == 'hd') {
                    // Get the value. 
                    parser.nextToken();
                    ret[2] = parser.getText();
                    System.debug('Day='+ret[2]);
                }
            }
        }
        return ret;        
     
                                          
                                      }
    
}
getting the following error:
EXCEPTION_THROWN [25]|System.DmlException: Update failed. First exception on row 0 with id a010U000006kjnSQAQ; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TriggerAfterInsertUpdate : execution of AfterUpdate

Please can anyone help me to fix this issue?
Thanks,
Sirisha
 
David Zhu 🔥David Zhu 🔥
Your code looks good.  I noticed there two comma signs in the soql.

 public static void dofuture(Id recordId){
        CustomObject __C  cust = [SELECT Id, Name,  IsChecked,
            Day_H__c, Month_H__c, Year_H__c,  Day_G__c, Month_G__c, Year_G__c FROM CustomObject__c
            WHERE Id = :recordId];
        List<String> hDate = verifyfields(cust ,cust .Day_G__c,cust .Month_G__c,cust .Year_G__c,cust .IsChecked);
                     cust .Year_H__c = hDate[0];
                    cust .Month_H__c = hDate[1];
                    cust .Day_H__c = hDate[2];
        update cust;
    }
Andrew GAndrew G
It would generally indicate that they don't have access to update that record.

to troubleshoot, work out the object and check the CRUD and sharing rules.  If unclear on the object, run in developer console:
Id sampleid = 'a010U000006kjnSQAQ';
 System.debug('object is '+ sampleid.getsobjecttype());
or just stick the object trigraph in a URL

regards
Andrew