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
Mary Anu 8Mary Anu 8 

not able to update a field in custom object based on the response of Rest webservice when invoked from before insert,before update trigger of the same custom object

v varaprasadv varaprasad
Hi Mary,

Please provide us sample trigger and class. SO that it will help to investigate further.

Use Before Trigger:
In the case of validation check in the same object.
Insert or update the same object.

Triggers will execute synchronously and web services will execute asynchronously..


Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
Mary Anu 8Mary Anu 8
this is the trigger from where webservice is called

trigger ArtifactRecordTrigger on FS_Artifact__c (before insert,before update) {
    
    Set<Id> recordIds = new Set<Id>();
    List<String> recordList=new List<String>();
    List<String> recordArtiList=new List<String>();
    
    Id recordTypeId = Schema.SObjectType.FS_Artifact__c.getRecordTypeInfosByName().get('Dry Run').getRecordTypeId();
    for(FS_Artifact__c arti:trigger.new)
    {
        
        if(arti.RecordTypeId==recordTypeId)
        {
            ArtifactWrapper wrap =new ArtifactWrapper(arti);
            system.debug('wrappes rec '+wrap);
            recordList.add(JSON.serialize(wrap));
            recordArtiList.add(JSON.serialize(arti));
            system.debug('serialised str '+JSON.serialize(wrap));
        }
        
    }
    
    system.debug('list of artifacts'+recordArtiList.size());
    system.debug('list of records '+recordList.size());
    
    if(!recordList.isEmpty() && recordList.size()>0 )
    {
        system.debug('calling web service');
        //ArtifactValidationWebService.fsArt = trigger.new;
       
        if(!System.isFuture())
        {
            ArtifactValidationWebService.getArtifactValidation(recordList,recordArtiList);  --- webservice invocation
        }
        
        
    }
    
    for(FS_Artifact__c artiRec:trigger.new)
        {
            system.debug('is valid value '+artiRec.IsValid__c);
            if(artiRec.IsValid__c=='Yes')
            {
                artiRec.adderror('success msg');
            }
            else if(artiRec.IsValid__c=='No')
            {
                artiRec.adderror('fail msg');
            }
        }
    
}


this is the webservice class

public class ArtifactValidationWebService {
    public static List<FS_Artifact__c> fsArt{set;get;}
    
    @future (callout=true)
    public static void getArtifactValidation(List<String> recordStrList,List<String> artifactStrList)
    {
        String responseMsg;
        ArtifactWrapper wrapper=null;
        FS_Artifact__c artifactRec=null;
        List<ArtifactWrapper> wrapperArtifactList=new List<ArtifactWrapper>();
       List<FS_Artifact__c> artifactRecList=new List<FS_Artifact__c>();
        List<FS_Artifact__c> updatedArtifactList=new List<FS_Artifact__c>();
        
         //to deserialise to List of ArtifactWrapper instance
        for(String str:recordStrList)
        {
            wrapper = (ArtifactWrapper) JSON.deserialize(str, ArtifactWrapper.class);
            system.debug('list of wrapper '+wrapper);
            wrapperArtifactList.add(wrapper);
        }
       
        //to deserialise to List of Sobject
        for(String str:artifactStrList)
        {
             artifactRec = (FS_Artifact__c) JSON.deserialize(str, FS_Artifact__c.class);
            artifactRecList.add(artifactRec);
        }
        system.debug('artifact record list'+artifactRecList.size());
        system.debug('artifact wrapper list'+wrapperArtifactList.size());
        
        if(!wrapperArtifactList.isEmpty() && wrapperArtifactList.size()>0)
        {
            RawContentWrapper raw=new RawContentWrapper(wrapperArtifactList);
            String input=JSON.serialize(raw);
            system.debug('input '+input);
            HttpRequest req = new HttpRequest();
            req.setHeader('Content-Type','application/json');
            req.setHeader('Authorization', 'Basic Z2F0ZVdheToyMDE3Q29jYSU=');
            req.setMethod('POST');
            req.setEndpoint(System.Label.FOT_ArtifactValidation_Url);
            req.setTimeout(120000);
            req.setBody(input);    
            
            Http http=new Http();
            try
            {
                HttpResponse response=http.send(req);
                if(response.getStatusCode()==200)
                {
                    system.debug('success '+response.getBody());
                    ResponseClass res=(ResponseClass)JSON.deserialize(response.getBody(), ResponseClass.class);
                    system.debug('response '+res);
                   // system.debug('trigger new rec'+trigger.new[0]);
                   if(!artifactRecList.isEmpty() && artifactRecList.size()>0)
                   {
                       for(FS_Artifact__c a:artifactRecList)
                       {
                           if(res.valid)
                           {
                              a.IsValid__c='Yes'; 
                           }
                           else
                           {
                               a.IsValid__c='No';
                           }
                           updatedArtifactList.add(a);
                       }
                       system.debug('list of updated artifact '+updatedArtifactList);
                  
                       upsert updatedArtifactList;
                       system.debug('after upsert');
                   }
                    
                }  
            }
            catch(Exception e)
            {
                system.debug('error msg '+e.getMessage()+' '+e.getLineNumber()+' '+e.getStackTraceString());
            }
            
        }
         
    }
    
    class ResponseClass
    {
        public boolean valid;
        public string hashCode;
        public string message;    
        public string textArea;
        public string noMsg;
        public string yesMsg;
    }
}