+ Start a Discussion
Matt Blatnik 8Matt Blatnik 8 

Apex Code that generates a S2S URL

Hi,

I am new to Apex coding and have a task at work.

I need code that will generate a S2S URL for every opportunity that has a GoLiveDate.

S2S URL should be in the following format:

Below is our S2S URL (you will need to call this for the relevant product / event):
 
https://track.omguk.com/apptag.asp?APPID={UniqueID}&MID=891203&PID={PID}status=&sskey={sskey}
 
 
Parameter nameRequired value
APPIDUnique ID – for leads this can be a unique lead ID, for sales, this will be the policy number
PIDPID from the table below
SSKEYOur SSKEY – we will generate this for each click and pass over in a landing page parameter

I have created a Apex trigger:

trigger GoLiveDateChange on Opportunity (before update) {
for (Opportunity Opp : Trigger.new) {
    if ( Trigger.oldMap.get(Opp.ID).Go_Live_Date__c != Trigger.newMap.get(Opp.ID).Go_Live_Date__c );
    { SendInfo2External.GoLiveUpdate(Opp.ID);
        System.debug('Opp.ID');
     }
   }
}


But am stuck with apex class. I have came up to here:

public class SendInfo2External {
@future (callout=true)
 public static void GoLiveUpdate(id)
 {
   //Construct HTTP request and response
    //Http request method,Endpoint and setBody
    HttpRequest req = new HttpRequest();
    req.setHeader('Content-Type','application/json');
    String endpoint = 'https://track.omguk.com/apptag.asp?' + 'APPID'+ '&MID=891203&'+ 'PID={PID}status=&sskey={sskey}';
     PID pid=[select medium__c from Opportunity where id=id];
     APPID appid=[select id from Opportunity where id=id];
    }
}

Can anyone help me?

Thank you,

Matt
 
Pankaj_GanwaniPankaj_Ganwani
Hi Matt,

Please use below mentioned code. Make sure you are providing correct sskey in the endpoint string and while testing for bulk records set the batch size to 100 since we have 100 callouts limitation in one transaction:

trigger GoLiveDateChange on Opportunity (after update) {
Set<Id> setOppId = new Set<Id>();
for (Opportunity Opp : Trigger.new) {
    if ( Trigger.oldMap.get(Opp.ID).Go_Live_Date__c != Trigger.newMap.get(Opp.ID).Go_Live_Date__c );
    { 
    setOppId.add(Opp.Id);
     }
   }

 SendInfo2External.GoLiveUpdate(setOppId);
}

public class SendInfo2External {
@future (callout=true)
 public static void GoLiveUpdate(Set<Id> setOppId)
 {
   //Construct HTTP request and response
    //Http request method,Endpoint and setBody
    List<Opportunity> lstUpdateOpp = new List<Opportunity>();
    for(Opportunity objOpp : [select medium__c,Id from Opportunity where Id In : setOppId])
    {
    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setHeader('Content-Type','application/json');
    String endpoint = 'https://track.omguk.com/apptag.asp?APPID='+objOpp.Id+'&MID=891203&PID='+objOpp.Medium__c+'status=&sskey=';
    req.setEndPoint(endpoint);
    req.setMethod('GET');
    HttpResponse res = h.send(req);
    lstUpdateOpp.add(new Opportunity(Id=objOpp.Id, Url__c = res.getBody());
    }
    update lstUpdateOpp;
}
}
Matt Blatnik 8Matt Blatnik 8
Hi Pankaj,

What is the Url__c field?

regards,

Matt
Matt Blatnik 8Matt Blatnik 8
Sorry Pankaj,

I am not required to fill in a URL field, but to ping the composed URL instead.

How can I do that?

Regards,

Matt
Pankaj_GanwaniPankaj_Ganwani
Hi Matt,

Okay, Then what is the use of that callout? Ideally, you should store the response somewhere. However, if your intention is to ping the end point url only, then you can simple perform callout by making changes in class:

public class SendInfo2External {
@future (callout=true)
 public static void GoLiveUpdate(Set<Id> setOppId)
 {
   //Construct HTTP request and response
    //Http request method,Endpoint and setBody
    List<Opportunity> lstUpdateOpp = new List<Opportunity>();
    for(Opportunity objOpp : [select medium__c,Id from Opportunity where Id In : setOppId])
    {
    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setHeader('Content-Type','application/json');
    String endpoint = 'https://track.omguk.com/apptag.asp?APPID='+objOpp.Id+'&MID=891203&PID='+objOpp.Medium__c+'status=&sskey=';
    req.setEndPoint(endpoint);
    req.setMethod('GET');
    HttpResponse res = h.send(req);
    system.debug('=================='+res.getBody());
    }
}
}

I removed the DML operation from the above code.
Matt Blatnik 8Matt Blatnik 8
Hi Pankaj,

I have updated the class. Wrote a test class, but it fails on run.

@IsTest (SeeAllData=true) public class CreateTestAccOpp {     static TestMethod void Test1(){         Account Acc = new Account(Name = 'ABC');     insert Acc;         Opportunity Opp = new Opportunity (Name = 'TestDrewberry', CloseDate=system.today(),Go_Live_Date__c = system.today(),StageName = 'Qualified', AccountId = Acc.id, source__c='unbiased.co.uk', LeadSource = 'MPPI(ASU)');     insert Opp;
}
}

 
Pankaj_GanwaniPankaj_Ganwani
Hi Matt,

Can you please share the error message with me?