Start a new topic

Updating channeltivity deal from salesforce

Hi All,


I am new to channeltivity and got a requirement where i need someone's help.


My requirement is to Update Channeltivity Close Date when Salesfroce Close Date is changed and Update Channeltivity Stage when Salesfroce Stage is changed. The mapping for stage needs to be following:


SFDC Stage Value  --  Channeltivity Stage Value
Marketing Qualified    Prospecting
Competing    Competing
Technical Eval    Tech Eval
Negotiation    Negotiation
Contracting    Contracting
Closed Won    Closed (Won)
Closed Lost    Closed (Lost)

etc


Can someone please point me to some direction?


Any help is much appreciated!


Thanks


Hi Gopal,


Channeltivity's plug-and-play Deal Registration integration with Salesforce flows data from Channeltivity into Salesforce (see more). In order to push changes from Salesforce back to your Channeltivity PRM, you'll need to write a Salesforce upsert trigger that connects to our Data API. Your trigger should only fire if the Opportunity is linked to Channeltivity (check whether the Channeltivity URL field is set, for example) and will need to pull the Channeltivity Deal Reg's current stage and close date so that you can compare it to the SFDC Opp's stage and close date, respectively. If the values are not the same (and only then), update the Channeltivity Deal Reg with the correct stage and close date values. It's really important that you don't update the Deal Reg if there's nothing to change, because any change on the Channeltivity side will get automatically pushed into Salesforce, which will fire the upsert trigger again.


Please note that you can access Data API documentation in your Channeltivity PRM by going to Admin > Settings > Data API.


Hope this is helpful.


Best regards,


Zach

Hi Zach,

Thanks for the details. I am able to authenticate the salesforce with channeltivity successfully and i am getting a sessionId in response. However when i am trying to make a query call using the sessionId it is saying a bad request with "{"Code":"2004","Description":"Session expired"}" response. I am not sure why sessionId is expiring too quick. Do you have any suggestion?

Thanks,
Gopal

 

Hi Gopal,


I'm happy to take a look. Would you mind posting your code (omitting your credentials or other identifying information, of course)? 


Thanks!


Zach

Hi Zach,


Please see my code below. This is a handler class called from a trigger based on the conditions. Please let me know if you have any questions.


public class ChanneltivityDealsHandler{

   

    @future(callout=true)

    public static void checkAndUpdateDeals(Set<Id> oppIds, Set<String> ctvtUrls) {

        try{

           

            String serverURL;

            String userName;

            String passWord;

           

            Channeltivity_Setting__c settings = Channeltivity_Setting__c.getOrgDefaults();

            if(settings!=null){

                if(settings.Server_URL__c!=null){

                    serverURL = settings.Server_URL__c;

                }

                if(settings.Username__c!=null){

                    userName = settings.Username__c;

                }

                if(settings.Password__c!=null){

                    passWord = settings.Password__c;

                }

               

                HttpRequest request = new HttpRequest();

                request.setEndpoint(serverURL + '/api/v1/CtvtService.svc/json/Login');

                request.setMethod('POST');

                request.setHeader('Content-Type', 'application/json;charset=UTF-8');

                request.setBody('{"username":"' + userName + '","password":"' + passWord + '"}');

               

                String sessionId = (new Http()).send(request).getBody();

               

                System.debug('sessionId>>'+sessionId);

               

                request = new HttpRequest();

                request.setEndpoint(serverURL + '/api/v1/CtvtService.svc/json/Query');

                request.setMethod('POST');

                request.setHeader('Content-Type', 'application/json;charset=UTF-8');

                //request.setHeader('Ctvt-SessionId', sessionId);

                request.setHeader('Authorization', 'OAuth ' + sessionId);

                request.setBody('{"query":"SELECT Key, Stage, CloseDate FROM DealRegistration"}');

               

                String body = (new Http()).send(request).getBody();

               

                System.debug('query result>>'+body);

            }

        }catch(Exception e){

            System.debug('An exception has occurred: ' + e.getMessage() + ' - ' + e.getLineNumber());

        }

    }

}

Hi Gopal,


Here's the working code. For some reason the sessionId had an extra set of double quotes on each end. There's probably a better way to do this, but I simply replaced the quotes from the sessionId variable. Also, the Ctvt-SessionId header must be set and not the Authorization one.


I hope everything works smoothly from now on.


Best regards,


Zach



public class ChanneltivityDealsHandler{

   

    @future(callout=true)

    public static void checkAndUpdateDeals(Set<Id> oppIds, Set<String> ctvtUrls) {

        try{

           

            String serverURL;

            String userName;

            String passWord;

           

            Channeltivity_Setting__c settings = Channeltivity_Setting__c.getOrgDefaults();

            if(settings!=null){

                if(settings.Server_URL__c!=null){

                    serverURL = settings.Server_URL__c;

                }

                if(settings.Username__c!=null){

                    userName = settings.Username__c;

                }

                if(settings.Password__c!=null){

                    passWord = settings.Password__c;

                }

               

                HttpRequest request = new HttpRequest();

                request.setEndpoint(serverURL + '/api/v1/CtvtService.svc/json/Login');

                request.setMethod('POST');

                request.setHeader('Content-Type', 'application/json;charset=UTF-8');

                request.setBody('{"username":"' + userName + '","password":"' + passWord + '"}');

               

                String sessionId = (new Http()).send(request).getBody();

                sessionId = sessionId.replace('"','');

               

                System.debug('sessionId>>'+sessionId);

               

                request = new HttpRequest();

                request.setEndpoint(serverURL + '/api/v1/CtvtService.svc/json/Query');

                request.setMethod('POST');

                request.setHeader('Content-Type', 'application/json;charset=UTF-8');

                request.setHeader('Ctvt-SessionId', sessionId);

                //request.setHeader('Authorization', 'OAuth ' + sessionId);

                request.setBody('{"query":"SELECT Key, Stage, CloseDate FROM DealRegistration"}');

               

                String body = (new Http()).send(request).getBody();

               

                System.debug('query result>>'+body);

            }

        }catch(Exception e){

            System.debug('An exception has occurred: ' + e.getMessage() + ' - ' + e.getLineNumber());

        }

    }

}

Hi Zach,

Great! That Worked! Thanks!
Just wondering if channeltivity supports IN operator in query so e.g. i want to have a query like SELECT Key FROM DealRegistration WHERE Key IN ('1','2','3')

Is that possible? For now i am using the query like SELECT Key FROM DealRegistration WHERE Key = '1' OR  Key = '2' OR Key ='3'

Hi Zach,

One more thing that when i query the Deals, the stage field is returning key. Is there any way i can have stage.name instead of stage.key from the query?


 

Hi Gopal,


Glad it worked. 


Channeltivity's Data API Query command doesn't support the IN operator. We might add it at some point in the future.


Also, you'll need to use the Describe command on the DealRegistration entity to look up the stage names based on the key.


Best regards,


Zach

Login or Signup to post a comment