• Soniya Raut
  • NEWBIE
  • -1 Points
  • Member since 2017
  • System Executive
  • Eternus Solutions pvt ltd

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
public class ClickAndDial {
    public String Phone {get; set;}
    public Id Id { get; set; }
    public Contact contact { get; set; }

    public ClickAndDial(ApexPages.StandardController controller) {
        contact =  (Contact) controller.getRecord();
        Id = contact.Id;
        Phone = contact.Phone;  
    }
    
    public PageReference clickAndDial() {
		PageReference pageRef = new PageReference('/' + Id);
                
       	String token = this.login();
        if (token == null || String.isEmpty(token)) {
            return pageRef;
        }
		Id userId = UserInfo.getUserId();

        System.debug(LoggingLevel.DEBUG,'User ID' +userId);
        List<User> prof = [SELECT Extension FROM User WHERE Id=:userId LIMIT 1];
        if (prof.isEmpty() || String.isEmpty(prof[0].Extension)) {
            return pageRef;
        }
		String myExt = prof[0].Extension;
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
  
        request.setMethod('POST');
        request.setEndpoint('callout:Voismart/jsondata/extjs/dialerHandler/click_and_dial');
        request.setHeader('Content-Type', 'application/x-www-form-urlencoded'); 
        String payload = 
            'token=' + EncodingUtil.urlEncode(token,'UTF-8') 
            + '&data='+ EncodingUtil.urlEncode(
                '[{"number_to_call":"' + Phone + 
                '", "number_to_connect":"' + myExt + 
                '", "auto_answer": true}]'
                ,'UTF-8');
        request.setBody(payload);
        HttpResponse res = http.send(request);
        
        return pageRef;
    }
    
    public String login() {
        Http http = new Http();
        HTTPRequest loginRequest = new HTTPRequest();
        loginRequest.setMethod('POST');
		loginRequest.setEndpoint('callout:Voismart');
		loginRequest.setHeader('Content-Type', 'application/x-www-form-urlencoded');

        
        String loginPayload = 
            'username=' + '{!$Credential.UserName}' + 
            '&password={!$Credential.Password}' +
            '&api=true' + '&browser_tz=' + EncodingUtil.urlEncode('Europe/Rome','UTF-8')+
            '&request_date='+ EncodingUtil.urlEncode(string.valueOfGmt(System.now()),'UTF-8');
        	
		loginRequest.setBody(loginPayload);
        HTTPResponse loginRes = http.send(loginRequest);
        System.debug('========>loginRes'+loginRes);

        Map<String, Object> values = (Map<String, Object>)JSON.deserializeUntyped(loginRes.getBody());
		String token = (String)values.get('token');
        return token;
    }
}

 
I have an issue . That is we need to remove "industry" field if the account type is "Prospect".