• sushanth s 2
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 2
    Replies
Hi all,

Could anyone help me to matching the field with two different objects
1.I Have two objects 1.Case 2.custom object Integration__c.here i'm tring to get
the matching the some fields of both objects.

2.Case object fields for matching AccountName and IntegrationTo__c and Integration__c object fields are account__c,integrationwith__c.

3.if the both object fields are matching then get the record details from Integration__c custom object.
for that i have developed a trigger as after insert on Case object.

trigger IntegrationType on Case (after Insert) {
List<ID> cIds = new List<ID>(); //List<Integration__c> integ = new List<Integration__c>();
for(Case cObj : Trigger.new){
if(cObj.Account.Name != null && cObj.IntegrationTo__c != null){
cIds.add(cObj.Id); //add case ids.
}
}
List<Integration__c> ll = [SELECT id,name,username__c,password__c,URL__c,account__c,integrationwith__c FROM Integration__c WHERE Id=:cIds];
for(Integration__c lst : ll){
if(!cIds.isEmpty()){

//help me here how to match the fields

String uname = lst.username__c;
String pwd= lst.password__c;
String url= lst.URL__c;
System.debug('Username :' +uname);
System.debug('Username :' +pwd);
System.debug('Username :' +url);
}
}
}
Hi,

   i'm trying to get the records from external third party application to salesforce case object.
   i'm getting the records from the below REST callout now i'm trying to develop the Aex batch job but i'm stucked at execute() method
  how to develop the execute() based on REST callout

   for the rest callout see below code

  @future (callout=true) global static void getIncident(String subject){
Http http = new Http();
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
string text = subject;
req.setEndpoint('https://myinstance.service-now.com/api/now/table/incident?sysparm_fields=impact%2Cincident_state%2Cshort_description%2Csys_id%2Ccontact_type&sysparm_limit=2&u_sftype=true');
req.setMethod('GET'); req.setHeader('Content-Type', 'application/json');
String username = 'admin';
String password = 'abcd';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
res = http.send(req); System.debug('jsonrResult :' + res.getBody());
Deserialization.ResponseResult res1= (Deserialization.ResponseResult)JSON.deserialize(res.getBody(), Deserialization.ResponseResult.class); System.debug('Results == :' + res1 );
List<Case> casesToUpsert = new List<Case>();
for(Deserialization d : res1.result ){
Case c = new Case();
c.Priority = d.impact;
c.Status = d.incident_state;
c.Subject = d.short_description;
c.ServiceNowId__c = d.sys_id;
c.Origin = d.contact_type;
casesToUpsert.add(c);
} system.debug('Cases to UPsert ::: ' +casesToUpsert);
if(casesToUpsert.size()>0){
Database.upsert(casesToUpsert,false) ;
}
How to develop my Execute() based on my REST callout class

this is my batach class
global class ServiveNowBatch implements Database.Batchable, Database.AllowsCallouts{
global Database.QueryLocator start(Database.BatchableContext BC){
String query = 'SELECT Id, Priority, Status, Subject, ServiceNowId__c, Origin FROM Case WHERE Status=1 LIMIT 50';
return Database.getQueryLocator(query);
} global void execute(Database.BatchableContext BC, List<Case> scope){

}
global void finish(Database.BatchableContext BC){ }
}

help me to develop the execute()

Thanks In Advance
Hi All,


       i have developed a REST class for to integrating salesforce with servicenow.now i'm getting the incidents created in servicenow into salesforce cases that's also working fine "BUT HOW TO WRITE A BATCH JOB" on Salesforce Cases and call it from after insert trigger.

in my apex rest class i'm calling all the incidents of type sftype from servicenow.
i'm encoding in endpoint url:

https://myInstance-now.com/api/now/table/incident?sysparm_fields=impact%2Cincident_state%2Cshort_description%2Csys_id%2Ccontact_type&sysparm_limit=2&u_sftype=true');         

here i'm calling all the incidents from servicenow into salsforce cases.

This is my REST class
global with sharing class SfService {
  
     @future (callout=true) 
     global static void getIncident(String subject){  
        
        Http http = new Http();
        HttpRequest req =  new HttpRequest();
        HttpResponse res = new HttpResponse();
         
        string text = subject;  
       //getting all the newly created incidents since last 20 mins of Type is SfType from serviceNow endpoint 
        req.setEndpoint('https://dev24994.service-now.com/api/now/table/incident?sysparm_fields=impact%2Cincident_state%2Cshort_description%2Csys_id%2Ccontact_type&sysparm_limit=2&u_sftype=true');
      
        req.setMethod('GET');
        req.setHeader('Content-Type', 'application/json'); 
      
        String username = 'admin';
        String password = 'abcd';
        Blob headerValue = Blob.valueOf(username + ':' + password);
        String authorizationHeader = 'BASIC ' +
        EncodingUtil.base64Encode(headerValue);
        req.setHeader('Authorization', authorizationHeader);  
        res = http.send(req);
        System.debug('jsonrResult :' + res.getBody());       
        Deserialization.ResponseResult theresult1 = (Deserialization.ResponseResult)JSON.deserialize(res.getBody(),  Deserialization.ResponseResult.class);
        System.debug('Results == :' + theresult1 ); 
       
        List<Case> casesToUpsert = new List<Case>();        
        for(Deserialization d : theresult1.result ){
                            
                Case c = new Case(); 
                c.Priority = d.impact;
                c.Status = d.incident_state;
                c.Subject = d.short_description;
                c.ServiceNowId__c = d.sys_id;
                c.Origin = d.contact_type;
                
                casesToUpsert.add(c);
               
        }
        system.debug('Cases to UPsert ::: ' +casesToUpsert);
      
        if(casesToUpsert.size()>0){
            Database.upsert(casesToUpsert,false) ;
        }
             
    }
    
}
Anyone help to develop the apex batch job on Cases  and call it from after insert cases so that if that batch job is running itpulling all the incidents from service now of type sftype into salesforce Cases.



Thanks In Advance
Hi,

      i have developed a apex class for connecting with the third party but now i'm going to write a batch job on that anyone guide me to developa batch job.i have look into some examples but completely not understood.

this is my class :


global with sharing class Service {
    
    global static void getIncident(String subject){  
        
        Http http = new Http();
        HttpRequest req =  new HttpRequest();
        HttpResponse res = new HttpResponse();
         
        string text = subject;
            
        req.setEndpoint('https://dev24994.service-now.com/api/now/table/incident?sysparm_fields=impact%2Cincident_state%2Csys_updated_onONLast%2520minute%40javascript%3Ags.minutesAgoStart(30)%40javascript%3Ags.minutesAgoEnd(0)%2Cshort_description%2Csys_id%2Ccontact_type&u_sftype=true&sysparm_limit=10');      
        req.setMethod('GET');
        req.setHeader('Content-Type', 'application/json');
        
        String username = 'admin';
        String password = 'xxxxxx';
       
        Blob headerValue = Blob.valueOf(username + ':' + password);
        String authorizationHeader = 'BASIC ' +
        EncodingUtil.base64Encode(headerValue);
        req.setHeader('Authorization', authorizationHeader);
              
        res = http.send(req);
        System.debug('jsonrResult :' + res.getBody());
      
        Deserialization.ResponseResult result = (Deserialization.ResponseResult)JSON.deserialize(res.getBody(),     Deserialization.ResponseResult.class);
        System.debug('Results == :' + result );
        
        List<Case> casesToUpsert = new List<Case>();        
        for(Deserialization d : theresult.result ){
                            
                Case c = new Case();
                c.Priority = d.impact;
                c.Status = d.incident_state;
                c.Subject = d.short_description;
                c.ServiceNowId__c = d.sys_id;
                c.Origin = d.contact_type;
                
                casesToUpsert.add(c);
               
        }
        system.debug('Cases to UPsert ::: ' +casesToUpsert);
        
        if(casesToUpsert.size()>0){
            Database.upsert(casesToUpsert,false) ;
        }
             
    }
    
}

if it is possible develop batch job on that for better understanding

Thanks In Advance
Hi,

   I'm trying to create incident in serviveNow from saesforce case for that i developed apex class and trigger but it won't shown ant errors so anyone check the code and findout where should i made mistake .after the case created i'm checked ij serviceNow but incident not created .


This is my apex class and trigger

apex class :

global class ServiceNow{      
   @future       
    public static void servicenowPost(Id CaseId){ 
    Case c = [Select Id, CaseNumber,Status from Case where Id =: CaseId limit 1];         
    Http http = new Http();         
    HttpRequest req =  new HttpRequest();         
    HttpResponse res = new HttpResponse();                  
    String username = 'admin';         
    String password = '$Test12345';                  
    Blob headerValue = Blob.valueOf(username + ':' + password);         
    String authorizationHeader = 'BASIC ' +         EncodingUtil.base64Encode(headerValue);               req.setHeader('Authorization', authorizationHeader);                  
req.setEndpoint('https://dev23577.service-now.com/');         
req.setMethod('POST');        
 req.setHeader('Content-Type', 'application/json');                  
JSONGenerator gen = JSON.createGenerator(true);        
 gen.writeStartObject();                 
 //you can add as many mappings as you want        
 gen.writeStringField('Number', c.CaseNumber);        
 gen.writeStringField('State', c.Status);                 
 gen.writeEndObject();                 
 //you can now use String pretty as your body         
String pretty = gen.getAsString();                 
 req.setBody(pretty);         
res = http.send(req);                  
system.debug(res.getBody());     
      }
}


trigger :

trigger servicenow on Case (after insert){
                                                 
  ServiceNow.servicenowPost(trigger.new[0].Id);                                                                          
 
 }

Thanks
 
Hi,

    I'm trying to integrate salesforce and serviceNow.If i create a new case in salesforce that is saved as incident in serviceNow Here i developed some code but i'm stucked at mapping the salesforce case fields with serviveNow incident fieds.


this is my basu code

Public with sharing class OutboundMsg {
 
     
    public static void servicenowPost(){  
        
        Http http = new Http();
        HttpRequest req =  new HttpRequest();
        HttpResponse res = new HttpResponse();
        
         String username = 'admin';
         String password = '$Test12345';
        
        Blob headerValue = Blob.valueOf(username + ':' + password);
        String authorizationHeader = 'BASIC ' +
        EncodingUtil.base64Encode(headerValue);
        req.setHeader('Authorization', authorizationHeader);
  
   
       
        req.setEndpoint('https://dev23577.service-now.com/');
        req.setMethod('POST');
        req.setHeader('Content-Type', 'application/x-www-form-urlencoded'); 
        
        
            
        //Id caseId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
      //  Case c = new Case(subject='',status='new',origin='web');
        //insert c;
       // req.setBody('c');
        
       // Create a new http object to send the request object
       // A response object is generated as a result of the request  
  
       
        res = http.send(req);
        System.debug(res.getBody());
         
 
   }
}

anyone suggest how to estrablish the mapping between salesforce and serviceNow.


Thanks
Hi,

This my apex class  already developed the class with exception handling but i'm not sure this is right way to handle or not so please modift if any better way to handle

public class Helper {

    public static void updateCaseStage(List<Case> cases) {
    
             List<ID> cIds = new List<ID>();
        
             List<Case_User__c> cuser = New  List<Case_User__c>();
             Map<ID, Case_User__c> cusermap = new Map<ID, Case_User__c>();

             Set<String> skillSet = new Set<String>();
             List<String> skillSetList = new List<String>();
    
             for(Case c: cases){
        
                   if(c.Status == 'New') {
                   cIds.add(c.Id); //add case ids.
                   }     
                   skillSet.add(c.CaseRelatedTo__c);
             }   
           skillSetList.addAll(skillSet);    
        
         //available users               
           try{
                   List<Case_User__c> availableCaseUsers = [SELECT AvailableUsers__c, Timings__c, SkillSet__c  FROM Case_User__c where Availability__c = TRUE ORDER BY Last_Case_Allocated_Time__c ASC];
                 //List<Case_User__c> availableCaseUsers = Database.query(queryString);
                    integer i=0;
                    integer nomatchingSkillset = availableCaseUsers.size();
                    for(Case c: cases){
                        for(; i<=availableCaseUsers.size();i++){
                        if(availableCaseUsers.get(i).SkillSet__c.contains(c.CaseRelatedTo__c)){
                          c.OwnerId = availableCaseUsers.get(i).AvailableUsers__c;
                          availableCaseUsers.get(i).Last_Case_Allocated_Time__c = DateTime.now();
                          cusermap.put(availableCaseUsers.get(i).id, availableCaseUsers.get(i));
                         } else {
                              nomatchingSkillset--;
                              if(nomatchingSkillset == 0){
                                  //TODO assign to case queue
                                  
                                     User u =[SELECT Email FROM User WHERE Id IN (SELECT UserOrGroupId FROM GroupMember WHERE Group.DeveloperName ='CaseQueue')];
                                     String us = u.Email;
                                                          
                                      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                                      String[] toAddresses = new String[] {us};
                                      mail.setToAddresses(toAddresses );
                                      mail.setSubject('No Users available to handle this case');
                                      mail.plainTextBody='handle this case';
                                      Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
                                                          
                                  break;
                              }
                              continue;
                        }
                        if(i>=availableCaseUsers.size()){
                            i=0;
                        }
                        break;
                     }      
                }
            }catch(System.DmlException e){
            
                 System.debug('Missing the required field in record');
                 
            }catch(Exception e){
            
                 System.debug('An Error Occured at:' +e.getMessage());
                 
            }finally{
            
                 System.debug('This is block just for closing the class');
            }      
          
 
     if(!cusermap.isEmpty()) {
      update cusermap.values();
     }

        
   }
}

anyone help me please
Thanks In Advance
Hi,

This my apex class  already developed the class with exception handling but i'm not sure this is right way to handle or not so please modift if any better way to handle

public class Helper {

    public static void updateCaseStage(List<Case> cases) {
    
             List<ID> cIds = new List<ID>();
        
             List<Case_User__c> cuser = New  List<Case_User__c>();
             Map<ID, Case_User__c> cusermap = new Map<ID, Case_User__c>();

             Set<String> skillSet = new Set<String>();
             List<String> skillSetList = new List<String>();
    
             for(Case c: cases){
        
                   if(c.Status == 'New') {
                   cIds.add(c.Id); //add case ids.
                   }     
                   skillSet.add(c.CaseRelatedTo__c);
             }   
           skillSetList.addAll(skillSet);    
        
         //available users               
           try{
                   List<Case_User__c> availableCaseUsers = [SELECT AvailableUsers__c, Timings__c, SkillSet__c  FROM Case_User__c where Availability__c = TRUE ORDER BY Last_Case_Allocated_Time__c ASC];
                 //List<Case_User__c> availableCaseUsers = Database.query(queryString);
                    integer i=0;
                    integer nomatchingSkillset = availableCaseUsers.size();
                    for(Case c: cases){
                        for(; i<=availableCaseUsers.size();i++){
                        if(availableCaseUsers.get(i).SkillSet__c.contains(c.CaseRelatedTo__c)){
                          c.OwnerId = availableCaseUsers.get(i).AvailableUsers__c;
                          availableCaseUsers.get(i).Last_Case_Allocated_Time__c = DateTime.now();
                          cusermap.put(availableCaseUsers.get(i).id, availableCaseUsers.get(i));
                         } else {
                              nomatchingSkillset--;
                              if(nomatchingSkillset == 0){
                                  //TODO assign to case queue
                                  
                                     User u =[SELECT Email FROM User WHERE Id IN (SELECT UserOrGroupId FROM GroupMember WHERE Group.DeveloperName ='CaseQueue')];
                                     String us = u.Email;
                                                          
                                      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                                      String[] toAddresses = new String[] {us};
                                      mail.setToAddresses(toAddresses );
                                      mail.setSubject('No Users available to handle this case');
                                      mail.plainTextBody='handle this case';
                                      Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
                                                          
                                  break;
                              }
                              continue;
                        }
                        if(i>=availableCaseUsers.size()){
                            i=0;
                        }
                        break;
                     }      
                }
            }catch(System.DmlException e){
            
                 System.debug('Missing the required field in record');
                 
            }catch(Exception e){
            
                 System.debug('An Error Occured at:' +e.getMessage());
                 
            }finally{
            
                 System.debug('This is block just for closing the class');
            }      
          
 
     if(!cusermap.isEmpty()) {
      update cusermap.values();
     }

        
   }
}

anyone help me please
Thanks In Advance