• Vitalii Sysak
  • NEWBIE
  • 0 Points
  • Member since 2023

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies

Hi everyone!

Need help.

 

I'm need create trigger who will work at @future and callout API from external services.

 

Here example what I'm write, have custom Object with field "Cover Letter' . Can I update this field in @future after get request from external service:


here is callout with example URL 

public class JobApplicationHttpCalloutForFuture {

    public static HttpResponse calloutCoverLetter(){
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        String apiUrl = 'https://th-apex-http-callout.herokuapp.com/animals';

        request.setEndpoint(apiUrl);
        request.setMethod('GET');

        HttpResponse response = http.send(request);

        return response;
    }

}

Here is trigger helpers methods

 public static void updateCoverLetter(List<Job_Application__c> listJobApplications) {
        Set<Id> jobApplicationsIds = new Set<Id>();

        for (Job_Application__c jobApplication : listJobApplications) {
            if (jobApplication.Cover_Letter__c == null) {
                jobApplicationsIds.add(jobApplication.Id);
            }
        }

        assyncUpdateCoverLetter(new List<Id>(jobApplicationsIds));
    }

    @Future(Callout=true)
    public static void assyncUpdateCoverLetter(List<Id> jobApplicationIds) {
        List<Job_Application__c> jobApplications = [SELECT Id, Cover_Letter__c FROM Job_Application__c WHERE Id IN :jobApplicationIds];
        HttpResponse response = JobApplicationHttpCalloutForFuture.calloutCoverLetter();

        if (response.getStatusCode() == 200) {
            String responseBody = response.getBody();
            List<Job_Application__c> jobApplicationsToUpdate = new List<Job_Application__c>();

            for (Job_Application__c jobApplication : jobApplications) {
                jobApplication.Cover_Letter__c = responseBody;
                jobApplicationsToUpdate.add(jobApplication);
            }

            if (!jobApplicationsToUpdate.isEmpty()) {
                update jobApplicationsToUpdate;
            }
        } else {
            System.debug('Error getting response code ' + response.getStatusCode());
        }
    }

Can Im update field after update ?

Have a task with triggers.

I'm have a custom object with fields, one of trigger method change fields value on values at child lookup object, sum some score.

I need to close edit for this fields, but with profiles i cant do this. When I write in trigger i can close this fields, but trigger also can't edit them. 

 

How I can write trigger or can i try another option for close edit this fields?

 

Here part of trigger and object with fields

private static final String STATUS_FOR_CHECK = 'Closed - Hired'; public static void getJobApplicationsForChangeStatusAfterUpdate(List<Job_Application__c> newJobApplications, Map<Id, Job_Application__c> oldJobApplications) { Set<Id> positionsToClose = new Set<Id>(); for (Job_Application__c newJobApp : newJobApplications) { Job_Application__c oldJobApp = oldJobApplications.get(newJobApp.Id); if (newJobApp.Overall_Average_Reviwes_Score__c != oldJobApp.Overall_Average_Reviwes_Score__c || newJobApp.Overal_Reviews__c != oldJobApp.Overal_Reviews__c) { newJobApp.addError('Fields with Reviews scores can not be changed'); } else { if (newJobApp.Status__c == STATUS_FOR_CHECK && oldJobApp != null && oldJobApp.Status__c != STATUS_FOR_CHECK) { positionsToClose.add(newJobApp.Position__c); } } } List<Position__c> positionsToUpdate = [SELECT Id, Status__c FROM Position__c WHERE Id IN :positionsToClose]; for (Position__c pos : positionsToUpdate) { pos.Status__c = STATUS_FOR_CHECK; } update positionsToUpdate; }

 

 

in this part I can check this fields

if (newJobApp.Overall_Average_Reviwes_Score__c != oldJobApp.Overall_Average_Reviwes_Score__c || newJobApp.Overal_Reviews__c != oldJobApp.Overal_Reviews__c) { newJobApp.addError('Fields with Reviews scores can not be changed'); }

Hi everyone!

Need help.

 

I'm need create trigger who will work at @future and callout API from external services.

 

Here example what I'm write, have custom Object with field "Cover Letter' . Can I update this field in @future after get request from external service:


here is callout with example URL 

public class JobApplicationHttpCalloutForFuture {

    public static HttpResponse calloutCoverLetter(){
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        String apiUrl = 'https://th-apex-http-callout.herokuapp.com/animals';

        request.setEndpoint(apiUrl);
        request.setMethod('GET');

        HttpResponse response = http.send(request);

        return response;
    }

}

Here is trigger helpers methods

 public static void updateCoverLetter(List<Job_Application__c> listJobApplications) {
        Set<Id> jobApplicationsIds = new Set<Id>();

        for (Job_Application__c jobApplication : listJobApplications) {
            if (jobApplication.Cover_Letter__c == null) {
                jobApplicationsIds.add(jobApplication.Id);
            }
        }

        assyncUpdateCoverLetter(new List<Id>(jobApplicationsIds));
    }

    @Future(Callout=true)
    public static void assyncUpdateCoverLetter(List<Id> jobApplicationIds) {
        List<Job_Application__c> jobApplications = [SELECT Id, Cover_Letter__c FROM Job_Application__c WHERE Id IN :jobApplicationIds];
        HttpResponse response = JobApplicationHttpCalloutForFuture.calloutCoverLetter();

        if (response.getStatusCode() == 200) {
            String responseBody = response.getBody();
            List<Job_Application__c> jobApplicationsToUpdate = new List<Job_Application__c>();

            for (Job_Application__c jobApplication : jobApplications) {
                jobApplication.Cover_Letter__c = responseBody;
                jobApplicationsToUpdate.add(jobApplication);
            }

            if (!jobApplicationsToUpdate.isEmpty()) {
                update jobApplicationsToUpdate;
            }
        } else {
            System.debug('Error getting response code ' + response.getStatusCode());
        }
    }

Can Im update field after update ?

Have a task with triggers.

I'm have a custom object with fields, one of trigger method change fields value on values at child lookup object, sum some score.

I need to close edit for this fields, but with profiles i cant do this. When I write in trigger i can close this fields, but trigger also can't edit them. 

 

How I can write trigger or can i try another option for close edit this fields?

 

Here part of trigger and object with fields

private static final String STATUS_FOR_CHECK = 'Closed - Hired'; public static void getJobApplicationsForChangeStatusAfterUpdate(List<Job_Application__c> newJobApplications, Map<Id, Job_Application__c> oldJobApplications) { Set<Id> positionsToClose = new Set<Id>(); for (Job_Application__c newJobApp : newJobApplications) { Job_Application__c oldJobApp = oldJobApplications.get(newJobApp.Id); if (newJobApp.Overall_Average_Reviwes_Score__c != oldJobApp.Overall_Average_Reviwes_Score__c || newJobApp.Overal_Reviews__c != oldJobApp.Overal_Reviews__c) { newJobApp.addError('Fields with Reviews scores can not be changed'); } else { if (newJobApp.Status__c == STATUS_FOR_CHECK && oldJobApp != null && oldJobApp.Status__c != STATUS_FOR_CHECK) { positionsToClose.add(newJobApp.Position__c); } } } List<Position__c> positionsToUpdate = [SELECT Id, Status__c FROM Position__c WHERE Id IN :positionsToClose]; for (Position__c pos : positionsToUpdate) { pos.Status__c = STATUS_FOR_CHECK; } update positionsToUpdate; }

 

 

in this part I can check this fields

if (newJobApp.Overall_Average_Reviwes_Score__c != oldJobApp.Overall_Average_Reviwes_Score__c || newJobApp.Overal_Reviews__c != oldJobApp.Overal_Reviews__c) { newJobApp.addError('Fields with Reviews scores can not be changed'); }