• Varsha D 9
  • NEWBIE
  • 30 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 7
    Replies
public class Api4 {
   public static void api(){
    Http h = new Http();
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint('https://demo1691926.mockable.io/Api12');
req.setHeader('Content-Type', 'application/json');
HttpResponse response = h.send(req);
system.debug('response  : '+response.getBody());
String json1= response.getBody();

       
DesearlizingJson desearilize1=(DesearlizingJson) Json.deserialize(json1,DesearlizingJson.Class);
System.debug('Phone is ' +desearilize1.phone);
System.debug('name is ' +desearilize1.Name);
System.debug('mobile is ' +desearilize1.MobilePhone);
      
 //trying to update deserialized json       
list<contact> lcon= new list<contact>();

List<contact> lcon1 = [select id,Name,MobilePhone,Phone from contact where LastName=:desearilize1.Name];
       for(contact c1:lcon1){
        
        c1.Phone=desearilize1.phone;
           lcon.add(c1);}
        update c1;
       
  }
}

Wrapper class

public class DesearlizingJson {
    public Decimal Phone;
    public String Name;
    public Decimal MobilePhone;

}

Thanks in Advance
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint('https://demo1691926.mockable.io/Api12');
req.setHeader('Content-Type', 'application/json');
HttpResponse response = h.send(req);
system.debug('response  : '+response.getBody());
String json1= response.getBody();
DesearlizingJson desearilize1=(DesearlizingJson) Json.deserialize(json1,DesearlizingJson.Class);
System.debug('Phone is' +desearilize1.phone);
System.debug('name  is' +desearilize1.name);


Wrapper class
public class DesearlizingJson {
    public Decimal phone;
    public String name;
    public Decimal mobile;

}
json:
{
 "Phone":75645433221,
 "name":"var",
 "mobile":8766555544
 }
ouput is:
phone is :87675565
name is  : varsha


i need help how to retrieve (i,e Expected output:)
phone:8898999
name:varsha 

i,e how to retrive both key and its corresponding value in debug log

Thanks in Advance 
 
User-added imageUser-added imageUser-added image
I m unable to generate my access token 
Error says:<OAuth>
<error>unsupported_grant_type</error>
<error_description>grant type not supported</error_description>
</OAuth>....this in my access token url
@HttpGet
     global static void sendMail(){
        RestRequest request = RestContext.request;
        String accountId = request.params.get('id');//error
        system.debug(accountId);
        Account objAccount = [SELECT Id,Name,(SELECT Id,Name FROM Contacts) FROM Account WHERE Id = :accountId LIMIT 1];
        
    
   // global static void sendMail(){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[]{'varsha.rcr37@gmail.com'}; 
 mail.setToAddresses(toAddresses);
     
//Contact con = [SELECT id, firstname FROM Contact LIMIT 1];
EmailTemplate et = [SELECT id FROM EmailTemplate WHERE developerName = 'gmail_salesforce'];
 

 
mail.setTargetObjectId(objAccount.id); 
mail.setTemplateId(et.id);
        
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
     }
}
User-added imageUser-added imageglobal class Email2 {
    @HttpGet
    global static void sendMail(){
list<Messaging.SingleEmailMessage> mail = new list<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage mails = new Messaging.SingleEmailMessage();
 
list <String> toAddresses = new list<String>();
 toAddresses.add('varsha.dustakar7@gmail.com');
 mails.setToAddresses(toAddresses);
 
List<Contact> con = [SELECT Id, firstname FROM Contact];
 List<Id> lstids= new List<Id>();
 for(Contact c:con) {
  lstids.add(c.id);
 }
EmailTemplate et = [SELECT id FROM EmailTemplate WHERE developerName = 'gmail_salesforce'];
mails.setTargetObjectId(lstids); 
mails.setTemplateId(et.id);
mail.add(mails); 
Messaging.sendEmail(new Messaging.singleEmailMessage[] { mails });
    }
}
This is my apex code for generating mail through rest api @RestResource(urlMapping='/Email_2/*')    

global class Email2 {
@HttpPost
    // Public method
    global static void sendMail(String address, String subject, String body) {
        // Create an email message object
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new List<String>{'varsha.rcr37@gmail.com'};
        mail.setToAddresses(toAddresses);
        mail.setSubject('subject2');
        mail.setPlainTextBody('body2');
        // Pass this email message to the built-in sendEmail method 
        // of the Messaging class
        Messaging.SendEmailResult[] results = Messaging.sendEmail(
                                 new Messaging.SingleEmailMessage[] { mail });
        
    }
User-added imagei tried regenerating my token tats not working for me .....Whether i need to insert some HEADER or just Authorization Oauth 2.0 is fine...
Thanks in Advance
 
public class Api4 {
   public static void api(){
    Http h = new Http();
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint('https://demo1691926.mockable.io/Api12');
req.setHeader('Content-Type', 'application/json');
HttpResponse response = h.send(req);
system.debug('response  : '+response.getBody());
String json1= response.getBody();

       
DesearlizingJson desearilize1=(DesearlizingJson) Json.deserialize(json1,DesearlizingJson.Class);
System.debug('Phone is ' +desearilize1.phone);
System.debug('name is ' +desearilize1.Name);
System.debug('mobile is ' +desearilize1.MobilePhone);
      
 //trying to update deserialized json       
list<contact> lcon= new list<contact>();

List<contact> lcon1 = [select id,Name,MobilePhone,Phone from contact where LastName=:desearilize1.Name];
       for(contact c1:lcon1){
        
        c1.Phone=desearilize1.phone;
           lcon.add(c1);}
        update c1;
       
  }
}

Wrapper class

public class DesearlizingJson {
    public Decimal Phone;
    public String Name;
    public Decimal MobilePhone;

}

Thanks in Advance
public class Api4 {
   public static void api(){
    Http h = new Http();
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint('https://demo1691926.mockable.io/Api12');
req.setHeader('Content-Type', 'application/json');
HttpResponse response = h.send(req);
system.debug('response  : '+response.getBody());
String json1= response.getBody();

       
DesearlizingJson desearilize1=(DesearlizingJson) Json.deserialize(json1,DesearlizingJson.Class);
System.debug('Phone is ' +desearilize1.phone);
System.debug('name is ' +desearilize1.Name);
System.debug('mobile is ' +desearilize1.MobilePhone);
      
 //trying to update deserialized json       
list<contact> lcon= new list<contact>();

List<contact> lcon1 = [select id,Name,MobilePhone,Phone from contact where LastName=:desearilize1.Name];
       for(contact c1:lcon1){
        
        c1.Phone=desearilize1.phone;
           lcon.add(c1);}
        update c1;
       
  }
}

Wrapper class

public class DesearlizingJson {
    public Decimal Phone;
    public String Name;
    public Decimal MobilePhone;

}

Thanks in Advance
@HttpGet
     global static void sendMail(){
        RestRequest request = RestContext.request;
        String accountId = request.params.get('id');//error
        system.debug(accountId);
        Account objAccount = [SELECT Id,Name,(SELECT Id,Name FROM Contacts) FROM Account WHERE Id = :accountId LIMIT 1];
        
    
   // global static void sendMail(){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[]{'varsha.rcr37@gmail.com'}; 
 mail.setToAddresses(toAddresses);
     
//Contact con = [SELECT id, firstname FROM Contact LIMIT 1];
EmailTemplate et = [SELECT id FROM EmailTemplate WHERE developerName = 'gmail_salesforce'];
 

 
mail.setTargetObjectId(objAccount.id); 
mail.setTemplateId(et.id);
        
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
     }
}
User-added imageUser-added imageglobal class Email2 {
    @HttpGet
    global static void sendMail(){
list<Messaging.SingleEmailMessage> mail = new list<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage mails = new Messaging.SingleEmailMessage();
 
list <String> toAddresses = new list<String>();
 toAddresses.add('varsha.dustakar7@gmail.com');
 mails.setToAddresses(toAddresses);
 
List<Contact> con = [SELECT Id, firstname FROM Contact];
 List<Id> lstids= new List<Id>();
 for(Contact c:con) {
  lstids.add(c.id);
 }
EmailTemplate et = [SELECT id FROM EmailTemplate WHERE developerName = 'gmail_salesforce'];
mails.setTargetObjectId(lstids); 
mails.setTemplateId(et.id);
mail.add(mails); 
Messaging.sendEmail(new Messaging.singleEmailMessage[] { mails });
    }
}