• Shruthi Ravi 8
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 0
    Replies
I have turned on pre chat form settings and selected the lead field. I Ccan see the field values in the details page before accepting the chat.

But when I accept the chat, lead reacord page is not poping up. Im not sure why it is'nt working

Im I missing soething here?
Test class
@isTest
public with sharing class SynthesisApiProxyTest {
    public SynthesisApiProxyTest() {}

    private
     class MockHttpResponseGenerator implements HttpCalloutMock {
       // Implement this interface method
      public HTTPResponse respond(HTTPRequest req) {
         String endpoint = req.getEndpoint();

         // Create a fake response
         HttpResponse res = new HttpResponse();
         res.setHeader('Content-Type', 'application/json');
         res.setStatusCode(200);

        System.assertEquals('POST', req.getMethod());
        System.assertEquals('https://test.uvwxxx.com/method', endpoint);

         res.setBody('{}');
         return res;
       }
     }

     @isTest
     static void testCallSynthesisApi() {
        Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());

        SynthesisInternal__c internal = new SynthesisInternal__c();
        internal.name = SynthesisOauthHandler.getParentTokenName();
        internal.Endpoint__c = 'https://test.uvwx.com/';
        insert internal;

        SynthesisOAuthToken__c token = new SynthesisOAuthToken__c();
        token.Synthesis_Internal__c = internal.Id;
        token.AccessToken__c = 'access_token_hooray';
        token.User__c = UserInfo.getUserId();
        insert token;

        Test.startTest();
        Object response = SynthesisApiProxy.callSynthesisApi(
          'POST', 'method', JSON.deserializeUntyped('{"foo":"bar"}'));
        Test.stopTest();
     }
}
 
public
with sharing class SynthesisApiProxy {
 public
  SynthesisApiProxy() {}

  /**
  * Make an API call to the Synthesis API. The stored access token will be used
  * if a new one is not specified.
  */
  @AuraEnabled(cacheable = false)
  public static Object callSynthesisApi(String apiMethod,
                                        String apiPath,
                                        Object body,
                                        String newAccessToken) {
    SynthesisOAuthToken__c token = SynthesisOauthHandler.getOauthToken();
    String accessToken;
    // Default to using the specified token, if one was specified.
    // Otherwise use the one saved on the user's custom object.
    if (newAccessToken == null) {
      accessToken = token.AccessToken__c;
    } else {
      accessToken = newAccessToken;
    }
    // If a token was not found on the record, tell the UI to get a new one.
    if (accessToken == null) {
      // TODO(shauvon): Tell the UI to start the oauth flow.
      return JSON.deserializeUntyped('{}');
      // newAccessToken = SynthesisOauthHandler.refreshOAuth(token);
      // accessToken = newAccessToken;
    }

    String payload = JSON.serialize(body);

    String endpoint = token.Synthesis_Internal__r.Endpoint__c + apiPath;
    HttpRequest httpRequest = new HttpRequest();
    httpRequest.setEndpoint(endpoint);
    httpRequest.setHeader('Authorization', 'Bearer ' + accessToken);
    httpRequest.setTimeout(30000);
    httpRequest.setHeader('Content-Type', 'application/json');
    if (apiMethod == 'PATCH') {
      httpRequest.setHeader('X-HTTP-Method-Override','PATCH');
      httpRequest.setMethod('POST');
    } else {
      httpRequest.setMethod(apiMethod);
    }
    if (body != null) {
      httpRequest.setBody(payload);
    }
    Http http = new Http();
    System.debug('Sending call to API ' + apiMethod + ': ' + apiPath + ', body ' + payload);
    HttpResponse httpResponse = http.send(httpRequest);

    // If the token is not accepted, get a new one and try again.
    if (httpResponse.getStatusCode() == 401 && newAccessToken == null) {
      // Apex won't allow updating a record and then calling an API,
      // so we have to call the API first, and then upsert the new token.
      System.debug('Getting new token!');
      if (token.RefreshToken__c != null) {
        Map<String, String> tokens = SynthesisOauthHandler.refreshUserOAuth(token);
        newAccessToken = tokens.get('AccessToken__c');
        token.RefreshToken__c = tokens.get('RefreshToken__c');
      }
      Object data = callSynthesisApi(apiMethod, apiPath, body, newAccessToken);
      return data;
    } else {
      // If we have a new token, save it to the record now.
      if (newAccessToken != null) {
        token.AccessToken__c = newAccessToken;
        upsert token;
      }
      String responseBody = httpResponse.getBody();
      try {
        return JSON.deserializeUntyped(responseBody);
      } catch (Exception e) {
        System.debug('Unable to parse json from ' + responseBody);
        throw e;
      }
    }

  }

  @AuraEnabled(cacheable = false)
  public static Object callSynthesisApi(String apiMethod,
                                        String apiPath,
                                        Object body) {
    return callSynthesisApi(apiMethod, apiPath, body, null);
  }
}

 
Hi,

Im unable to save the status field on the record once the record is saved. It throws me an error "opp already exists this record :: !!!"   Please help me to fix it.

Below is the triigger which is throwing the error
trigger SegmentationRequestsTrigger on Segmentation_Request__c (before insert,before Update) {
    
    Map<String,String> agencybyOppId = new Map<String,String>();
    Map<Id,List<String>> agencyIdByOppId = new Map<Id,List<String>>(); 
    
    String tempString = '';
    Set<String> agencyId = new Set<String>();
    for(Segmentation_Request__c SegmentationRequest : [SELECT   Agency_ID__c, Approval_Status__c, Opportunity__c FROM Segmentation_Request__c WHERE Opportunity__c != '']){
        
        if(String.isNotBlank(SegmentationRequest.Agency_ID__c) && String.isNotBlank(SegmentationRequest.Approval_Status__c)){
            tempString = SegmentationRequest.Agency_ID__c.trim()+':'+SegmentationRequest.Approval_Status__c;
            if(SegmentationRequest.Approval_Status__c == 'Requested'){ 
                if(agencyIdByOppId != null && agencyIdByOppId.containsKey(SegmentationRequest.Opportunity__c)){
                    List<String> temp = agencyIdByOppId.get(SegmentationRequest.Opportunity__c);  
                    temp.add(tempString);
                    agencyIdByOppId.put(SegmentationRequest.Opportunity__c,temp);
                }else{ 
                    agencyIdByOppId.put(SegmentationRequest.Opportunity__c,new List<String>{tempString});
                }
            } 
            //segIdByAgencyId.put(SegmentationRequest.Id,SegmentationRequest.Approval_Status__c);
            agencybyOppId.put(SegmentationRequest.Agency_ID__c.trim(),SegmentationRequest.Opportunity__c); 
        }
    } 
    
    for(Segmentation_Request__c SegmentationRequest : Trigger.New){
        if(String.isNotBlank(SegmentationRequest.Agency_ID__c) && String.isNotBlank(SegmentationRequest.Approval_Status__c)  && 
           (Trigger.isInsert ||  (Trigger.isUpdate && ((SegmentationRequest.Agency_ID__c != trigger.oldMap.get(SegmentationRequest.Id).Agency_ID__c)|| (SegmentationRequest.Approval_Status__c != trigger.oldMap.get(SegmentationRequest.Id).Approval_Status__c))))){
               system.debug('@@## SegmentationRequest.Opportunity__c '+SegmentationRequest.Opportunity__c);
               system.debug('@@## agencyIdByOppId.containsKey(SegmentationRequest.Opportunity__c) '+agencyIdByOppId.containsKey(SegmentationRequest.Opportunity__c));
               if(agencyIdByOppId != null && agencyIdByOppId.containsKey(SegmentationRequest.Opportunity__c)){
                   String strPreRequested = SegmentationRequest.Agency_ID__c.trim() + ':Requested';
                   tempString = SegmentationRequest.Agency_ID__c.trim()+':'+SegmentationRequest.Approval_Status__c;
                   List<String> temp = agencyIdByOppId.get(SegmentationRequest.Opportunity__c);
                   system.debug('@@## Update '+tempString);
                   if(temp != null && temp.size() > 1 ){ 
                       for(String agencywithStatus : temp){ 
                           system.debug('@@## agencywithStatus '+agencywithStatus);
                           system.debug('@@## strPreRequested '+strPreRequested);
                           system.debug('@@## 1 '+(agencywithStatus == tempString));
                           system.debug('@@## 2 '+(strPreRequested == agencywithStatus));
                           if(agencywithStatus == tempString || strPreRequested == agencywithStatus){
                               system.debug('@@## Same Id ');
                               system.debug('@@## '+agencybyOppId.containsKey(SegmentationRequest.Agency_ID__c.trim()));
                               system.debug('@@## '+agencybyOppId.get(SegmentationRequest.Agency_ID__c.trim()));
                               system.debug('@@## '+SegmentationRequest.Opportunity__c);
                               if(agencybyOppId != null && agencybyOppId.containsKey(SegmentationRequest.Agency_ID__c.trim()) && agencybyOppId.get(SegmentationRequest.Agency_ID__c.trim()) == SegmentationRequest.Opportunity__c ){
                                   SegmentationRequest.addError('opp already exists this record :: !!!');
                               }
                           }
                           
                       }
                   }
               } 
           } 
    }