Synchronize Salesforce opportunity data with Square Peg's PMS external system|| Challenge 4

Поделиться
HTML-код
  • Опубликовано: 12 дек 2024

Комментарии • 4

  • @Salesforcebuddy1711
    @Salesforcebuddy1711  Год назад +2

    ProjectCalloutService :-
    public class ProjectCalloutService {
    //method to be invoked by ProcessBuilder apex
    @InvocableMethod
    public static void postOpportunityToPMS(List oppoIds){
    Opportunity opp = [SELECT Id,Name,Account.Name,CloseDate,Amount FROM Opportunity WHERE Id = :oppoIds[0]];
    String serviceToken = ServiceTokens__c.getValues('ProjectServiceToken').Token__c;

    String jsonInput = '{
    ' +
    ' "opportunityId" : "'+opp.Id+'",
    '+
    ' "opportunityName" : "'+opp.Name+'",
    '+
    ' "accountName" : "'+opp.Account.Name+'",
    '+
    ' "closeDate" : "'+String.ValueOf(opp.CloseDate).mid(0,10)+'",
    '+
    ' "amount" : "'+opp.Amount+'"
    }';

    System.enqueueJob(new QueueablePMSCall(serviceToken, jsonInput, opp.Id));
    }

    class QueueablePMSCall implements System.Queueable, Database.AllowsCallouts{
    private String serviceToken;
    private String jsonInput;
    private Id oppId;

    public QueueablePMSCall(String serviceToken, String jsonInput, Id oppId){
    this.serviceToken = serviceToken;
    this.jsonInput = jsonInput;
    this.oppId = oppId;
    }

    public void execute(QueueableContext qc){
    postToPMS(serviceToken, jsonInput, oppId);
    }
    }

    @Future(callout=true)
    private static void postToPMS(String serviceToken, String jsonInput, Id oppoId){
    HTTPRequest req = new HTTPRequest();
    req.setEndPoint('callout:ProjectService');
    req.setMethod('POST');
    req.setHeader('token',serviceToken);
    req.setHeader('Content-Type', 'application/json;charset=UTF-8');
    req.setBody(jsonInput);

    HTTP http = new HTTP();
    HTTPResponse res = http.send(req);

    Opportunity opp = new Opportunity(Id=oppoId);
    if(res.getStatusCode() == 201){
    opp.StageName = 'Submitted Project';
    System.debug('Success: ' + res.getStatus());
    }else{
    opp.StageName = 'Resubmit Project';
    System.debug('Failure: ' + res.getStatusCode() + ' ' + res.getStatus());
    }
    update opp;
    }
    }

  • @doruksonmez9218
    @doruksonmez9218 11 месяцев назад +1

    Changed my whole code and still same error. 500 Internal Server Error

    • @amitjodha
      @amitjodha 8 месяцев назад

      I too am facing this

    • @subhambhattacharyya8802
      @subhambhattacharyya8802 6 месяцев назад

      Change "class QueueablePMSCall implements System.Queueable, Database.AllowsCallouts" to "class QueueablePMSCall implements Queueable, Database.AllowsCallouts"