• Joseph Gillick 5
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 0
    Replies
Dear SF community, 

I am a novice to development and am having trouble getting a test class to even get 1% of coverage for the below code:
 
global with sharing class LeopardSubscriptionUpdate {
    
    @InvocableMethod(label='Get Opp Id' description='Send Opportunity Into after closing')
    global static void GetOppId(List<Id> NewOppId) {
        List<Opportunity> NewOppIds =[Select Id 
                from Opportunity 
                where Id in :NewOppId ];
        System.enqueueJob(new makeGetCallout(NewOppId));
    }

    global class makeGetCallout implements System.Queueable, Database.AllowsCallouts {
        List<Id> NewOppId = new List<Id>() ; 
        global makeGetCallout(List<Id> ids){
            NewOppId = ids ;
        }

        global void execute(System.QueueableContext ctx) {

            Opportunity o= [Select Id ,
                Owner.Name,
                Subscription_Type__c,
                AVSFQB__Primary_Contact__r.FirstName, 
                AVSFQB__Primary_Contact__r.LastName, 
                SBQQ__PrimaryQuote__r.InHouse_Credits__c, 
                SBQQ__PrimaryQuote__r.SBQQ__EndDate__c, 
                Account.name, Account.Id, Account.Market_Segment__c, 
                AVSFQB__Billing_State__c, 
                AVSFQB__Primary_Contact__r.Email 
                from Opportunity 
                where Id in :NewOppId ] ; 
            List<OpportunityLineItem> prodCodeList = [Select ProductCode from OpportunityLineItem];
            JSONGenerator gen = JSON.createGenerator(true); 
            gen.writeStartObject();      
            gen.writeStringField('CustomerUserFirstName', o.AVSFQB__Primary_Contact__r.FirstName);
            gen.writeStringField('CustomerUserLastName', o.AVSFQB__Primary_Contact__r.LastName);
            gen.writeStringField('CustomerName',o.Account.name);
            gen.writeStringField('UpdatedBy',o.Owner.Name);
            gen.writeStringField('SubscriptionLevel',o.Subscription_Type__c);
            gen.writeStringField('CustomerSalesforceId',o.Account.Id);
            gen.writeFieldName('Products');
            gen.writeStartArray();
            for ( OpportunityLineItem  c : prodCodeList) {
                gen.writeString( c.ProductCode);
                }
            gen.writeEndArray();

            gen.writeStringField('OrganizationType',o.Account.Market_Segment__c);
            gen.writeNumberField('InHouseDownloadCredits',o.SBQQ__PrimaryQuote__r.InHouse_Credits__c);
            gen.writeDateField('EndDate',o.SBQQ__PrimaryQuote__r.SBQQ__EndDate__c);      
            gen.writeStringField('Region',o.AVSFQB__Billing_State__c);
            gen.writeStringField('Email', o.AVSFQB__Primary_Contact__r.Email);
            gen.writeEndObject();    
            String jsonS = gen.getAsString();            
            System.debug('jsonMaterials'+jsonS);

        // Sending the http body with JSON 

            String endpoint = '*endpoiint*.com';
            HttpRequest req = new HttpRequest();
            req.setEndpoint(endpoint);
            req.setMethod('POST');
            req.setbody(jsonS);
            Http http = new Http();
            HTTPResponse response = http.send(req);
            //return response;  

        }
    
    }
}

I'm attempting to trigger this via the process builder on an opportunity field update. If anyone has any thoughts on test classes for this, I'd be forever indebted. 

All the best,

Joe
Dear Salesforce community,

I'm trying to send a Post request to an external system. In this json I am sending fields from an opportunity. I ran in to a problem when trying to add all the product line item, product codes in to the class that generates the json. I keep getting this error message: static can only be used on methods of a top level type. This is what my code looks like at the moment:
 
public class LeopardSubscriptionUpdate {
    public static HttpResponse makeGetCallout() {

 

Opportunity o= [Select Id ,AVSFQB__Primary_Contact__r.name, Account.name, Account.Market_Segment__c, AVSFQB__Billing_State__c, AVSFQB__Primary_Contact__r.Email from Opportunity Limit 1] ; 
JSONGenerator gen = JSON.createGenerator(true); 
    gen.writeStartObject();      
    gen.writeStringField('Customer User Full Name', o.AVSFQB__Primary_Contact__r.name);
    gen.writeStringField('Customer Name',o.Account.name);
    gen.writeStringField('Organization Type',o.Account.Market_Segment__c);
    gen.writeStringField('State',o.AVSFQB__Billing_State__c);
    gen.writeStringField('Email', o.AVSFQB__Primary_Contact__r.Email);
    gen.writeEndObject();    
    String jsonS = gen.getAsString();
System.debug('jsonMaterials'+jsonS);
string returnValue = generateStringArray();
System.debug(returnValue);

 

// Sending the http body with JSON 

 

String endpoint = 'myurl.com';
HttpRequest req = new HttpRequest();
req.setEndpoint(endpoint);
req.setMethod('POST');
req.setbody(jsonS);
Http http = new Http();
HTTPResponse response = http.send(req);
  return response;  }
    
   public void generateStringArray(){
        List<OpportunityLineItem> prodCodeList = [Select ProductCode from OpportunityLineItem];
    }
}

I feel like i'm failing to understand the relationship between public classes, public static classes and public void classes. I'm not a developer and kind of just winging it.

Thank you!